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

Function strip_code_fence

core/src/llm/structured.rs:336–358  ·  view source on GitHub ↗

Strip ```json ... ``` or ``` ... ``` fences.

(text: &str)

Source from the content-addressed store, hash-verified

334
335/// Strip ```json ... ``` or ``` ... ``` fences.
336fn strip_code_fence(text: &str) -> Option<&str> {
337 let start_patterns = ["```json\n", "```json\r\n", "```\n", "```\r\n"];
338 for pat in &start_patterns {
339 if let Some(rest) = text.strip_prefix(pat) {
340 // Find closing fence
341 if let Some(end) = rest.rfind("```") {
342 return Some(&rest[..end]);
343 }
344 }
345 }
346 // Also handle inline: ```json{...}```
347 if let Some(inner) = text.strip_prefix("```json") {
348 if let Some(end) = inner.rfind("```") {
349 return Some(inner[..end].trim());
350 }
351 }
352 if let Some(inner) = text.strip_prefix("```") {
353 if let Some(end) = inner.rfind("```") {
354 return Some(inner[..end].trim());
355 }
356 }
357 None
358}
359
360/// Find the first balanced `{...}` substring using bracket counting.
361fn find_balanced_json_object(text: &str) -> Option<&str> {

Callers 2

extract_json_valueFunction · 0.85
extract_all_json_valuesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected