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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
//! Hand-written port of the recursive JSON grammar that `JsonManipulator` expresses in PHP via a
//! PCRE `(?(DEFINE) ... )` block with `(?&json)` subroutine calls. The `regex` crate cannot express
//! recursive subpatterns, so — per docs/dev/regex-porting.md — the grammar is decomposed here into
//! plain byte scanners. Each `scan_*` returns the byte offset just past the construct, or `None`
//! when the bytes do not match, mirroring how the PCRE grammar would fail to match un-regexable
//! content.
//!
//! The grammar being ported (from JsonManipulator::DEFINES):
//!
//! ```text
//! number -? (?= [1-9]|0(?!\d) ) \d++ (?:\.\d++)? (?:[eE] [+-]?+ \d++)?
//! boolean true | false | null
//! string " (?:[^"\\]*+ | \\["\\bfnrt/] | \\u[0-9A-Fa-f]{4})* "
//! array \[ (?: (?&json) \s*+ (?: , (?&json) \s*+ )*+ )?+ \s*+ \]
//! pair \s*+ (?&string) \s*+ : (?&json) \s*+
//! object \{ (?: (?&pair) (?: , (?&pair) )*+ )?+ \s*+ \}
//! json \s*+ (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) )
//! ```
/// PCRE `\s`: space, tab, newline, carriage return, form feed, vertical tab.
fn is_ws(b: u8) -> bool {
matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0c | 0x0b)
}
pub fn skip_ws(b: &[u8], mut p: usize) -> usize {
while p < b.len() && is_ws(b[p]) {
p += 1;
}
p
}
/// `string`: a JSON string starting at `b[p] == '"'`. Returns the offset past the closing quote.
pub fn scan_string(b: &[u8], p: usize) -> Option<usize> {
if b.get(p) != Some(&b'"') {
return None;
}
let mut i = p + 1;
loop {
match b.get(i)? {
b'"' => return Some(i + 1),
b'\\' => match b.get(i + 1)? {
b'"' | b'\\' | b'b' | b'f' | b'n' | b'r' | b't' | b'/' => i += 2,
b'u' => {
for k in 0..4 {
if !b.get(i + 2 + k)?.is_ascii_hexdigit() {
return None;
}
}
i += 6;
}
_ => return None,
},
_ => i += 1,
}
}
}
/// `number`. Returns the offset past the number, or `None` (e.g. leading zeros are rejected).
pub fn scan_number(b: &[u8], p: usize) -> Option<usize> {
let mut i = p;
if b.get(i) == Some(&b'-') {
i += 1;
}
// (?= [1-9] | 0(?!\d) )
match b.get(i)? {
b'1'..=b'9' => {}
b'0' => {
if b.get(i + 1).is_some_and(|d| d.is_ascii_digit()) {
return None;
}
}
_ => return None,
}
let start = i;
while b.get(i).is_some_and(|c| c.is_ascii_digit()) {
i += 1;
}
debug_assert!(i > start);
// (?: \. \d++ )? — optional, so only consume the dot when at least one digit follows.
if b.get(i) == Some(&b'.') && b.get(i + 1).is_some_and(|c| c.is_ascii_digit()) {
let mut j = i + 1;
while b.get(j).is_some_and(|c| c.is_ascii_digit()) {
j += 1;
}
i = j;
}
// (?: [eE] [+-]?+ \d++ )? — optional; only consume when a complete exponent follows.
if matches!(b.get(i), Some(b'e' | b'E')) {
let mut j = i + 1;
if matches!(b.get(j), Some(b'+' | b'-')) {
j += 1;
}
let exp = j;
while b.get(j).is_some_and(|c| c.is_ascii_digit()) {
j += 1;
}
if j > exp {
i = j;
}
}
Some(i)
}
fn scan_literal(b: &[u8], p: usize, word: &[u8]) -> Option<usize> {
if b[p..].starts_with(word) {
Some(p + word.len())
} else {
None
}
}
/// `json`: optional leading whitespace then a single JSON value. Returns the offset past the value
/// (trailing whitespace is left for the caller, matching the grammar where `json` ends at the value).
pub fn scan_value(b: &[u8], p: usize) -> Option<usize> {
let i = skip_ws(b, p);
match b.get(i)? {
b'"' => scan_string(b, i),
b'{' => scan_object(b, i),
b'[' => scan_array(b, i),
b't' => scan_literal(b, i, b"true"),
b'f' => scan_literal(b, i, b"false"),
b'n' => scan_literal(b, i, b"null"),
b'-' | b'0'..=b'9' => scan_number(b, i),
_ => None,
}
}
/// `array` starting at `b[p] == '['`. Returns the offset past the closing `]`.
pub fn scan_array(b: &[u8], p: usize) -> Option<usize> {
let mut i = p + 1;
let probe = skip_ws(b, i);
if b.get(probe) == Some(&b']') {
return Some(probe + 1);
}
i = scan_value(b, i)?;
i = skip_ws(b, i);
while b.get(i) == Some(&b',') {
i = scan_value(b, i + 1)?;
i = skip_ws(b, i);
}
if b.get(i) == Some(&b']') {
Some(i + 1)
} else {
None
}
}
/// `pair`: `\s* "key" \s* : json \s*`. Returns the offset past the trailing whitespace.
pub fn scan_pair(b: &[u8], p: usize) -> Option<usize> {
let mut i = skip_ws(b, p);
i = scan_string(b, i)?;
i = skip_ws(b, i);
if b.get(i) != Some(&b':') {
return None;
}
i = scan_value(b, i + 1)?;
Some(skip_ws(b, i))
}
/// `object` starting at `b[p] == '{'`. Returns the offset past the closing `}`.
pub fn scan_object(b: &[u8], p: usize) -> Option<usize> {
let mut i = p + 1;
let probe = skip_ws(b, i);
if b.get(probe) == Some(&b'}') {
return Some(probe + 1);
}
i = scan_pair(b, i)?;
while b.get(i) == Some(&b',') {
i = scan_pair(b, i + 1)?;
}
i = skip_ws(b, i);
if b.get(i) == Some(&b'}') {
Some(i + 1)
} else {
None
}
}
/// The kind of value a top-level key must hold for a match to be accepted, mirroring whether the
/// PHP pattern captures `(?&json)`, `(?&object)` or `(?&array)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueKind {
Json,
Object,
Array,
}
/// A located top-level key/value within a JSON object's text. All fields are byte offsets into the
/// scanned contents:
/// - `key_pos`: where the `"key"` token begins (after `\s* { \s*` and any preceding pairs)
/// - `value_pos`: where the value begins (after `"key" \s* : \s*`)
/// - `value_end`: just past the value
#[derive(Debug, Clone, Copy)]
pub struct KeyMatch {
pub key_pos: usize,
pub value_pos: usize,
pub value_end: usize,
}
/// Locates a top-level key whose token text is exactly `encoded_key` (e.g. `"config"`), reproducing
/// the JsonManipulator patterns of the form
/// `\s* \{ \s* (?: (?&string) \s* : (?&json) \s* , \s* )*? KEY \s* : \s* (value)`.
///
/// Returns `None` when the object cannot be parsed up to the key, or the target value is not of the
/// requested `kind` (matching how the PCRE grammar would fail and the caller would abort).
pub fn find_top_level_key(
contents: &[u8],
encoded_key: &[u8],
kind: ValueKind,
) -> Option<KeyMatch> {
let mut i = skip_ws(contents, 0);
if contents.get(i) != Some(&b'{') {
return None;
}
i = skip_ws(contents, i + 1);
loop {
// Each top-level entry must start with a string key.
if contents.get(i) != Some(&b'"') {
return None;
}
let key_pos = i;
let key_end = scan_string(contents, i)?;
let is_target = &contents[key_pos..key_end] == encoded_key;
let colon = skip_ws(contents, key_end);
if contents.get(colon) != Some(&b':') {
return None;
}
let value_pos = skip_ws(contents, colon + 1);
if is_target {
let value_end = match kind {
ValueKind::Json => scan_value(contents, value_pos),
ValueKind::Object if contents.get(value_pos) == Some(&b'{') => {
scan_object(contents, value_pos)
}
ValueKind::Array if contents.get(value_pos) == Some(&b'[') => {
scan_array(contents, value_pos)
}
_ => None,
};
// Keys are unique, so a wrong-kind target cannot match later either.
return value_end.map(|value_end| KeyMatch {
key_pos,
value_pos,
value_end,
});
}
// A non-target entry must be a complete `key : value ,` pair to keep scanning.
let value_end = scan_value(contents, value_pos)?;
let comma = skip_ws(contents, value_end);
if contents.get(comma) != Some(&b',') {
return None;
}
i = skip_ws(contents, comma + 1);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn full(scan: fn(&[u8], usize) -> Option<usize>, s: &str) -> bool {
scan(s.as_bytes(), 0) == Some(s.len())
}
#[test]
fn strings() {
assert!(full(scan_string, r#""hello""#));
assert!(full(scan_string, r#""a\"b""#));
assert!(full(scan_string, r#""é""#));
assert!(full(scan_string, r#""""#));
assert_eq!(scan_string(br#""abc"#, 0), None); // unterminated
assert_eq!(scan_string(br#""\x""#, 0), None); // invalid escape
}
#[test]
fn numbers() {
assert!(full(scan_number, "0"));
assert!(full(scan_number, "-1"));
assert!(full(scan_number, "12.5"));
assert!(full(scan_number, "1e10"));
assert!(full(scan_number, "-3.14E-2"));
assert_eq!(scan_number(b"01", 0), None); // leading zero
assert_eq!(scan_number(b"1.", 0), Some(1)); // ".": no fraction digits -> number is just "1"
}
#[test]
fn values_and_containers() {
assert!(full(scan_value, " true"));
assert!(full(scan_value, "[1, 2, 3]"));
assert!(full(scan_value, "[]"));
assert!(full(scan_value, r#"{"a": 1, "b": [true, null]}"#));
assert!(full(scan_value, "{}"));
assert!(full(
scan_value,
"{\n \"a\": {\n \"b\": \"c\"\n }\n}"
));
assert_eq!(scan_value(br#"{"a": }"#, 0), None); // missing value
assert_eq!(scan_value(br#"{"a" 1}"#, 0), None); // missing colon
}
#[test]
fn value_stops_at_end_of_value() {
// scan_value reports the end of the value, leaving trailing content.
assert_eq!(scan_value(br#"{"a": 1}, "rest""#, 0), Some(8));
}
#[test]
fn finds_top_level_key() {
let c = "{\n \"foo\": \"bar\",\n \"require\": {\n \"a\": \"1\"\n }\n}";
let m = find_top_level_key(c.as_bytes(), br#""require""#, ValueKind::Object).unwrap();
assert_eq!(&c[m.key_pos..m.value_pos], "\"require\": ");
assert_eq!(
&c[m.value_pos..m.value_end],
"{\n \"a\": \"1\"\n }"
);
// first key
let m2 = find_top_level_key(c.as_bytes(), br#""foo""#, ValueKind::Json).unwrap();
assert_eq!(&c[m2.value_pos..m2.value_end], "\"bar\"");
// missing key
assert!(find_top_level_key(c.as_bytes(), br#""nope""#, ValueKind::Json).is_none());
// wrong kind
assert!(find_top_level_key(c.as_bytes(), br#""foo""#, ValueKind::Array).is_none());
}
}
|