MCPcopy
hub / github.com/rs/zerolog / decodeUTF8String

Function decodeUTF8String

internal/cbor/decode_stream.go:243–271  ·  view source on GitHub ↗
(src *bufio.Reader)

Source from the content-addressed store, hash-verified

241}
242
243func decodeUTF8String(src *bufio.Reader) []byte {
244 pb := readByte(src)
245 major := pb & maskOutAdditionalType
246 minor := pb & maskOutMajorType
247 if major != majorTypeUtf8String {
248 panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major))
249 }
250 result := []byte{'"'}
251 length := decodeIntAdditionalType(src, minor)
252 len := int(length)
253 pbs := readNBytes(src, len)
254
255 for i := 0; i < len; i++ {
256 // Check if the character needs encoding. Control characters, slashes,
257 // and the double quote need json encoding. Bytes above the ascii
258 // boundary needs utf8 encoding.
259 if pbs[i] < 0x20 || pbs[i] > 0x7e || pbs[i] == '\\' || pbs[i] == '"' {
260 // We encountered a character that needs to be encoded. Switch
261 // to complex version of the algorithm.
262 dst := []byte{'"'}
263 dst = decodeStringComplex(dst, string(pbs), uint(i))
264 return append(dst, '"')
265 }
266 }
267 // The string has no need for encoding and therefore is directly
268 // appended to the byte slice.
269 result = append(result, pbs...)
270 return append(result, '"')
271}
272
273func array2Json(src *bufio.Reader, dst io.Writer) {
274 dst.Write([]byte{'['})

Callers 2

cbor2JsonOneObjectFunction · 0.85
TestDecodeStringFunction · 0.85

Calls 4

readByteFunction · 0.85
decodeIntAdditionalTypeFunction · 0.85
readNBytesFunction · 0.85
decodeStringComplexFunction · 0.85

Tested by 1

TestDecodeStringFunction · 0.68