1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
pub(super) fn find_balanced_json_span(content: &str) -> Option<&str> {
let mut start_idx: Option<usize> = None;
let mut depth = 0usize;
let mut in_string = false;
let mut escaped = false;
for (idx, ch) in content.char_indices() {
if in_string {
if escaped {
escaped = false;
continue;
}
match ch {
'\\' => {
escaped = true;
}
'"' => {
in_string = false;
}
_ => {}
}
continue;
}
match ch {
'"' => {
in_string = true;
}
'{' => {
if depth == 0 {
start_idx = Some(idx);
}
depth += 1;
}
'}' => {
if depth == 0 {
continue;
}
depth -= 1;
if depth == 0
&& let Some(start) = start_idx
{
let end = idx + ch.len_utf8();
return content.get(start..end);
}
}
_ => {}
}
}
None
}