fastpastebin/vendor/github.com/rs/zerolog/internal/cbor/string.go

69 lines
1.7 KiB
Go
Raw Normal View History

2018-04-30 18:42:17 +05:00
package cbor
// AppendStrings encodes and adds an array of strings to the dst byte array.
2019-03-07 07:56:50 +05:00
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
2018-04-30 18:42:17 +05:00
major := majorTypeArray
l := len(vals)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, byte(major|lb))
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
2019-03-07 07:56:50 +05:00
dst = e.AppendString(dst, v)
2018-04-30 18:42:17 +05:00
}
return dst
}
// AppendString encodes and adds a string to the dst byte array.
2019-03-07 07:56:50 +05:00
func (Encoder) AppendString(dst []byte, s string) []byte {
2018-04-30 18:42:17 +05:00
major := majorTypeUtf8String
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, byte(major|lb))
} else {
dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l))
}
return append(dst, s...)
}
// AppendBytes encodes and adds an array of bytes to the dst byte array.
2019-03-07 07:56:50 +05:00
func (Encoder) AppendBytes(dst, s []byte) []byte {
2018-04-30 18:42:17 +05:00
major := majorTypeByteString
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, byte(major|lb))
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
return append(dst, s...)
}
// AppendEmbeddedJSON adds a tag and embeds input JSON as such.
func AppendEmbeddedJSON(dst, s []byte) []byte {
major := majorTypeTags
minor := additionalTypeEmbeddedJSON
2019-03-07 07:56:50 +05:00
// Append the TAG to indicate this is Embedded JSON.
dst = append(dst, byte(major|additionalTypeIntUint16))
dst = append(dst, byte(minor>>8))
dst = append(dst, byte(minor&0xff))
// Append the JSON Object as Byte String.
2018-04-30 18:42:17 +05:00
major = majorTypeByteString
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, byte(major|lb))
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
return append(dst, s...)
}