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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! The small JSON scans two hooks share: where a string ends, where a bracket
//! closes, and the value of a top-level key. Brace-matched, string-aware and
//! dependency-free (this crate has no dependencies), and deliberately not a
//! parser: each caller asks one question of one document.
/// Index of the closing quote of the string whose content starts at `from`.
pub(super) fn string_end(bytes: &[u8], from: usize) -> Option<usize> {
let mut escaped = false;
for (i, &c) in bytes.iter().enumerate().skip(from) {
if escaped {
escaped = false;
} else if c == b'\\' {
escaped = true;
} else if c == b'"' {
return Some(i);
}
}
None
}
/// Index of the `shut` matching the `open` at `from`, string-aware.
fn bracket_end(bytes: &[u8], from: usize, open: u8, shut: u8) -> Option<usize> {
let (mut depth, mut i) = (0usize, from);
while i < bytes.len() {
match bytes[i] {
b'"' => i = string_end(bytes, i + 1)?,
c if c == open => depth += 1,
c if c == shut => {
depth = depth.checked_sub(1)?;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
i += 1;
}
None
}
/// The text of the value of the top-level `key` of a JSON document, when that
/// value is an array or an object (brackets included). A depth-tracking scan:
/// a string only counts as a key at depth 1 with a `:` after it, so the same
/// word elsewhere cannot answer.
pub(super) fn top_level_value<'a>(json: &'a str, key: &str) -> Option<&'a str> {
let bytes = json.as_bytes();
let (mut depth, mut i) = (0usize, 0usize);
while i < bytes.len() {
match bytes[i] {
b'"' => {
let start = i + 1;
let close = string_end(bytes, start)?;
let content = &json[start..close];
i = close + 1;
if depth != 1 {
continue;
}
let mut j = i;
while j < bytes.len() && bytes[j].is_ascii_whitespace() {
j += 1;
}
if j >= bytes.len() || bytes[j] != b':' || content != key {
continue;
}
let mut k = j + 1;
while k < bytes.len() && bytes[k].is_ascii_whitespace() {
k += 1;
}
let (open, shut) = match bytes.get(k)? {
b'[' => (b'[', b']'),
b'{' => (b'{', b'}'),
_ => return None,
};
let end = bracket_end(bytes, k, open, shut)?;
return Some(&json[k..=end]);
}
b'{' | b'[' => {
depth += 1;
i += 1;
}
b'}' | b']' => {
depth = depth.saturating_sub(1);
i += 1;
}
_ => i += 1,
}
}
None
}