blob: 572651fa09e4edeef40ceb65dc3ec3e98786c45d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
// Package misc provides miscellaneous functions.
package misc
import (
"errors"
"strings"
)
// Copy_map the map src to dst without clearing existing items in dst.
func Copy_map[K comparable, V any](dst map[K]V, src map[K]V) {
for k, v := range src {
dst[k] = v
}
}
// String_to_byte_ptr returns a pointer to the first byte of a string. It
// ensures that the returned pointer is null-terminated.
func String_to_byte_ptr(s string) (*byte, error) {
// If the string already contains a null then whoever attempts to
// interpret this as a null-terminated string won't be able to see the
// whole string. This is probably not expected by the caller.
if strings.IndexByte(s, 0) != -1 {
return nil, Err_null_byte
}
buf := make([]byte, len(s)+1) // Zeros them out...
copy(buf, s) // ... so the last byte would be null.
return &buf[0], nil
}
var Err_null_byte = errors.New("string contains null byte")
|