aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/preg.rs
blob: 4b4346747774d105b5265a40c5d13a22e7c8b026 (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
pub const PREG_PATTERN_ORDER: i64 = 1;
pub const PREG_SET_ORDER: i64 = 2;
pub const PREG_OFFSET_CAPTURE: i64 = 256;
pub const PREG_UNMATCHED_AS_NULL: i64 = 512;
pub const PREG_SPLIT_NO_EMPTY: i64 = 1;
pub const PREG_SPLIT_DELIM_CAPTURE: i64 = 2;
pub const PREG_SPLIT_OFFSET_CAPTURE: i64 = 4;
pub const PREG_GREP_INVERT: i64 = 1;

#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum CaptureKey {
    ByIndex(usize),
    ByName(String),
}

#[derive(Debug, Default)]
pub struct PregOffsetCaptureMatches {
    groups: Vec<Vec<(String, usize)>>,
}

impl PregOffsetCaptureMatches {
    pub fn group(&self, i: usize) -> &[(String, usize)] {
        &self.groups[i]
    }
}

pub fn preg_quote(str: &str, delimiter: Option<char>) -> String {
    // Regex pattern compatibility:
    // PHP's preg_quote escapes `<` and `>` (PCRE treats `\<`/`\>` as literals), but the `regex`
    // crate reads `\<`/`\>` as start-of-word / end-of-word boundary assertions. `<` and `>` are
    // already literal in the `regex` crate, so they are emitted unescaped to preserve the intended
    // literal match.
    const SPECIAL: &str = ".\\+*?[^]$(){}=!|:-#";
    let mut out = String::new();
    for c in str.chars() {
        if c == '\0' {
            out.push_str("\\000");
        } else if SPECIAL.contains(c) || Some(c) == delimiter {
            out.push('\\');
            out.push(c);
        } else {
            out.push(c);
        }
    }
    out
}

// Returns whether the pattern matched; populates matches[0]=full match, matches[1..]=captures.
// Optional groups that did not participate in the match are stored as None.
pub fn preg_match(pattern: &str, subject: &str, matches: &mut Vec<Option<String>>) -> bool {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    matches.clear();
    match re.captures(subject) {
        Some(caps) => {
            for g in 0..caps.len() {
                matches.push(caps.get(g).map(|m| m.as_str().to_string()));
            }
            true
        }
        None => false,
    }
}

pub fn preg_replace(pattern: &str, replacement: &str, subject: &str) -> String {
    preg_replace2(pattern, replacement, subject, -1, None)
}

pub fn preg_split(pattern: &str, subject: &str) -> Vec<String> {
    preg_split2(pattern, subject, -1, 0)
}

// PREG_PATTERN_ORDER: the outer vec is indexed by capture group, the inner by
// match occurrence. Non-participating groups are reported as "".
pub fn preg_match_all(pattern: &str, subject: &str) -> Vec<Vec<String>> {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let group_count = re.captures_len();
    let mut groups: Vec<Vec<String>> = vec![Vec::new(); group_count];
    for caps in re.captures_iter(subject) {
        for (g, group) in groups.iter_mut().enumerate() {
            group.push(
                caps.get(g)
                    .map(|m| m.as_str().to_string())
                    .unwrap_or_default(),
            );
        }
    }
    groups
}

// PREG_SET_ORDER: the outer vec is indexed by match occurrence, the inner by
// capture group (a classic `$matches` row).
pub fn preg_match_all_set_order(
    pattern: &str,
    subject: &str,
    matches: &mut Vec<Vec<String>>,
) -> usize {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let mut rows: Vec<Vec<String>> = Vec::new();
    for caps in re.captures_iter(subject) {
        rows.push(php_match_row(&caps));
    }
    let count = rows.len();
    *matches = rows;
    count
}

pub fn preg_grep(pattern: &str, input: &[String]) -> Vec<String> {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    input.iter().filter(|s| re.is_match(s)).cloned().collect()
}

pub fn preg_match_all_offset_capture(
    pattern: &str,
    subject: &str,
    matches: &mut PregOffsetCaptureMatches,
) -> usize {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let group_count = re.captures_len();
    matches.groups = vec![Vec::new(); group_count];

    let mut count = 0;
    for caps in re.captures_iter(subject) {
        count += 1;
        for g in 0..group_count {
            // PHP stores ["", -1] for non-participating groups under
            // PREG_OFFSET_CAPTURE; the unsigned offset here approximates -1 as 0,
            // which callers must not rely on for absent groups.
            let entry = caps
                .get(g)
                .map(|m| (m.as_str().to_string(), m.start()))
                .unwrap_or_else(|| (String::new(), 0));
            matches.groups[g].push(entry);
        }
    }

    count
}

pub fn preg_replace_callback<F>(
    pattern: &str,
    mut callback: F,
    subject: &str,
) -> anyhow::Result<String>
where
    F: FnMut(&[Option<String>]) -> anyhow::Result<String>,
{
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let mut out: Vec<u8> = Vec::new();
    let mut last = 0;
    for caps in re.captures_iter(subject) {
        let m = caps.get(0).unwrap();
        out.extend_from_slice(&subject.as_bytes()[last..m.start()]);
        let groups: Vec<Option<String>> = (0..caps.len())
            .map(|g| caps.get(g).map(|x| x.as_str().to_string()))
            .collect();
        let replaced = callback(&groups)?;
        out.extend_from_slice(replaced.as_bytes());
        last = m.end();
    }
    out.extend_from_slice(&subject.as_bytes()[last..]);
    Ok(String::from_utf8_lossy(&out).into_owned())
}

// Returns whether the pattern matched. Unmatched groups are reported as None
// (PREG_UNMATCHED_AS_NULL).
pub fn preg_match2(
    pattern: &str,
    subject: &str,
    matches: &mut indexmap::IndexMap<CaptureKey, Option<String>>,
    flags: i64,
    offset: usize,
) -> bool {
    let (re, anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0;
    // An anchored (`A`) pattern must match starting exactly at `offset`; the `regex` crate cannot
    // anchor a `captures_at` search, so search the sub-slice beginning at `offset` and require the
    // match to start at its head.
    let caps = if anchored {
        re.captures(&subject[offset..])
            .filter(|c| c.get(0).map(|m| m.start()) == Some(0))
    } else {
        re.captures_at(subject, offset)
    };

    matches.clear();
    if let Some(caps) = &caps {
        let names: Vec<Option<&str>> = re.capture_names().collect();
        *matches = single_match_map(caps, &names, unmatched_as_null);
    }

    caps.is_some()
}

pub fn preg_match_all2(
    pattern: &str,
    subject: &str,
    matches: &mut indexmap::IndexMap<CaptureKey, Vec<Option<String>>>,
    flags: i64,
    offset: usize,
) -> usize {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0;
    let group_count = re.captures_len();
    let names: Vec<Option<&str>> = re.capture_names().collect();

    // PREG_PATTERN_ORDER: one column per group, one row per match occurrence.
    let mut groups: Vec<Vec<Option<String>>> = vec![Vec::new(); group_count];
    let mut count = 0;
    for caps in re.captures_iter(&subject[offset..]) {
        count += 1;
        for (g, column) in groups.iter_mut().enumerate() {
            let value = caps.get(g).map(|m| m.as_str().to_string());
            column.push(if unmatched_as_null {
                value
            } else {
                Some(value.unwrap_or_default())
            });
        }
    }

    matches.clear();
    for (g, column) in groups.into_iter().enumerate() {
        if let Some(Some(name)) = names.get(g) {
            matches.insert(CaptureKey::ByName((*name).to_string()), column.clone());
        }
        matches.insert(CaptureKey::ByIndex(g), column);
    }

    count
}

pub fn preg_match_all_offset_capture2(
    pattern: &str,
    subject: &str,
    matches: &mut indexmap::IndexMap<CaptureKey, Vec<(Option<String>, i64)>>,
    flags: i64,
    offset: usize,
) -> usize {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0;
    let group_count = re.captures_len();
    let names: Vec<Option<&str>> = re.capture_names().collect();

    let mut groups: Vec<Vec<(Option<String>, i64)>> = vec![Vec::new(); group_count];
    let mut count = 0;
    for caps in re.captures_iter(&subject[offset..]) {
        count += 1;
        for (g, column) in groups.iter_mut().enumerate() {
            let entry = match caps.get(g) {
                Some(m) => (Some(m.as_str().to_string()), (m.start() + offset) as i64),
                None if unmatched_as_null => (None, -1),
                None => (Some(String::new()), -1),
            };
            column.push(entry);
        }
    }

    matches.clear();
    for (g, column) in groups.into_iter().enumerate() {
        if let Some(Some(name)) = names.get(g) {
            matches.insert(CaptureKey::ByName((*name).to_string()), column.clone());
        }
        matches.insert(CaptureKey::ByIndex(g), column);
    }

    count
}

pub fn preg_replace2(
    pattern: &str,
    replacement: &str,
    subject: &str,
    limit: i64,
    count: Option<&mut usize>,
) -> String {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let limit = if limit < 0 {
        usize::MAX
    } else {
        limit as usize
    };

    let mut out: Vec<u8> = Vec::new();
    let mut last = 0usize;
    let mut n = 0usize;
    for caps in re.captures_iter(subject) {
        if n >= limit {
            break;
        }
        let m = caps.get(0).unwrap();
        out.extend_from_slice(&subject.as_bytes()[last..m.start()]);
        php_replacement_expand(replacement, &caps, &mut out);
        last = m.end();
        n += 1;
    }
    out.extend_from_slice(&subject.as_bytes()[last..]);

    if let Some(count) = count {
        *count = n;
    }
    String::from_utf8_lossy(&out).into_owned()
}

pub fn preg_replace_callback2<
    F: FnMut(&indexmap::IndexMap<CaptureKey, Option<String>>) -> String,
>(
    pattern: &str,
    mut callback: F,
    subject: &str,
    limit: i64,
    count: Option<&mut usize>,
    flags: i64,
) -> String {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let unmatched_as_null = flags & PREG_UNMATCHED_AS_NULL != 0;
    let names: Vec<Option<&str>> = re.capture_names().collect();
    let limit = if limit < 0 {
        usize::MAX
    } else {
        limit as usize
    };

    let mut out: Vec<u8> = Vec::new();
    let mut last = 0usize;
    let mut n = 0usize;
    for caps in re.captures_iter(subject) {
        if n >= limit {
            break;
        }
        let m = caps.get(0).unwrap();
        out.extend_from_slice(&subject.as_bytes()[last..m.start()]);
        let map = single_match_map(&caps, &names, unmatched_as_null);
        out.extend_from_slice(callback(&map).as_bytes());
        last = m.end();
        n += 1;
    }
    out.extend_from_slice(&subject.as_bytes()[last..]);

    if let Some(count) = count {
        *count = n;
    }
    String::from_utf8_lossy(&out).into_owned()
}

pub fn preg_split2(pattern: &str, subject: &str, limit: i64, flags: i64) -> Vec<String> {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let no_empty = flags & PREG_SPLIT_NO_EMPTY != 0;
    let delim_capture = flags & PREG_SPLIT_DELIM_CAPTURE != 0;
    // `limit` counts the resulting pieces; a non-positive value means no limit.
    let max_delims = if limit > 0 {
        (limit as usize).saturating_sub(1)
    } else {
        usize::MAX
    };

    let mut result: Vec<String> = Vec::new();
    let push = |s: &str, result: &mut Vec<String>| {
        if !(no_empty && s.is_empty()) {
            result.push(s.to_string());
        }
    };

    let mut last = 0usize;
    for (delims, caps) in re.captures_iter(subject).enumerate() {
        if delims >= max_delims {
            break;
        }
        let m = caps.get(0).unwrap();
        push(&subject[last..m.start()], &mut result);
        if delim_capture {
            // Mirror preg_match: trailing unmatched groups are dropped, interior
            // unmatched groups are emitted as "".
            if let Some(last_g) = (1..caps.len()).rev().find(|&g| caps.get(g).is_some()) {
                for g in 1..=last_g {
                    push(caps.get(g).map(|x| x.as_str()).unwrap_or(""), &mut result);
                }
            }
        }
        last = m.end();
    }
    push(&subject[last..], &mut result);

    result
}

pub fn preg_grep2(pattern: &str, array: &[&str], flags: i64) -> Vec<String> {
    let (re, _anchored) =
        compile_php_pattern(pattern).unwrap_or_else(|e| panic!("invalid regex: {e}"));
    let invert = flags & PREG_GREP_INVERT != 0;
    array
        .iter()
        .filter(|s| re.is_match(s) != invert)
        .map(|s| s.to_string())
        .collect()
}

// Translates a PHP PCRE pattern (delimiters + trailing modifiers) into a regex
// the `regex` crate can compile. Only delimiter stripping and the i/x/s/m
// modifiers are handled; PCRE-only constructs (possessive quantifiers,
// lookaround, backreferences) are not supported by `regex` and must be avoided
// in the caller's pattern.
// TODO(phase-c): replace with a faithful PCRE engine to restore full semantics.
// PCRE treats `\<` and `\>` as escaped literal `<`/`>`, but the `regex` crate
// reads them as start/end-of-word boundary assertions. Rewrite those escapes to
// the literal characters so PCRE-sourced patterns (e.g. anything run through
// `preg_quote`, which escapes `<` and `>`) keep their original meaning. A `\\`
// escapes the following backslash, so `\\<` is left untouched.
fn translate_pcre_literals(inner: &str) -> String {
    let mut out = String::with_capacity(inner.len());
    let mut chars = inner.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.peek() {
                Some('<') | Some('>') => {
                    out.push(chars.next().unwrap());
                }
                Some('\\') => {
                    out.push('\\');
                    out.push(chars.next().unwrap());
                }
                _ => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    out
}

fn compile_php_pattern(pattern: &str) -> anyhow::Result<(regex::Regex, bool)> {
    let delimiter = pattern
        .chars()
        .next()
        .ok_or_else(|| anyhow::anyhow!("empty regex pattern"))?;
    // PCRE allows bracket-style delimiters whose closing character differs from
    // the opening one: `(...)`, `{...}`, `[...]`, `<...>`.
    let closing = match delimiter {
        '(' => ')',
        '{' => '}',
        '[' => ']',
        '<' => '>',
        c => c,
    };
    let end = pattern
        .rfind(closing)
        .filter(|&i| i >= delimiter.len_utf8())
        .ok_or_else(|| anyhow::anyhow!("unterminated regex pattern: {pattern}"))?;
    let inner = &pattern[delimiter.len_utf8()..end];
    let modifiers = &pattern[end + closing.len_utf8()..];

    let flags: String = modifiers
        .chars()
        .filter(|c| matches!(c, 'i' | 'x' | 's' | 'm'))
        .collect();

    // PCRE's `A` (PCRE_ANCHORED) modifier requires the match to start exactly at the search offset.
    // The `regex` crate has no per-search anchoring, so the offset-based callers
    // (`preg_match2`/`preg_match_all2`) honour it by searching a sub-slice that begins at the offset;
    // here we only surface the flag.
    let anchored = modifiers.contains('A');

    let inner = translate_pcre_literals(inner);
    let translated = if flags.is_empty() {
        inner
    } else {
        format!("(?{flags}){inner}")
    };

    Ok((regex::Regex::new(&translated)?, anchored))
}

// Expands a PHP preg replacement template against `caps`, appending bytes to
// `out`. Backreferences are written as `$1`, `${1}`, `\1` or `\\1`; a literal
// `$` or `\` not forming a reference is emitted verbatim. Out-of-range or
// non-participating groups expand to nothing.
fn php_replacement_expand(template: &str, caps: &regex::Captures, out: &mut Vec<u8>) {
    let bytes = template.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'\\' if i + 1 < bytes.len() && bytes[i + 1].is_ascii_digit() => {
                let (group, consumed) = php_replacement_group(&bytes[i + 1..]);
                if let Some(m) = caps.get(group) {
                    out.extend_from_slice(m.as_str().as_bytes());
                }
                i += 1 + consumed;
            }
            b'\\' if i + 1 < bytes.len() && bytes[i + 1] == b'\\' => {
                out.push(b'\\');
                i += 2;
            }
            // A backslash escapes a following `$`, yielding a literal dollar sign (so an escaped
            // `\$1` is not mistaken for the `$1` backreference).
            b'\\' if i + 1 < bytes.len() && bytes[i + 1] == b'$' => {
                out.push(b'$');
                i += 2;
            }
            b'$' if i + 1 < bytes.len() && bytes[i + 1] == b'{' => {
                let rest = &bytes[i + 2..];
                match rest.iter().position(|&b| b == b'}') {
                    Some(c) if c > 0 && rest[..c].iter().all(|b| b.is_ascii_digit()) => {
                        let group: usize =
                            std::str::from_utf8(&rest[..c]).unwrap().parse().unwrap();
                        if let Some(m) = caps.get(group) {
                            out.extend_from_slice(m.as_str().as_bytes());
                        }
                        i += 2 + c + 1;
                    }
                    _ => {
                        out.push(b'$');
                        i += 1;
                    }
                }
            }
            b'$' if i + 1 < bytes.len() && bytes[i + 1].is_ascii_digit() => {
                let (group, consumed) = php_replacement_group(&bytes[i + 1..]);
                if let Some(m) = caps.get(group) {
                    out.extend_from_slice(m.as_str().as_bytes());
                }
                i += 1 + consumed;
            }
            b => {
                out.push(b);
                i += 1;
            }
        }
    }
}

// Reads up to two leading ASCII digits as a PHP backreference group number.
fn php_replacement_group(bytes: &[u8]) -> (usize, usize) {
    let mut group = 0usize;
    let mut consumed = 0usize;
    while consumed < 2 && consumed < bytes.len() && bytes[consumed].is_ascii_digit() {
        group = group * 10 + (bytes[consumed] - b'0') as usize;
        consumed += 1;
    }
    (group, consumed)
}

// Classic preg_match `$matches` row: index 0 is the full match, trailing
// unmatched groups are truncated and interior unmatched groups become "".
fn php_match_row(caps: &regex::Captures) -> Vec<String> {
    let last = (0..caps.len())
        .rev()
        .find(|&g| caps.get(g).is_some())
        .unwrap_or(0);
    (0..=last)
        .map(|g| {
            caps.get(g)
                .map(|m| m.as_str().to_string())
                .unwrap_or_default()
        })
        .collect()
}

// Builds a single match's `$matches` map with both named and numbered keys
// (the named key precedes its number). With PREG_UNMATCHED_AS_NULL, every group
// is present and non-participating ones are None; otherwise classic semantics
// apply: trailing unmatched groups are dropped and interior ones become "".
fn single_match_map(
    caps: &regex::Captures,
    names: &[Option<&str>],
    unmatched_as_null: bool,
) -> indexmap::IndexMap<CaptureKey, Option<String>> {
    let mut out = indexmap::IndexMap::new();
    let group_count = caps.len();
    let last_participating = (0..group_count).rev().find(|&i| caps.get(i).is_some());

    for i in 0..group_count {
        let m = caps.get(i);
        if !unmatched_as_null
            && m.is_none()
            && let Some(last) = last_participating
            && i > last
        {
            break;
        }
        let value = if unmatched_as_null {
            m.map(|m| m.as_str().to_string())
        } else {
            Some(m.map(|m| m.as_str().to_string()).unwrap_or_default())
        };
        if let Some(Some(name)) = names.get(i) {
            out.insert(CaptureKey::ByName((*name).to_string()), value.clone());
        }
        out.insert(CaptureKey::ByIndex(i), value);
    }
    out
}