MCPcopy Index your code
hub / github.com/segmentio/encoding / encodeString

Method encodeString

json/encode.go:134–220  ·  view source on GitHub ↗
(b []byte, p unsafe.Pointer)

Source from the content-addressed store, hash-verified

132}
133
134func (e encoder) encodeString(b []byte, p unsafe.Pointer) ([]byte, error) {
135 s := *(*string)(p)
136 if len(s) == 0 {
137 return append(b, `""`...), nil
138 }
139 i := 0
140 j := 0
141 escapeHTML := (e.flags & EscapeHTML) != 0
142
143 b = append(b, '"')
144
145 if len(s) >= 8 {
146 if j = escapeIndex(s, escapeHTML); j < 0 {
147 return append(append(b, s...), '"'), nil
148 }
149 }
150
151 for j < len(s) {
152 c := s[j]
153
154 if c >= 0x20 && c <= 0x7f && c != '\\' && c != '"' && (!escapeHTML || (c != '<' && c != '>' && c != '&')) {
155 // fast path: most of the time, printable ascii characters are used
156 j++
157 continue
158 }
159
160 switch c {
161 case '\\', '"', '\b', '\f', '\n', '\r', '\t':
162 b = append(b, s[i:j]...)
163 b = append(b, '\\', escapeByteRepr(c))
164 i = j + 1
165 j = j + 1
166 continue
167
168 case '<', '>', '&':
169 b = append(b, s[i:j]...)
170 b = append(b, `\u00`...)
171 b = append(b, hex[c>>4], hex[c&0xF])
172 i = j + 1
173 j = j + 1
174 continue
175 }
176
177 // This encodes bytes < 0x20 except for \t, \n and \r.
178 if c < 0x20 {
179 b = append(b, s[i:j]...)
180 b = append(b, `\u00`...)
181 b = append(b, hex[c>>4], hex[c&0xF])
182 i = j + 1
183 j = j + 1
184 continue
185 }
186
187 r, size := utf8.DecodeRuneInString(s[j:])
188
189 if r == utf8.RuneError && size == 1 {
190 b = append(b, s[i:j]...)
191 b = append(b, `\ufffd`...)

Callers 9

encodeKeyFragmentFunction · 0.95
encodeToStringMethod · 0.95
encodeMapStringStringMethod · 0.95
encodeMapStringBoolMethod · 0.95
encodeTextMarshalerMethod · 0.95
AppendEscapeFunction · 0.95

Calls 2

escapeIndexFunction · 0.85
escapeByteReprFunction · 0.85

Tested by

no test coverage detected