| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"fmt" |
| 7 |
"math/bits" |
| 8 |
"reflect" |
| 9 |
"strconv" |
| 10 |
"unicode/utf8" |
| 11 |
"unsafe" |
| 12 |
|
| 13 |
"github.com/goccy/go-json/internal/errors" |
| 14 |
) |
| 15 |
|
| 16 |
type stringDecoder struct { |
| 17 |
structName string |
| 18 |
fieldName string |
| 19 |
// typ is the type of the string, which the type errors report. |
| 20 |
typ reflect.Type |
| 21 |
} |
| 22 |
|
| 23 |
func newStringDecoder(structName, fieldName string) *stringDecoder { |
| 24 |
return &stringDecoder{ |
| 25 |
structName: structName, |
| 26 |
fieldName: fieldName, |
| 27 |
typ: reflect.TypeOf(""), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (d *stringDecoder) errUnmarshalType(typeName string, offset int64) *errors.UnmarshalTypeError { |
| 32 |
return &errors.UnmarshalTypeError{ |
| 33 |
Value: typeName, |
| 34 |
Type: reflect.TypeOf(""), |
| 35 |
Offset: offset, |
| 36 |
Struct: d.structName, |
| 37 |
Field: d.fieldName, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func (d *stringDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 42 |
var ( |
| 43 |
s string |
| 44 |
c int64 |
| 45 |
ok bool |
| 46 |
err error |
| 47 |
) |
| 48 |
buf := ctx.Buf |
| 49 |
if start := skipWhiteSpace(buf, cursor); buf[start] == '"' { |
| 50 |
s, c, ok, err = d.decodeStringField(ctx, start+1) |
| 51 |
} else { |
| 52 |
s, c, ok, err = d.decodeStringValue(ctx, cursor) |
| 53 |
} |
| 54 |
if err != nil { |
| 55 |
return 0, err |
| 56 |
} |
| 57 |
if ok { |
| 58 |
**(**string)(unsafe.Pointer(&p)) = s |
| 59 |
} |
| 60 |
return c, nil |
| 61 |
} |
| 62 |
|
| 63 |
func (d *stringDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 64 |
bytes, c, err := d.decodeByte(ctx.Buf, cursor) |
| 65 |
if err != nil { |
| 66 |
return nil, 0, err |
| 67 |
} |
| 68 |
if bytes == nil { |
| 69 |
return [][]byte{nullbytes}, c, nil |
| 70 |
} |
| 71 |
return [][]byte{bytes}, c, nil |
| 72 |
} |
| 73 |
|
| 74 |
var ( |
| 75 |
hexToInt = [256]int{ |
| 76 |
'0': 0, |
| 77 |
'1': 1, |
| 78 |
'2': 2, |
| 79 |
'3': 3, |
| 80 |
'4': 4, |
| 81 |
'5': 5, |
| 82 |
'6': 6, |
| 83 |
'7': 7, |
| 84 |
'8': 8, |
| 85 |
'9': 9, |
| 86 |
'A': 10, |
| 87 |
'B': 11, |
| 88 |
'C': 12, |
| 89 |
'D': 13, |
| 90 |
'E': 14, |
| 91 |
'F': 15, |
| 92 |
'a': 10, |
| 93 |
'b': 11, |
| 94 |
'c': 12, |
| 95 |
'd': 13, |
| 96 |
'e': 14, |
| 97 |
'f': 15, |
| 98 |
} |
| 99 |
) |
| 100 |
|
| 101 |
var runeErrBytes = []byte(string(utf8.RuneError)) |
| 102 |
|
| 103 |
// stringInfo is what scanString found in a string. |
| 104 |
type stringInfo struct { |
| 105 |
// firstEscape is the offset of the first backslash in the bytes of the string, or -1 if it has no escape: |
| 106 |
// its bytes are the ones of the value then. |
| 107 |
firstEscape int |
| 108 |
// nonASCII is whether a byte of the string is not ASCII: the string may be invalid UTF-8. |
| 109 |
nonASCII bool |
| 110 |
} |
| 111 |
|
| 112 |
// decodeByte returns the bytes of the string at cursor, or nil for null: a part of buf, or a slice of their own if |
| 113 |
// the string has an escape or invalid UTF-8 ( see decodeLiteral ). |
| 114 |
func (d *stringDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) { |
| 115 |
literal, next, info, err := d.scanString(buf, cursor) |
| 116 |
if next < 0 { |
| 117 |
literal, next, info, err = d.scanStringRest(buf, literal, -next-1, info) |
| 118 |
} |
| 119 |
if err != nil || literal == nil { |
| 120 |
return literal, next, err |
| 121 |
} |
| 122 |
return decodeLiteral(literal, info), next, nil |
| 123 |
} |
| 124 |
|
| 125 |
// unescapeLong returns the string of the literal, which has an escape and is too long for the arena: it is decoded |
| 126 |
// into the scratch bytes of the context, and copied, so that the bytes of the string are not zeroed before they are |
| 127 |
// written, and the buffer is not written. |
| 128 |
func (ctx *RuntimeContext) unescapeLong(literal []byte, info stringInfo) string { |
| 129 |
if len(literal) > maxUnescapeScratchSize { |
| 130 |
decoded := decodeLiteral(literal, info) |
| 131 |
return unsafe.String(unsafe.SliceData(decoded), len(decoded)) |
| 132 |
} |
| 133 |
if cap(ctx.unescaped) < len(literal) { |
| 134 |
ctx.unescaped = make([]byte, len(literal)) |
| 135 |
} |
| 136 |
n := unescapeTo(unsafe.Pointer(unsafe.SliceData(ctx.unescaped)), literal, info.firstEscape) |
| 137 |
decoded := ctx.unescaped[:n] |
| 138 |
if info.nonASCII && !utf8.Valid(decoded) { |
| 139 |
return string(coerceUTF8(decoded)) |
| 140 |
} |
| 141 |
return string(decoded) |
| 142 |
} |
| 143 |
|
| 144 |
// decodeLiteral decodes the escapes of the literal and replaces its invalid UTF-8, into a slice of its own if it |
| 145 |
// has any: the buffer is never written, so that it keeps the input, which is checked again for a syntax error |
| 146 |
// ( see Stream.decodeError ). |
| 147 |
func decodeLiteral(literal []byte, info stringInfo) []byte { |
| 148 |
if info.firstEscape >= 0 { |
| 149 |
out := make([]byte, len(literal)) |
| 150 |
literal = out[:unescapeTo(unsafe.Pointer(unsafe.SliceData(out)), literal, info.firstEscape)] |
| 151 |
} |
| 152 |
if info.nonASCII && !utf8.Valid(literal) { |
| 153 |
literal = coerceUTF8(literal) |
| 154 |
} |
| 155 |
return literal |
| 156 |
} |
| 157 |
|
| 158 |
// validLiteral returns the literal of a string without an escape, whose bytes are not all ASCII, with its invalid |
| 159 |
// UTF-8 replaced ( see decodeLiteral ). |
| 160 |
func validLiteral(literal []byte) []byte { |
| 161 |
if !utf8.Valid(literal) { |
| 162 |
return coerceUTF8(literal) |
| 163 |
} |
| 164 |
return literal |
| 165 |
} |
| 166 |
|
| 167 |
// decodeStringValue is decodeString for the value of a string: a value of another kind is a type error, which is |
| 168 |
// recorded, and skipped ( see skipOtherValue ). It is a function of its own, so that the decoding of the other |
| 169 |
// strings, as the keys, is the one it was, and that Decode keeps nothing across its call. |
| 170 |
func (d *stringDecoder) decodeStringValue(ctx *RuntimeContext, cursor int64) (string, int64, bool, error) { |
| 171 |
literal, next, info, err := d.scanString(ctx.Buf, cursor) |
| 172 |
if next < 0 { |
| 173 |
return d.decodeStringRest(ctx, literal, -next-1, info) |
| 174 |
} |
| 175 |
if err != nil { |
| 176 |
return d.skipOtherValue(ctx, err) |
| 177 |
} |
| 178 |
if literal == nil { |
| 179 |
return "", next, false, nil |
| 180 |
} |
| 181 |
if info.firstEscape >= 0 && len(literal) <= maxArenaStringSize { |
| 182 |
// an escaped string is decoded into the arena, out of the buffer, which is never written |
| 183 |
dst := ctx.reserveArena(len(literal)) |
| 184 |
n := unescapeTo(unsafe.Pointer(unsafe.SliceData(dst)), literal, info.firstEscape) |
| 185 |
decoded := dst[:n] |
| 186 |
if info.nonASCII && !utf8.Valid(decoded) { |
| 187 |
return string(coerceUTF8(decoded)), next, true, nil |
| 188 |
} |
| 189 |
ctx.arena = ctx.arena[:len(ctx.arena)+n] |
| 190 |
return unsafe.String(unsafe.SliceData(decoded), n), next, true, nil |
| 191 |
} |
| 192 |
if info.firstEscape >= 0 { |
| 193 |
return ctx.unescapeLong(literal, info), next, true, nil |
| 194 |
} |
| 195 |
if info.nonASCII { |
| 196 |
literal = validLiteral(literal) |
| 197 |
} |
| 198 |
return ctx.makeString(literal), next, true, nil |
| 199 |
} |
| 200 |
|
| 201 |
// decodeStringField is decodeStringValue for the string whose bytes start at start, after its quote, which the decoder |
| 202 |
// of a string field decodes. Its words are looked at for their end, an escape and a control byte, and, until the |
| 203 |
// first one is met, a byte which is not ASCII: the words of a plain ASCII string are looked at for nothing more, and |
| 204 |
// its bytes are the string. An escape is validated where it is met, and a byte which is not ASCII makes the words |
| 205 |
// look past such bytes, so that any string is scanned once. The string is then decoded as decodeStringValue decodes |
| 206 |
// it: a short escaped string into the arena, a long one from where the words stop ( see decodeStringRest ). Where |
| 207 |
// the CPU has a SIMD scan, the words are looked at in the first 64 bytes only, after which a long string is scanned |
| 208 |
// by SIMD. An error in the string is a syntax error of the string. |
| 209 |
func (d *stringDecoder) decodeStringField(ctx *RuntimeContext, start int64) (string, int64, bool, error) { |
| 210 |
buf := ctx.Buf |
| 211 |
b := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 212 |
buflen := int64(len(buf)) |
| 213 |
cursor := start |
| 214 |
wordsEnd := buflen |
| 215 |
if hasStringSIMD { |
| 216 |
wordsEnd = min(buflen, start+64) |
| 217 |
} |
| 218 |
// The words of a plain ASCII string, which most are, are looked at by this loop, which keeps nothing else: the |
| 219 |
// loop below goes on from the byte it stops at, which is not read again. |
| 220 |
for cursor+8 <= wordsEnd { |
| 221 |
w := load64(buf, cursor) |
| 222 |
if stop := keyEndBytes(w) | w&msb; stop != 0 { |
| 223 |
cursor += int64(bits.TrailingZeros64(stop) / 8) |
| 224 |
if char(b, cursor) == '"' { |
| 225 |
return ctx.makeString(buf[start:cursor]), cursor + 1, true, nil |
| 226 |
} |
| 227 |
break |
| 228 |
} |
| 229 |
cursor += 8 |
| 230 |
} |
| 231 |
firstEscape := int64(-1) |
| 232 |
nonASCII := false |
| 233 |
// ascii are the top bits which stop the words at a byte which is not ASCII, until the first one is met. |
| 234 |
ascii := uint64(msb) |
| 235 |
for { |
| 236 |
c := char(b, cursor) |
| 237 |
switch { |
| 238 |
case c == '"': |
| 239 |
literal, next := buf[start:cursor], cursor+1 |
| 240 |
if firstEscape < 0 { |
| 241 |
if nonASCII { |
| 242 |
literal = validLiteral(literal) |
| 243 |
} |
| 244 |
return ctx.makeString(literal), next, true, nil |
| 245 |
} |
| 246 |
if len(literal) <= maxArenaStringSize { |
| 247 |
// an escaped string is decoded into the arena, out of the buffer, which is never written |
| 248 |
dst := ctx.reserveArena(len(literal)) |
| 249 |
n := unescapeTo(unsafe.Pointer(unsafe.SliceData(dst)), literal, int(firstEscape)) |
| 250 |
decoded := dst[:n] |
| 251 |
if nonASCII && !utf8.Valid(decoded) { |
| 252 |
return string(coerceUTF8(decoded)), next, true, nil |
| 253 |
} |
| 254 |
ctx.arena = ctx.arena[:len(ctx.arena)+n] |
| 255 |
return unsafe.String(unsafe.SliceData(decoded), n), next, true, nil |
| 256 |
} |
| 257 |
return ctx.unescapeLong(literal, stringInfo{firstEscape: int(firstEscape), nonASCII: nonASCII}), next, true, nil |
| 258 |
case c == '\\': |
| 259 |
if firstEscape < 0 { |
| 260 |
firstEscape = cursor - start |
| 261 |
} |
| 262 |
cursor++ |
| 263 |
switch char(b, cursor) { |
| 264 |
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': |
| 265 |
cursor++ |
| 266 |
case 'u': |
| 267 |
if cursor+5 >= buflen { |
| 268 |
return "", 0, false, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 269 |
} |
| 270 |
for i := int64(1); i <= 4; i++ { |
| 271 |
c := char(b, cursor+i) |
| 272 |
if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) { |
| 273 |
return "", 0, false, errors.ErrSyntax(fmt.Sprintf("json: invalid character %c in \\u hexadecimal character escape", c), cursor+i) |
| 274 |
} |
| 275 |
} |
| 276 |
cursor += 5 |
| 277 |
default: |
| 278 |
return "", 0, false, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 279 |
} |
| 280 |
case c >= 0x80: |
| 281 |
nonASCII, ascii = true, 0 |
| 282 |
cursor++ |
| 283 |
case c == nul: |
| 284 |
return "", 0, false, errors.ErrUnexpectedEndOfJSON("string", cursor) |
| 285 |
case c < 0x20: |
| 286 |
return "", 0, false, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar(c)), cursor+1) |
| 287 |
default: |
| 288 |
// the words stopped where they may not be read, not at a byte to look at |
| 289 |
if wordsEnd != buflen { |
| 290 |
// the words stopped at wordsEnd, not at a byte to look at: the rest is scanned by SIMD |
| 291 |
return d.decodeStringRest(ctx, buf[start:cursor], cursor, stringInfo{firstEscape: int(firstEscape), nonASCII: nonASCII}) |
| 292 |
} |
| 293 |
cursor++ |
| 294 |
} |
| 295 |
for cursor+8 <= wordsEnd { |
| 296 |
w := load64(buf, cursor) |
| 297 |
if stop := keyEndBytes(w) | w&ascii; stop != 0 { |
| 298 |
cursor += int64(bits.TrailingZeros64(stop) / 8) |
| 299 |
break |
| 300 |
} |
| 301 |
cursor += 8 |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// skipOtherValue returns the error of scanString for a value which is not a string, or, for a value of another |
| 307 |
// kind, which scanString reports by an error at its position, records its type error and skips it. The skipped |
| 308 |
// value is not nested in the ones before it: its depth is counted from 0. |
| 309 |
// |
| 310 |
//go:noinline |
| 311 |
func (d *stringDecoder) skipOtherValue(ctx *RuntimeContext, err error) (string, int64, bool, error) { |
| 312 |
var cursor int64 |
| 313 |
switch e := err.(type) { |
| 314 |
case *errors.UnmarshalTypeError: |
| 315 |
// a number, an array or an object |
| 316 |
cursor = e.Offset |
| 317 |
case *errors.SyntaxError: |
| 318 |
// true and false start no string |
| 319 |
if c := ctx.Buf[e.Offset]; c != 't' && c != 'f' { |
| 320 |
return "", 0, false, err |
| 321 |
} |
| 322 |
cursor = e.Offset |
| 323 |
default: |
| 324 |
return "", 0, false, err |
| 325 |
} |
| 326 |
next, err := ctx.skipTypeError(cursor, 0, d.typ) |
| 327 |
return "", next, false, err |
| 328 |
} |
| 329 |
|
| 330 |
// decodeString returns the string at cursor as a value to store, and false for null. |
| 331 |
// An escaped string is decoded out of the buffer directly. |
| 332 |
func (d *stringDecoder) decodeString(ctx *RuntimeContext, cursor int64) (string, int64, bool, error) { |
| 333 |
literal, next, info, err := d.scanString(ctx.Buf, cursor) |
| 334 |
if next < 0 { |
| 335 |
literal, next, info, err = d.scanStringRest(ctx.Buf, literal, -next-1, info) |
| 336 |
} |
| 337 |
if err != nil || literal == nil { |
| 338 |
return "", next, false, err |
| 339 |
} |
| 340 |
if info.firstEscape >= 0 && len(literal) <= maxArenaStringSize { |
| 341 |
// an escaped string is decoded into the arena, out of the buffer, which is never written |
| 342 |
dst := ctx.reserveArena(len(literal)) |
| 343 |
n := unescapeTo(unsafe.Pointer(unsafe.SliceData(dst)), literal, info.firstEscape) |
| 344 |
decoded := dst[:n] |
| 345 |
if info.nonASCII && !utf8.Valid(decoded) { |
| 346 |
return string(coerceUTF8(decoded)), next, true, nil |
| 347 |
} |
| 348 |
ctx.arena = ctx.arena[:len(ctx.arena)+n] |
| 349 |
return unsafe.String(unsafe.SliceData(decoded), n), next, true, nil |
| 350 |
} |
| 351 |
if info.firstEscape >= 0 { |
| 352 |
return ctx.unescapeLong(literal, info), next, true, nil |
| 353 |
} |
| 354 |
if info.nonASCII { |
| 355 |
literal = validLiteral(literal) |
| 356 |
} |
| 357 |
return ctx.makeString(literal), next, true, nil |
| 358 |
} |
| 359 |
|
| 360 |
// plainStringEnd returns the position of the quote which ends the string whose bytes start at start, if its bytes |
| 361 |
// are all ASCII and none of them is escaped, and -1 for any other string, which the caller decodes by decodeString. |
| 362 |
// Most of the keys and many of the values are such strings: this is their scan alone, eight bytes at a time, with |
| 363 |
// nothing to keep about the bytes it passes. A word is read only where the buffer has room for it, so that a string |
| 364 |
// at the end of the buffer is left to decodeString too. |
| 365 |
func plainStringEnd(buf []byte, start int64) int64 { |
| 366 |
cursor := start |
| 367 |
buflen := int64(len(buf)) |
| 368 |
for cursor+8 <= buflen { |
| 369 |
w := load64(buf, cursor) |
| 370 |
// keyEndBytes has no false bit before its first true one, and none at a byte which is not ASCII, which the top |
| 371 |
// bits add: the first bit is the first byte which ends the plain bytes. |
| 372 |
if stop := keyEndBytes(w) | w&msb; stop != 0 { |
| 373 |
cursor += int64(bits.TrailingZeros64(stop) / 8) |
| 374 |
if buf[cursor] == '"' { |
| 375 |
return cursor |
| 376 |
} |
| 377 |
return -1 |
| 378 |
} |
| 379 |
cursor += 8 |
| 380 |
} |
| 381 |
return -1 |
| 382 |
} |
| 383 |
|
| 384 |
// scanString finds the string at cursor, and returns its bytes as they are in buf, the position after it and |
| 385 |
// what it found in it, or nil for null. |
| 386 |
// |
| 387 |
// Most strings are short, and scanned by its loop, which calls nothing so that it keeps its values in the |
| 388 |
// registers. After 64 bytes of plain bytes, it stops and returns the bytes of the string so far, -1 minus the |
| 389 |
// position it stopped at, and what it found so far: the caller continues the scan by scanStringRest, which scans |
| 390 |
// the runs of plain bytes of a long string by SIMD where the CPU has it. The position is negative so that the |
| 391 |
// result has no field more, whose return every string would pay for. |
| 392 |
func (d *stringDecoder) scanString(buf []byte, cursor int64) ([]byte, int64, stringInfo, error) { |
| 393 |
for { |
| 394 |
switch buf[cursor] { |
| 395 |
case ' ', '\n', '\t', '\r': |
| 396 |
cursor++ |
| 397 |
case '[': |
| 398 |
return nil, 0, stringInfo{}, d.errUnmarshalType("array", cursor) |
| 399 |
case '{': |
| 400 |
return nil, 0, stringInfo{}, d.errUnmarshalType("object", cursor) |
| 401 |
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 402 |
return nil, 0, stringInfo{}, d.errUnmarshalType("number", cursor) |
| 403 |
case '"': |
| 404 |
cursor++ |
| 405 |
start := cursor |
| 406 |
b := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 407 |
buflen := int64(len(buf)) |
| 408 |
firstEscape := int64(-1) |
| 409 |
// high accumulates the bytes of the string: it is not zero if one of them is not ASCII. |
| 410 |
var high uint64 |
| 411 |
// The words are read up to wordsEnd: the end of the buffer, or, where the CPU has a SIMD scan, 64 |
| 412 |
// bytes after the start, after which the rest of a long string is left to scanStringRest ( see |
| 413 |
// scanString ). It is looked at once for a string, not for every word. |
| 414 |
wordsEnd := buflen |
| 415 |
if hasStringSIMD { |
| 416 |
wordsEnd = min(buflen, start+64) |
| 417 |
} |
| 418 |
for { |
| 419 |
// The words with nothing to look at are skipped eight bytes at a time: a word is read only where |
| 420 |
// the buffer has room for it, so that its end is scanned byte by byte. |
| 421 |
for cursor+8 <= wordsEnd { |
| 422 |
w := load64(buf, cursor) |
| 423 |
if special := keyEndBytes(w); special != 0 { |
| 424 |
i := int64(bits.TrailingZeros64(special) / 8) |
| 425 |
high |= w & msb & (1<<(uint(i)*8&63) - 1) |
| 426 |
cursor += i |
| 427 |
break |
| 428 |
} |
| 429 |
high |= w & msb |
| 430 |
cursor += 8 |
| 431 |
} |
| 432 |
c := char(b, cursor) |
| 433 |
switch c { |
| 434 |
case '\\': |
| 435 |
if firstEscape < 0 { |
| 436 |
firstEscape = cursor - start |
| 437 |
} |
| 438 |
cursor++ |
| 439 |
switch char(b, cursor) { |
| 440 |
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': |
| 441 |
cursor++ |
| 442 |
case 'u': |
| 443 |
if cursor+5 >= buflen { |
| 444 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 445 |
} |
| 446 |
for i := int64(1); i <= 4; i++ { |
| 447 |
c := char(b, cursor+i) |
| 448 |
if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) { |
| 449 |
return nil, 0, stringInfo{}, errors.ErrSyntax(fmt.Sprintf("json: invalid character %c in \\u hexadecimal character escape", c), cursor+i) |
| 450 |
} |
| 451 |
} |
| 452 |
cursor += 5 |
| 453 |
default: |
| 454 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 455 |
} |
| 456 |
case '"': |
| 457 |
return buf[start:cursor], cursor + 1, stringInfo{firstEscape: int(firstEscape), nonASCII: high&msb != 0}, nil |
| 458 |
case nul: |
| 459 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("string", cursor) |
| 460 |
default: |
| 461 |
if c < 0x20 { |
| 462 |
return nil, 0, stringInfo{}, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar(c)), cursor+1) |
| 463 |
} |
| 464 |
if wordsEnd != buflen { |
| 465 |
// the words stopped at wordsEnd, not at a byte to look at |
| 466 |
return buf[start:cursor], -cursor - 1, stringInfo{firstEscape: int(firstEscape), nonASCII: high&msb != 0}, nil |
| 467 |
} |
| 468 |
high |= uint64(c) |
| 469 |
cursor++ |
| 470 |
} |
| 471 |
} |
| 472 |
case 'n': |
| 473 |
if err := validateNull(buf, cursor); err != nil { |
| 474 |
return nil, 0, stringInfo{}, err |
| 475 |
} |
| 476 |
cursor += 4 |
| 477 |
return nil, cursor, stringInfo{}, nil |
| 478 |
default: |
| 479 |
return nil, 0, stringInfo{}, errors.ErrInvalidBeginningOfValue(buf[cursor], cursor) |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
// scanStringRest continues the scan of a long string from where scanString stopped, which returned the bytes |
| 485 |
// of the string so far, the position it stopped at and what it found so far: the runs of plain bytes are scanned |
| 486 |
// by SIMD ( see indexStringSpecial ), and a run near the end of the buffer by words. The escapes are validated as |
| 487 |
// scanString does. |
| 488 |
func (d *stringDecoder) scanStringRest(buf, literal []byte, cursor int64, info stringInfo) ([]byte, int64, stringInfo, error) { |
| 489 |
start := cursor - int64(len(literal)) |
| 490 |
firstEscape := int64(info.firstEscape) |
| 491 |
var high uint64 |
| 492 |
if info.nonASCII { |
| 493 |
high = msb |
| 494 |
} |
| 495 |
b := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 496 |
buflen := int64(len(buf)) |
| 497 |
for { |
| 498 |
if i, h, ok := indexStringSpecial(unsafe.Add(b, cursor), int(buflen-cursor)); ok { |
| 499 |
high |= h |
| 500 |
cursor += int64(i) |
| 501 |
} else { |
| 502 |
for cursor+8 <= buflen { |
| 503 |
w := load64(buf, cursor) |
| 504 |
if special := keyEndBytes(w); special != 0 { |
| 505 |
i := int64(bits.TrailingZeros64(special) / 8) |
| 506 |
high |= w & msb & (1<<(uint(i)*8&63) - 1) |
| 507 |
cursor += i |
| 508 |
break |
| 509 |
} |
| 510 |
high |= w & msb |
| 511 |
cursor += 8 |
| 512 |
} |
| 513 |
} |
| 514 |
c := char(b, cursor) |
| 515 |
switch c { |
| 516 |
case '\\': |
| 517 |
if firstEscape < 0 { |
| 518 |
firstEscape = cursor - start |
| 519 |
} |
| 520 |
cursor++ |
| 521 |
switch char(b, cursor) { |
| 522 |
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': |
| 523 |
cursor++ |
| 524 |
case 'u': |
| 525 |
if cursor+5 >= buflen { |
| 526 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 527 |
} |
| 528 |
for i := int64(1); i <= 4; i++ { |
| 529 |
c := char(b, cursor+i) |
| 530 |
if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) { |
| 531 |
return nil, 0, stringInfo{}, errors.ErrSyntax(fmt.Sprintf("json: invalid character %c in \\u hexadecimal character escape", c), cursor+i) |
| 532 |
} |
| 533 |
} |
| 534 |
cursor += 5 |
| 535 |
default: |
| 536 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("escaped string", cursor) |
| 537 |
} |
| 538 |
case '"': |
| 539 |
return buf[start:cursor], cursor + 1, stringInfo{firstEscape: int(firstEscape), nonASCII: high&msb != 0}, nil |
| 540 |
case nul: |
| 541 |
return nil, 0, stringInfo{}, errors.ErrUnexpectedEndOfJSON("string", cursor) |
| 542 |
default: |
| 543 |
if c < 0x20 { |
| 544 |
return nil, 0, stringInfo{}, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar(c)), cursor+1) |
| 545 |
} |
| 546 |
high |= uint64(c) |
| 547 |
cursor++ |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
var unescapeMap = [256]byte{ |
| 553 |
'"': '"', |
| 554 |
'\\': '\\', |
| 555 |
'/': '/', |
| 556 |
'b': '\b', |
| 557 |
'f': '\f', |
| 558 |
'n': '\n', |
| 559 |
'r': '\r', |
| 560 |
't': '\t', |
| 561 |
} |
| 562 |
|
| 563 |
func unsafeAdd(ptr unsafe.Pointer, offset int) unsafe.Pointer { |
| 564 |
return unsafe.Add(ptr, offset) |
| 565 |
} |
| 566 |
|
| 567 |
// copyToBackslash copies the bytes from src up to the first backslash of the n bytes, or all of them, to dst, and |
| 568 |
// returns how many it copied: bytes.IndexByte and copy go by vectors. It is not inlined, so that unescapeTo keeps |
| 569 |
// its registers for the short runs, which most are. |
| 570 |
// |
| 571 |
//go:noinline |
| 572 |
func copyToBackslash(dst, src unsafe.Pointer, n int) int { |
| 573 |
rest := unsafe.Slice((*byte)(src), n) |
| 574 |
if i := bytes.IndexByte(rest, '\\'); i >= 0 { |
| 575 |
n = i |
| 576 |
} |
| 577 |
copy(unsafe.Slice((*byte)(dst), n), rest[:n]) |
| 578 |
return n |
| 579 |
} |
| 580 |
|
| 581 |
// unescapeTo decodes the escapes of the bytes of an escaped string into out, which has room for as many bytes and |
| 582 |
// is not the bytes themselves, and returns the length of the result, which is at most the length of the bytes. |
| 583 |
// |
| 584 |
// first is the offset of the first backslash, which scanString found. |
| 585 |
func unescapeTo(out unsafe.Pointer, buf []byte, first int) int { |
| 586 |
p := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 587 |
end := unsafeAdd(p, len(buf)) |
| 588 |
// the bytes before the first escape, by words: a short string is not worth a call of memmove. |
| 589 |
i := 0 |
| 590 |
for ; i+8 <= first; i += 8 { |
| 591 |
*(*uint64)(unsafeAdd(out, i)) = *(*uint64)(unsafeAdd(p, i)) |
| 592 |
} |
| 593 |
for ; i < first; i++ { |
| 594 |
*(*byte)(unsafeAdd(out, i)) = *(*byte)(unsafeAdd(p, i)) |
| 595 |
} |
| 596 |
src := unsafeAdd(p, first) |
| 597 |
dst := unsafeAdd(out, first) |
| 598 |
for src != end { |
| 599 |
// The bytes up to the next backslash are copied eight at a time, from the words which are all in the |
| 600 |
// string. A word is written whole, and the output goes on to its backslash: out has the length of buf, and |
| 601 |
// is never ahead of the bytes read, so that a word written at dst ends before out does. A run longer than |
| 602 |
// two words is looked through by bytes.IndexByte and moved by copy, which go by vectors. |
| 603 |
for words := 0; *(*byte)(src) != '\\' && uintptr(src)+8 <= uintptr(end); words++ { |
| 604 |
if words == 2 { |
| 605 |
n := copyToBackslash(dst, src, int(uintptr(end)-uintptr(src))) |
| 606 |
src = unsafeAdd(src, n) |
| 607 |
dst = unsafeAdd(dst, n) |
| 608 |
break |
| 609 |
} |
| 610 |
w := binary.LittleEndian.Uint64((*[8]byte)(src)[:]) |
| 611 |
binary.LittleEndian.PutUint64((*[8]byte)(dst)[:], w) |
| 612 |
if backslash := firstByteMask(w, '\\'); backslash != 0 { |
| 613 |
n := bits.TrailingZeros64(backslash) / 8 |
| 614 |
src = unsafeAdd(src, n) |
| 615 |
dst = unsafeAdd(dst, n) |
| 616 |
break |
| 617 |
} |
| 618 |
src = unsafeAdd(src, 8) |
| 619 |
dst = unsafeAdd(dst, 8) |
| 620 |
} |
| 621 |
if src == end { |
| 622 |
break |
| 623 |
} |
| 624 |
c := char(src, 0) |
| 625 |
if c == '\\' { |
| 626 |
escapeChar := char(src, 1) |
| 627 |
if escapeChar != 'u' { |
| 628 |
*(*byte)(dst) = unescapeMap[escapeChar] |
| 629 |
src = unsafeAdd(src, 2) |
| 630 |
dst = unsafeAdd(dst, 1) |
| 631 |
} else { |
| 632 |
v1 := hexToInt[char(src, 2)] |
| 633 |
v2 := hexToInt[char(src, 3)] |
| 634 |
v3 := hexToInt[char(src, 4)] |
| 635 |
v4 := hexToInt[char(src, 5)] |
| 636 |
code := rune((v1 << 12) | (v2 << 8) | (v3 << 4) | v4) |
| 637 |
if code >= 0xd800 && code < 0xdc00 && uintptr(unsafeAdd(src, 11)) < uintptr(end) { |
| 638 |
if char(src, 6) == '\\' && char(src, 7) == 'u' { |
| 639 |
v1 := hexToInt[char(src, 8)] |
| 640 |
v2 := hexToInt[char(src, 9)] |
| 641 |
v3 := hexToInt[char(src, 10)] |
| 642 |
v4 := hexToInt[char(src, 11)] |
| 643 |
lo := rune((v1 << 12) | (v2 << 8) | (v3 << 4) | v4) |
| 644 |
if lo >= 0xdc00 && lo < 0xe000 { |
| 645 |
code = (code-0xd800)<<10 | (lo - 0xdc00) + 0x10000 |
| 646 |
src = unsafeAdd(src, 6) |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
var b [utf8.UTFMax]byte |
| 651 |
n := utf8.EncodeRune(b[:], code) |
| 652 |
switch n { |
| 653 |
case 4: |
| 654 |
*(*byte)(unsafeAdd(dst, 3)) = b[3] |
| 655 |
fallthrough |
| 656 |
case 3: |
| 657 |
*(*byte)(unsafeAdd(dst, 2)) = b[2] |
| 658 |
fallthrough |
| 659 |
case 2: |
| 660 |
*(*byte)(unsafeAdd(dst, 1)) = b[1] |
| 661 |
fallthrough |
| 662 |
case 1: |
| 663 |
*(*byte)(unsafeAdd(dst, 0)) = b[0] |
| 664 |
} |
| 665 |
src = unsafeAdd(src, 6) |
| 666 |
dst = unsafeAdd(dst, n) |
| 667 |
} |
| 668 |
} else { |
| 669 |
*(*byte)(dst) = c |
| 670 |
src = unsafeAdd(src, 1) |
| 671 |
dst = unsafeAdd(dst, 1) |
| 672 |
} |
| 673 |
} |
| 674 |
return int(uintptr(dst) - uintptr(out)) |
| 675 |
} |
| 676 |
|
| 677 |
// firstByteMask returns the word which has the top bit of the first byte of w which is c, if it has one, and maybe |
| 678 |
// the top bits of bytes after it: a subtraction borrows from the next byte only at a byte which is c. |
| 679 |
func firstByteMask(w, c uint64) uint64 { |
| 680 |
x := w ^ (c * lsb) |
| 681 |
return (x - lsb) &^ x & msb |
| 682 |
} |
| 683 |
|
| 684 |
// coerceUTF8 returns a copy of the literal in which every byte of an invalid UTF-8 sequence |
| 685 |
// is replaced by utf8.RuneError, as encoding/json does. |
| 686 |
func coerceUTF8(literal []byte) []byte { |
| 687 |
out := make([]byte, 0, len(literal)+2*utf8.UTFMax) |
| 688 |
for i := 0; i < len(literal); { |
| 689 |
r, size := utf8.DecodeRune(literal[i:]) |
| 690 |
if r == utf8.RuneError && size == 1 { |
| 691 |
out = append(out, runeErrBytes...) |
| 692 |
} else { |
| 693 |
out = append(out, literal[i:i+size]...) |
| 694 |
} |
| 695 |
i += size |
| 696 |
} |
| 697 |
return out |
| 698 |
} |
| 699 |
|
| 700 |
// quoteChar formats c as a quoted character literal, as encoding/json does in its errors. |
| 701 |
func quoteChar(c byte) string { |
| 702 |
if c == '\'' { |
| 703 |
return `'\''` |
| 704 |
} |
| 705 |
if c == '"' { |
| 706 |
return `'"'` |
| 707 |
} |
| 708 |
s := strconv.Quote(string(c)) |
| 709 |
return "'" + s[1:len(s)-1] + "'" |
| 710 |
} |