Debian Codegraph @

internal/decoder/string_unescape.go

raw
1 package decoder
2
3 import (
4 "fmt"
5 "math/bits"
6 "unicode/utf8"
7 "unsafe"
8
9 "github.com/goccy/go-json/internal/errors"
10 )
11
12 // decodeStringRest is decodeStringValue for a long string, which scanString stopped in: it scans the rest of the
13 // string as scanStringRest does, and, at an escape, leaves the rest to unescapeRest, which decodes the string in
14 // the same pass. The loop of a string without an escape keeps nothing more than the one of scanStringRest.
15 //
16 //go:noinline
17 func (d *stringDecoder) decodeStringRest(ctx *RuntimeContext, literal []byte, cursor int64, info stringInfo) (string, int64, bool, error) {
18 if info.firstEscape >= 0 {
19 return d.unescapeRest(ctx, literal, cursor, info)
20 }
21 buf := ctx.Buf
22 start := cursor - int64(len(literal))
23 var high uint64
24 if info.nonASCII {
25 high = msb
26 }
27 b := (*sliceHeader)(unsafe.Pointer(&buf)).data
28 buflen := int64(len(buf))
29 for {
30 if i, h, ok := indexStringSpecial(unsafe.Add(b, cursor), int(buflen-cursor)); ok {
31 high |= h
32 cursor += int64(i)
33 } else {
34 for cursor+8 <= buflen {
35 w := load64(buf, cursor)
36 if special := keyEndBytes(w); special != 0 {
37 i := int64(bits.TrailingZeros64(special) / 8)
38 high |= w & msb & (1<<(uint(i)*8&63) - 1)
39 cursor += i
40 break
41 }
42 high |= w & msb
43 cursor += 8
44 }
45 }
46 c := char(b, cursor)
47 switch c {
48 case '\\':
49 return d.unescapeRest(ctx, buf[start:cursor], cursor, stringInfo{firstEscape: -1, nonASCII: high&msb != 0})
50 case '"':
51 literal := buf[start:cursor]
52 if high&msb != 0 {
53 literal = validLiteral(literal)
54 }
55 return ctx.makeString(literal), cursor + 1, true, nil
56 case nul:
57 return d.skipOtherValue(ctx, errors.ErrUnexpectedEndOfJSON("string", cursor))
58 default:
59 if c < 0x20 {
60 return d.skipOtherValue(ctx, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar(c)), cursor+1))
61 }
62 high |= uint64(c)
63 cursor++
64 }
65 }
66 }
67
68 // unescapeRest continues decodeStringRest from the bytes of the string so far, literal, and the position after
69 // them, cursor, which is at an escape or after the first 64 bytes of a string with an escape: it scans the rest
70 // of the string and decodes it in the same pass, into the scratch bytes of the context. A run of plain bytes is
71 // scanned by SIMD, and moved at the byte which ends it.
72 //
73 //go:noinline
74 func (d *stringDecoder) unescapeRest(ctx *RuntimeContext, literal []byte, cursor int64, info stringInfo) (string, int64, bool, error) {
75 buf := ctx.Buf
76 start := cursor - int64(len(literal))
77 var high uint64
78 if info.nonASCII {
79 high = msb
80 }
81 escaped := info.firstEscape >= 0
82 out := ctx.unescaped[:0]
83 if escaped {
84 out = growBytes(out, len(literal)+64)
85 out = out[:unescapeTo(unsafe.Pointer(unsafe.SliceData(out)), literal, info.firstEscape)]
86 }
87 // run is the position of the plain bytes which are not in out yet, once the string has an escape.
88 run := cursor
89 b := (*sliceHeader)(unsafe.Pointer(&buf)).data
90 buflen := int64(len(buf))
91 for {
92 if i, h, ok := indexStringSpecial(unsafe.Add(b, cursor), int(buflen-cursor)); ok {
93 high |= h
94 cursor += int64(i)
95 } else {
96 for cursor+8 <= buflen {
97 w := load64(buf, cursor)
98 if special := keyEndBytes(w); special != 0 {
99 i := int64(bits.TrailingZeros64(special) / 8)
100 high |= w & msb & (1<<(uint(i)*8&63) - 1)
101 cursor += i
102 break
103 }
104 high |= w & msb
105 cursor += 8
106 }
107 }
108 c := char(b, cursor)
109 switch c {
110 case '\\':
111 if escaped {
112 out = append(out, buf[run:cursor]...)
113 } else {
114 escaped = true
115 out = append(growBytes(out, int(cursor-start)+64), buf[start:cursor]...)
116 }
117 cursor++
118 switch e := char(b, cursor); e {
119 case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
120 out = append(out, unescapeMap[e])
121 cursor++
122 case 'u':
123 code, ok := hex4(b, cursor+1, buflen)
124 if !ok {
125 return d.skipOtherValue(ctx, d.hexError(buf, cursor))
126 }
127 cursor += 5
128 if code >= 0xd800 && code < 0xdc00 && char(b, cursor) == '\\' && cursor+1 < buflen && char(b, cursor+1) == 'u' {
129 if lo, ok := hex4(b, cursor+2, buflen); ok && lo >= 0xdc00 && lo < 0xe000 {
130 code = (code-0xd800)<<10 | (lo - 0xdc00) + 0x10000
131 cursor += 6
132 }
133 }
134 out = utf8.AppendRune(out, code)
135 default:
136 return d.skipOtherValue(ctx, errors.ErrUnexpectedEndOfJSON("escaped string", cursor))
137 }
138 run = cursor
139 case '"':
140 next := cursor + 1
141 if !escaped {
142 literal := buf[start:cursor]
143 if high&msb != 0 {
144 literal = validLiteral(literal)
145 }
146 return ctx.makeString(literal), next, true, nil
147 }
148 out = append(out, buf[run:cursor]...)
149 if cap(out) <= maxUnescapeScratchSize {
150 ctx.unescaped = out[:0]
151 }
152 if high&msb != 0 && !utf8.Valid(out) {
153 return string(coerceUTF8(out)), next, true, nil
154 }
155 return ctx.copyString(out), next, true, nil
156 case nul:
157 return d.skipOtherValue(ctx, errors.ErrUnexpectedEndOfJSON("string", cursor))
158 default:
159 if c < 0x20 {
160 return d.skipOtherValue(ctx, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar(c)), cursor+1))
161 }
162 high |= uint64(c)
163 cursor++
164 }
165 }
166 }
167
168 // growBytes returns b with room for n bytes more.
169 func growBytes(b []byte, n int) []byte {
170 if cap(b)-len(b) < n {
171 b = append(b[:cap(b)], make([]byte, n)...)[:len(b)]
172 }
173 return b
174 }
175
176 // hex4 returns the code of the four hexadecimal digits at pos, and false if they are not four of them.
177 func hex4(b unsafe.Pointer, pos, buflen int64) (rune, bool) {
178 if pos+4 >= buflen {
179 return 0, false
180 }
181 var code rune
182 for i := int64(0); i < 4; i++ {
183 c := char(b, pos+i)
184 if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
185 return 0, false
186 }
187 code = code<<4 | rune(hexToInt[c])
188 }
189 return code, true
190 }
191
192 // hexError returns the error of the \u escape at pos, whose digits are not four hexadecimal ones, as scanString does.
193 func (d *stringDecoder) hexError(buf []byte, pos int64) error {
194 if pos+5 >= int64(len(buf)) {
195 return errors.ErrUnexpectedEndOfJSON("escaped string", pos)
196 }
197 for i := int64(1); i <= 4; i++ {
198 c := buf[pos+i]
199 if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
200 return errors.ErrSyntax(fmt.Sprintf("json: invalid character %c in \\u hexadecimal character escape", c), pos+i)
201 }
202 }
203 return errors.ErrUnexpectedEndOfJSON("escaped string", pos)
204 }