Find the byte offset where JSON content starts in a text stream. Skips leading prose/whitespace to find `{` or `[` that isn't inside a string.
(text: &str)
| 449 | /// Find the byte offset where JSON content starts in a text stream. |
| 450 | /// Skips leading prose/whitespace to find `{` or `[` that isn't inside a string. |
| 451 | fn find_json_start(text: &str) -> Option<usize> { |
| 452 | // Skip past code fence markers if present |
| 453 | let (search_text, offset) = if let Some(rest) = text.strip_prefix("```json") { |
| 454 | (rest, 7) |
| 455 | } else if let Some(rest) = text.strip_prefix("```") { |
| 456 | (rest, 3) |
| 457 | } else { |
| 458 | (text, 0) |
| 459 | }; |
| 460 | |
| 461 | let mut in_string = false; |
| 462 | let mut escape_next = false; |
| 463 | for (i, &b) in search_text.as_bytes().iter().enumerate() { |
| 464 | if escape_next { |
| 465 | escape_next = false; |
| 466 | continue; |
| 467 | } |
| 468 | match b { |
| 469 | b'\\' if in_string => { |
| 470 | escape_next = true; |
| 471 | } |
| 472 | b'"' => { |
| 473 | in_string = !in_string; |
| 474 | } |
| 475 | b'{' | b'[' if !in_string => { |
| 476 | return Some(offset + i); |
| 477 | } |
| 478 | _ => {} |
| 479 | } |
| 480 | } |
| 481 | None |
| 482 | } |
| 483 | |
| 484 | // --------------------------------------------------------------------------- |
| 485 | // Partial JSON parsing (for streaming) |
no outgoing calls
no test coverage detected