2018-03-28 18:49:41 +00:00
|
|
|
package cbor
|
|
|
|
|
|
|
|
// AppendStrings encodes and adds an array of strings to the dst byte array.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
|
2018-03-28 18:49:41 +00: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 {
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = e.AppendString(dst, v)
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendString encodes and adds a string to the dst byte array.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (Encoder) AppendString(dst []byte, s string) []byte {
|
2018-03-28 18:49:41 +00: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.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (Encoder) AppendBytes(dst, s []byte) []byte {
|
2018-03-28 18:49:41 +00: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.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (Encoder) AppendEmbeddedJSON(dst, s []byte) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
major := majorTypeTags
|
|
|
|
minor := additionalTypeEmbeddedJSON
|
|
|
|
|
2018-04-03 21:07:18 +00: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-03-28 18:49:41 +00: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...)
|
|
|
|
}
|