blob: ff42afab4d73844167fe559cc361e6a37c5ced7e [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001package cbor
2
3// AppendStrings encodes and adds an array of strings to the dst byte array.
4func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
5 major := majorTypeArray
6 l := len(vals)
7 if l <= additionalMax {
8 lb := byte(l)
9 dst = append(dst, byte(major|lb))
10 } else {
11 dst = appendCborTypePrefix(dst, major, uint64(l))
12 }
13 for _, v := range vals {
14 dst = e.AppendString(dst, v)
15 }
16 return dst
17}
18
19// AppendString encodes and adds a string to the dst byte array.
20func (Encoder) AppendString(dst []byte, s string) []byte {
21 major := majorTypeUtf8String
22
23 l := len(s)
24 if l <= additionalMax {
25 lb := byte(l)
26 dst = append(dst, byte(major|lb))
27 } else {
28 dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l))
29 }
30 return append(dst, s...)
31}
32
33// AppendBytes encodes and adds an array of bytes to the dst byte array.
34func (Encoder) AppendBytes(dst, s []byte) []byte {
35 major := majorTypeByteString
36
37 l := len(s)
38 if l <= additionalMax {
39 lb := byte(l)
40 dst = append(dst, byte(major|lb))
41 } else {
42 dst = appendCborTypePrefix(dst, major, uint64(l))
43 }
44 return append(dst, s...)
45}
46
47// AppendEmbeddedJSON adds a tag and embeds input JSON as such.
48func AppendEmbeddedJSON(dst, s []byte) []byte {
49 major := majorTypeTags
50 minor := additionalTypeEmbeddedJSON
51
52 // Append the TAG to indicate this is Embedded JSON.
53 dst = append(dst, byte(major|additionalTypeIntUint16))
54 dst = append(dst, byte(minor>>8))
55 dst = append(dst, byte(minor&0xff))
56
57 // Append the JSON Object as Byte String.
58 major = majorTypeByteString
59
60 l := len(s)
61 if l <= additionalMax {
62 lb := byte(l)
63 dst = append(dst, byte(major|lb))
64 } else {
65 dst = appendCborTypePrefix(dst, major, uint64(l))
66 }
67 return append(dst, s...)
68}