SYS-REQ-045 Tries to find the end of string Support if string contains escaped quote symbols.
(data []byte)
| 278 | // Tries to find the end of string |
| 279 | // Support if string contains escaped quote symbols. |
| 280 | func stringEnd(data []byte) (int, bool) { |
| 281 | // fast path: SIMD-scan for the first '"' or '\'. If no '\' precedes the |
| 282 | // first '"' (the overwhelmingly common case for JSON string values), |
| 283 | // the quote is unescaped and we return directly — skipping the per-byte |
| 284 | // escape-tracking loop. Bound: bytes.IndexByte finds the first match in |
| 285 | // either direction, so firstBackslash > firstQuote (or == -1) is exactly |
| 286 | // the condition "no backslash precedes the closing quote", which is |
| 287 | // equivalent to the slow loop's `escaped == false` state at the quote. |
| 288 | firstQuote := bytes.IndexByte(data, '"') |
| 289 | if firstQuote == -1 { |
| 290 | // Slow path's tail semantics: return -1 with escaped flag true iff |
| 291 | // at least one '\' was encountered before end-of-input. |
| 292 | return -1, bytes.IndexByte(data, '\\') != -1 |
| 293 | } |
| 294 | firstBackslash := bytes.IndexByte(data, '\\') |
| 295 | if firstBackslash == -1 || firstBackslash > firstQuote { |
| 296 | return firstQuote + 1, false |
| 297 | } |
| 298 | // Slow path: at least one '\' precedes the first '"' — the per-byte |
| 299 | // walker is needed to disambiguate escaped vs. unescaped quotes. |
| 300 | escaped := false |
| 301 | for i, c := range data { |
| 302 | if c == '"' { |
| 303 | if !escaped { |
| 304 | return i + 1, false |
| 305 | } else { |
| 306 | j := i - 1 |
| 307 | for { |
| 308 | if j < 0 || data[j] != '\\' { |
| 309 | return i + 1, true // even number of backslashes |
| 310 | } |
| 311 | j-- |
| 312 | if j < 0 || data[j] != '\\' { |
| 313 | break // odd number of backslashes |
| 314 | } |
| 315 | j-- |
| 316 | |
| 317 | } |
| 318 | } |
| 319 | } else if c == '\\' { |
| 320 | escaped = true |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return -1, escaped |
| 325 | } |
| 326 | |
| 327 | // SYS-REQ-046 |
| 328 | // Find end of the data structure, array or object. |
no outgoing calls