-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.go
More file actions
34 lines (29 loc) · 856 Bytes
/
string.go
File metadata and controls
34 lines (29 loc) · 856 Bytes
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
31
32
33
34
package bogo
import (
"bytes"
"errors"
)
var stringEncodeError = errors.New("string encoding error")
func encodeString(v string) ([]byte, error) {
// Always use the standard encoding format, even for empty strings
lenInfoBytes, err := encodeUint(uint64(len(v)))
if err != nil {
return []byte{}, wrapError(stringEncodeError, err.Error())
}
buf := bytes.Buffer{}
if err = buf.WriteByte(byte(TypeString)); err != nil {
return []byte{}, wrapError(stringEncodeError, err.Error())
}
if _, err = buf.Write(lenInfoBytes[1:]); err != nil {
return []byte{}, wrapError(stringEncodeError, err.Error())
}
buf.WriteString(v)
return buf.Bytes(), nil
}
func decodeString(data []byte, sizeLen int) (any, error) {
size, err := decodeUint(data[:sizeLen])
if err != nil {
return nil, err
}
return string(data[sizeLen : sizeLen+int(size)]), nil
}