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

Function decodeStringComplex

internal/cbor/decode_stream.go:138–195  ·  view source on GitHub ↗
(dst []byte, s string, pos uint)

Source from the content-addressed store, hash-verified

136}
137
138func decodeStringComplex(dst []byte, s string, pos uint) []byte {
139 i := int(pos)
140 start := 0
141
142 for i < len(s) {
143 b := s[i]
144 if b >= utf8.RuneSelf {
145 r, size := utf8.DecodeRuneInString(s[i:])
146 if r == utf8.RuneError && size == 1 {
147 // In case of error, first append previous simple characters to
148 // the byte slice if any and append a replacement character code
149 // in place of the invalid sequence.
150 if start < i {
151 dst = append(dst, s[start:i]...)
152 }
153 dst = append(dst, `\ufffd`...)
154 i += size
155 start = i
156 continue
157 }
158 i += size
159 continue
160 }
161 if b >= 0x20 && b <= 0x7e && b != '\\' && b != '"' {
162 i++
163 continue
164 }
165 // We encountered a character that needs to be encoded.
166 // Let's append the previous simple characters to the byte slice
167 // and switch our operation to read and encode the remainder
168 // characters byte-by-byte.
169 if start < i {
170 dst = append(dst, s[start:i]...)
171 }
172 switch b {
173 case '"', '\\':
174 dst = append(dst, '\\', b)
175 case '\b':
176 dst = append(dst, '\\', 'b')
177 case '\f':
178 dst = append(dst, '\\', 'f')
179 case '\n':
180 dst = append(dst, '\\', 'n')
181 case '\r':
182 dst = append(dst, '\\', 'r')
183 case '\t':
184 dst = append(dst, '\\', 't')
185 default:
186 dst = append(dst, '\\', 'u', '0', '0', hexTable[b>>4], hexTable[b&0xF])
187 }
188 i++
189 start = i
190 }
191 if start < len(s) {
192 dst = append(dst, s[start:]...)
193 }
194 return dst
195}

Callers 1

decodeUTF8StringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected