MCPcopy Create free account
hub / github.com/AI45Lab/Code / find_json_start

Function find_json_start

core/src/llm/structured.rs:451–482  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
451fn 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)

Callers 1

generate_streamingFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected