seuil 0.1.1

A complete, safe JSONata implementation in Rust — JSON query, transform, and expression evaluation
Documentation
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! String built-in functions for JSONata.

use std::borrow::Cow;

use crate::evaluator::value::{ArrayFlags, FnContext, Value};
use crate::{Error, Result, Span};

use super::{assert_arg, assert_array_of_type, bad_arg, max_args, min_args};

// ---------------------------------------------------------------------------
// $string(arg, prettify?)
// ---------------------------------------------------------------------------

pub fn fn_string<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    max_args!(context, args, 2);

    let input = if args.is_empty() {
        context.input
    } else {
        args[0]
    };

    if input.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }

    // When called with no explicit args and input is Null (no data),
    // JSONata returns undefined.
    if args.is_empty() && input.is_null() {
        return Ok(Value::undefined(context.arena));
    }

    let pretty = args.get(1).copied();
    if let Some(p) = pretty {
        assert_arg!(p.is_undefined() || p.is_bool(), context, 2);
    }

    if input.is_string() {
        return Ok(input);
    }
    if input.is_function() {
        return Ok(Value::string(context.arena, ""));
    }
    if input.is_number() && !input.is_finite() {
        return Err(Error::D3001StringNotFinite(Span::at(context.char_index)));
    }

    let is_pretty = pretty
        .map(|p| matches!(p, Value::Bool(true)))
        .unwrap_or(false);

    if is_pretty {
        let output = input.serialize_strict(true)?;
        Ok(Value::string(context.arena, &output))
    } else {
        let output = input.serialize_strict(false)?;
        Ok(Value::string(context.arena, &output))
    }
}

// ---------------------------------------------------------------------------
// $length(str)
// ---------------------------------------------------------------------------

pub fn fn_length<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    max_args!(context, args, 1);

    let arg = if args.is_empty() {
        // No explicit arg: use context input
        let input = context.input;
        if input.is_undefined() || input.is_null() {
            return Err(Error::T0411ContextNotValid(
                Span::at(context.char_index),
                1,
                context.name.to_string(),
            ));
        }
        if !input.is_string() {
            return Err(Error::T0411ContextNotValid(
                Span::at(context.char_index),
                1,
                context.name.to_string(),
            ));
        }
        input
    } else {
        args[0]
    };

    if arg.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }

    assert_arg!(arg.is_string(), context, 1);
    Ok(Value::number(
        context.arena,
        arg.as_str().chars().count() as f64,
    ))
}

// ---------------------------------------------------------------------------
// $substring(str, start, length?)
// ---------------------------------------------------------------------------

pub fn fn_substring<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let string = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let start = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let length = args
        .get(2)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if string.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }

    assert_arg!(string.is_string(), context, 1);
    assert_arg!(start.is_number(), context, 2);

    let string = string.as_str();
    let len = string.chars().count() as isize;
    let mut start_idx = start.as_isize();

    if len + start_idx < 0 {
        start_idx = 0;
    }
    let start_idx = if start_idx < 0 {
        len + start_idx
    } else {
        start_idx
    };

    if length.is_undefined() {
        let sub: String = string.chars().skip(start_idx as usize).collect();
        Ok(Value::string(context.arena, &sub))
    } else {
        assert_arg!(length.is_number(), context, 3);
        let length = length.as_isize();
        if length < 0 {
            Ok(Value::string(context.arena, ""))
        } else {
            let end = (start_idx + length) as usize;
            let sub: String = string
                .chars()
                .skip(start_idx as usize)
                .take(end - start_idx as usize)
                .collect();
            Ok(Value::string(context.arena, &sub))
        }
    }
}

// ---------------------------------------------------------------------------
// $substringBefore(str, chars)
// ---------------------------------------------------------------------------

pub fn fn_substring_before<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let string = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let chars = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if !string.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    if !chars.is_string() {
        return Err(Error::D3010EmptyPattern(Span::at(context.char_index)));
    }

    let s: &str = &string.as_str();
    let c: &str = &chars.as_str();

    if let Some(index) = s.find(c) {
        Ok(Value::string(context.arena, &s[..index]))
    } else {
        Ok(Value::string(context.arena, s))
    }
}

// ---------------------------------------------------------------------------
// $substringAfter(str, chars)
// ---------------------------------------------------------------------------

pub fn fn_substring_after<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let string = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let chars = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if !string.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    if !chars.is_string() {
        return Err(Error::D3010EmptyPattern(Span::at(context.char_index)));
    }

    let s: &str = &string.as_str();
    let c: &str = &chars.as_str();

    if let Some(index) = s.find(c) {
        let after = index + c.len();
        Ok(Value::string(context.arena, &s[after..]))
    } else {
        Ok(Value::string(context.arena, s))
    }
}

// ---------------------------------------------------------------------------
// $uppercase(str)
// ---------------------------------------------------------------------------

pub fn fn_uppercase<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let arg = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if !arg.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    Ok(Value::string(context.arena, &arg.as_str().to_uppercase()))
}

// ---------------------------------------------------------------------------
// $lowercase(str)
// ---------------------------------------------------------------------------

pub fn fn_lowercase<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let arg = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if !arg.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    Ok(Value::string(context.arena, &arg.as_str().to_lowercase()))
}

// ---------------------------------------------------------------------------
// $trim(str)
// ---------------------------------------------------------------------------

pub fn fn_trim<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    let arg = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if !arg.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    let original = arg.as_str();
    let mut words = original.split_whitespace();
    let trimmed = match words.next() {
        None => String::new(),
        Some(first) => {
            let mut result = String::from(first);
            for word in words {
                result.push(' ');
                result.push_str(word);
            }
            result
        }
    };
    Ok(Value::string(context.arena, &trimmed))
}

// ---------------------------------------------------------------------------
// $pad(str, width, char?)
// ---------------------------------------------------------------------------

pub fn fn_pad<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    let str_value = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if !str_value.is_string() {
        return Ok(Value::undefined(context.arena));
    }
    let width_value = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if !width_value.is_number() {
        return Ok(Value::undefined(context.arena));
    }

    let s = str_value.as_str();
    let width_i64 = width_value.as_f64().round() as i64;
    let width = width_i64.unsigned_abs() as usize;
    let is_right = width_i64 > 0;

    let pad_char = args
        .get(2)
        .and_then(|v| v.try_as_str())
        .filter(|c| !c.is_empty())
        .unwrap_or(Cow::Borrowed(" "));

    let pad_len = width.saturating_sub(s.chars().count());
    if pad_len == 0 {
        return Ok(Value::string(context.arena, &s));
    }

    let padding: String = pad_char.chars().cycle().take(pad_len).collect();
    let result = if is_right {
        format!("{}{}", s, padding)
    } else {
        format!("{}{}", padding, s)
    };
    Ok(Value::string(context.arena, &result))
}

// ---------------------------------------------------------------------------
// $contains(str, pattern)
// ---------------------------------------------------------------------------

pub fn fn_contains<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    let str_value = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let token = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if str_value.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    assert_arg!(str_value.is_string(), context, 1);

    let s = str_value.as_str();
    let result = match token {
        Value::Regex(ref regex_literal) => regex_literal.get_regex().find(&s).is_some(),
        Value::String(_) => {
            let tok = token.as_str();
            s.contains(&*tok)
        }
        _ => bad_arg!(context, 2),
    };

    Ok(Value::bool_val(context.arena, result))
}

// ---------------------------------------------------------------------------
// $split(str, separator, limit?)
// ---------------------------------------------------------------------------

pub fn fn_split<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    let str_value = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let separator = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let limit_value = args
        .get(2)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if str_value.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    assert_arg!(str_value.is_string(), context, 1);

    let s = str_value.as_str();
    let sep_is_regex = matches!(separator, Value::Regex(_));
    if !sep_is_regex && !separator.is_string() {
        bad_arg!(context, 2);
    }

    let limit = if limit_value.is_undefined() {
        None
    } else {
        assert_arg!(limit_value.is_number(), context, 3);
        if limit_value.as_f64() < 0.0 {
            return Err(Error::D3020NegativeLimit(Span::at(context.char_index)));
        }
        Some(limit_value.as_f64() as usize)
    };

    let substrings: Vec<String> = if sep_is_regex {
        let regex = match separator {
            Value::Regex(ref rl) => rl.get_regex(),
            _ => unreachable!(),
        };
        let mut results = Vec::new();
        let mut last_end = 0;
        let effective_limit = limit.unwrap_or(usize::MAX);
        for m in regex.find_iter(&s) {
            if results.len() >= effective_limit {
                break;
            }
            if m.start() > last_end {
                results.push(s[last_end..m.start()].to_string());
            }
            last_end = m.end();
        }
        if results.len() < effective_limit {
            results.push(s[last_end..].to_string());
        }
        results
    } else {
        let sep_str = separator.as_str();
        if sep_str.is_empty() {
            if let Some(limit) = limit {
                s.chars().take(limit).map(|c| c.to_string()).collect()
            } else {
                s.chars().map(|c| c.to_string()).collect()
            }
        } else if let Some(limit) = limit {
            s.split(&*sep_str)
                .take(limit)
                .map(|x| x.to_string())
                .collect()
        } else {
            s.split(&*sep_str).map(|x| x.to_string()).collect()
        }
    };

    let result = Value::array_with_capacity(context.arena, substrings.len(), ArrayFlags::empty());
    for sub in &substrings {
        result.push(Value::string(context.arena, sub));
    }
    Ok(result)
}

// ---------------------------------------------------------------------------
// $join(array, separator?)
// ---------------------------------------------------------------------------

pub fn fn_join<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    min_args!(context, args, 1);
    max_args!(context, args, 2);
    let strings = args[0];

    if strings.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    if strings.is_string() {
        return Ok(strings);
    }

    assert_array_of_type!(strings.is_array(), context, 1, "string");

    let separator = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    assert_arg!(
        separator.is_undefined() || separator.is_string(),
        context,
        2
    );

    let sep: Cow<'_, str> = if separator.is_string() {
        separator.as_str()
    } else {
        Cow::Borrowed("")
    };

    let mut result = String::with_capacity(256);
    let total = strings.len();
    for (i, member) in strings.members().enumerate() {
        assert_array_of_type!(member.is_string(), context, 1, "string");
        result.push_str(&member.as_str());
        if i < total - 1 {
            result.push_str(&sep);
        }
    }

    Ok(Value::string(context.arena, &result))
}

// ---------------------------------------------------------------------------
// $replace(str, pattern, replacement, limit?)
// ---------------------------------------------------------------------------

pub fn fn_replace<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    let str_value = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let pattern = args
        .get(1)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let replacement = args
        .get(2)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    let limit_value = args
        .get(3)
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));

    if str_value.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    if pattern.is_string() && pattern.as_str().is_empty() {
        return Err(Error::D3010EmptyPattern(Span::at(context.char_index)));
    }

    assert_arg!(str_value.is_string(), context, 1);

    let s = str_value.as_str();
    let limit = if limit_value.is_undefined() {
        None
    } else {
        assert_arg!(limit_value.is_number(), context, 4);
        if limit_value.as_isize() < 0 {
            return Err(Error::D3011NegativeLimit(Span::at(context.char_index)));
        }
        Some(limit_value.as_isize() as usize)
    };

    // String pattern (simple replace)
    if pattern.is_string() {
        assert_arg!(replacement.is_string(), context, 3);
        let pat = pattern.as_str();
        let rep = replacement.as_str();
        let replaced = if let Some(limit) = limit {
            s.replacen(&*pat, &rep, limit)
        } else {
            s.replace(&*pat, &rep)
        };
        return Ok(Value::string(context.arena, &replaced));
    }

    // Regex pattern
    let regex = match pattern {
        Value::Regex(ref rl) => rl.get_regex(),
        _ => bad_arg!(context, 2),
    };

    // Replacement can be a string or a function
    if replacement.is_function() {
        // Function replacement: fn(match_obj) -> string
        let span = Span::at(context.char_index);
        let mut result = String::new();
        let mut last_end = 0;

        for (count, m) in regex.find_iter(&s).enumerate() {
            if m.range().is_empty() {
                return Err(Error::D1004ZeroLengthMatch(Span::at(context.char_index)));
            }
            if let Some(limit) = limit {
                if count >= limit {
                    break;
                }
            }
            result.push_str(&s[last_end..m.start()]);

            // Build match object like $match returns
            let matched_text = &s[m.start()..m.end()];
            let match_obj = Value::object(context.arena);
            match_obj.insert("match", Value::string(context.arena, matched_text));
            match_obj.insert("index", Value::number(context.arena, m.start() as f64));
            let groups = Value::array(context.arena, ArrayFlags::empty());
            for cap in &m.captures {
                if let Some(ref range) = cap {
                    groups.push(Value::string(context.arena, &s[range.start..range.end]));
                } else {
                    groups.push(Value::null(context.arena));
                }
            }
            match_obj.insert("groups", groups);

            let replaced = (context.apply_fn)(span, context.input, replacement, &[match_obj])?;
            if replaced.is_string() {
                result.push_str(&replaced.as_str());
            } else {
                return Err(Error::D3012InvalidReplacementType(Span::at(
                    context.char_index,
                )));
            }
            last_end = m.end();
        }
        result.push_str(&s[last_end..]);
        return Ok(Value::string(context.arena, &result));
    }

    assert_arg!(replacement.is_string(), context, 3);
    let rep_str = replacement.as_str();

    let mut result = String::new();
    let mut last_end = 0;

    for (count, m) in regex.find_iter(&s).enumerate() {
        if m.range().is_empty() {
            return Err(Error::D1004ZeroLengthMatch(Span::at(context.char_index)));
        }
        if let Some(limit) = limit {
            if count >= limit {
                break;
            }
        }
        result.push_str(&s[last_end..m.start()]);

        // Expand backreferences in replacement: $0, $1, $2, ..., $$
        let expanded = expand_backreferences(&rep_str, &m, &s);
        result.push_str(&expanded);
        last_end = m.end();
    }
    result.push_str(&s[last_end..]);

    Ok(Value::string(context.arena, &result))
}

// ---------------------------------------------------------------------------
// $match(str, pattern, limit?)
// ---------------------------------------------------------------------------

pub fn fn_match<'a>(context: FnContext<'a, '_>, args: &[&'a Value<'a>]) -> Result<&'a Value<'a>> {
    let value = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if value.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    assert_arg!(value.is_string(), context, 1);

    let pattern = args.get(1).copied();
    let pattern = match pattern {
        Some(p) => p,
        None => return Err(Error::D3010EmptyPattern(Span::at(context.char_index))),
    };

    let regex = match pattern {
        Value::Regex(ref rl) => rl.get_regex(),
        _ => return Err(Error::D3010EmptyPattern(Span::at(context.char_index))),
    };

    let limit = args.get(2).and_then(|v| {
        if v.is_number() {
            Some(v.as_f64() as usize)
        } else {
            None
        }
    });

    let input_str = value.as_str();
    let arena = context.arena;
    let effective_limit = limit.unwrap_or(usize::MAX);

    let matches_arr = Value::array(arena, ArrayFlags::empty());

    for (i, m) in regex.find_iter(&input_str).enumerate() {
        if i >= effective_limit {
            break;
        }

        let matched_text = &input_str[m.start()..m.end()];
        let match_obj = Value::object(arena);
        match_obj.insert("match", Value::string(arena, matched_text));
        match_obj.insert("index", Value::number(arena, m.start() as f64));

        // Build capture groups array (skip first element which is the full match)
        let groups = Value::array(arena, ArrayFlags::empty());
        for cap in &m.captures {
            if let Some(ref range) = cap {
                groups.push(Value::string(arena, &input_str[range.start..range.end]));
            } else {
                groups.push(Value::null(arena));
            }
        }
        match_obj.insert("groups", groups);
        matches_arr.push(match_obj);
    }

    // JSONata semantics: no matches → undefined, one match → unwrap object, multiple → array
    if matches_arr.is_empty() {
        Ok(Value::undefined(arena))
    } else if matches_arr.len() == 1 && limit.is_none() {
        Ok(matches_arr
            .get_member(0)
            .unwrap_or_else(|| Value::undefined(arena)))
    } else {
        Ok(matches_arr)
    }
}

// ---------------------------------------------------------------------------
// $base64encode(str)
// ---------------------------------------------------------------------------

pub fn fn_base64_encode<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    max_args!(context, args, 1);
    let arg = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if arg.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    assert_arg!(arg.is_string(), context, 1);

    use base64::Engine;
    let encoded = base64::engine::general_purpose::STANDARD.encode(arg.as_str().as_bytes());
    Ok(Value::string(context.arena, &encoded))
}

// ---------------------------------------------------------------------------
// $base64decode(str)
// ---------------------------------------------------------------------------

pub fn fn_base64_decode<'a>(
    context: FnContext<'a, '_>,
    args: &[&'a Value<'a>],
) -> Result<&'a Value<'a>> {
    max_args!(context, args, 1);
    let arg = args
        .first()
        .copied()
        .unwrap_or_else(|| Value::undefined(context.arena));
    if arg.is_undefined() {
        return Ok(Value::undefined(context.arena));
    }
    assert_arg!(arg.is_string(), context, 1);

    use base64::Engine;
    let decoded = base64::engine::general_purpose::STANDARD
        .decode(arg.as_str().as_bytes())
        .map_err(|e| Error::D3137Error(e.to_string()))?;
    let decoded_str = String::from_utf8(decoded).map_err(|e| Error::D3137Error(e.to_string()))?;
    Ok(Value::string(context.arena, &decoded_str))
}

// ---------------------------------------------------------------------------
// Regex backreference expansion
// ---------------------------------------------------------------------------

/// Expand backreference patterns in a replacement string.
/// $0 = full match, $1..$N = capture groups, $$ = literal '$'
/// Supports multi-digit group refs: $12 matches group 12 if it exists,
/// otherwise tries $1 followed by literal "2".
/// Non-existent single-digit groups produce empty string.
fn expand_backreferences(replacement: &str, m: &regress::Match, input: &str) -> String {
    let mut result = String::with_capacity(replacement.len());
    let bytes = replacement.as_bytes();
    let num_captures = m.captures.len(); // number of capture groups (not counting full match)
    let mut i = 0;

    while i < bytes.len() {
        if bytes[i] == b'$' && i + 1 < bytes.len() {
            let next = bytes[i + 1];
            if next == b'$' {
                // $$ -> literal $
                result.push('$');
                i += 2;
            } else if next.is_ascii_digit() {
                // Collect all consecutive digits after $
                let digit_start = i + 1;
                let mut digit_end = digit_start;
                while digit_end < bytes.len() && bytes[digit_end].is_ascii_digit() {
                    digit_end += 1;
                }
                let digits = &replacement[digit_start..digit_end];

                // Try the longest possible group number, then fall back
                // to shorter prefixes. E.g., $12: try group 12, else try group 1 + "2"
                let mut matched = false;
                let mut try_len = digits.len();
                while try_len > 0 {
                    let try_num: usize = digits[..try_len].parse().unwrap_or(0);
                    if try_num <= num_captures {
                        // Use m.group() which handles 0=full match, 1..=N=captures
                        if let Some(range) = m.group(try_num) {
                            result.push_str(&input[range.start..range.end]);
                        }
                        // Append remaining digits as literal text
                        result.push_str(&digits[try_len..]);
                        matched = true;
                        break;
                    }
                    try_len -= 1;
                }
                if !matched {
                    // Single-digit $N where N > num_captures: consume $N, output empty
                    // (JSONata behavior: non-existent group refs produce empty string)
                    if digits.len() == 1 {
                        // Just consume $N and output nothing
                    } else {
                        // Multi-digit: consume only $+first_digit, rest are literal
                        // e.g. $18 with only 0 captures: $1->empty, "8" literal
                        result.push_str(&digits[1..]);
                    }
                }
                i = digit_end;
            } else {
                result.push('$');
                i += 1;
            }
        } else {
            result.push(bytes[i] as char);
            i += 1;
        }
    }

    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Span;
    use bumpalo::Bump;

    fn dummy_apply_fn<'a>(
        _span: Span,
        _input: &'a Value<'a>,
        _proc: &'a Value<'a>,
        _args: &[&'a Value<'a>],
    ) -> crate::Result<&'a Value<'a>> {
        Err(crate::Error::D3137Error("dummy apply_fn".to_string()))
    }

    fn ctx(arena: &Bump) -> FnContext<'_, '_> {
        FnContext {
            name: "test",
            char_index: 0,
            input: Value::undefined(arena),
            arena,
            apply_fn: &dummy_apply_fn,
        }
    }

    #[test]
    fn test_length() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello");
        let result = fn_length(c, &[s]).unwrap();
        assert_eq!(result.as_f64(), 5.0);
    }

    #[test]
    fn test_uppercase_lowercase() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "Hello World");
        let up = fn_uppercase(c.clone(), &[s]).unwrap();
        assert_eq!(up.as_str().as_ref(), "HELLO WORLD");
        let lo = fn_lowercase(c, &[s]).unwrap();
        assert_eq!(lo.as_str().as_ref(), "hello world");
    }

    #[test]
    fn test_trim() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "  hello   world  ");
        let result = fn_trim(c, &[s]).unwrap();
        assert_eq!(result.as_str().as_ref(), "hello world");
    }

    #[test]
    fn test_substring() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello");
        let start = Value::number(&arena, 1.0);
        let len = Value::number(&arena, 3.0);
        let result = fn_substring(c, &[s, start, len]).unwrap();
        assert_eq!(result.as_str().as_ref(), "ell");
    }

    #[test]
    fn test_contains_string() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello world");
        let tok = Value::string(&arena, "world");
        let result = fn_contains(c, &[s, tok]).unwrap();
        assert_eq!(result.as_bool(), true);
    }

    #[test]
    fn test_split_basic() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "a,b,c");
        let sep = Value::string(&arena, ",");
        let result = fn_split(c, &[s, sep]).unwrap();
        assert!(result.is_array());
        assert_eq!(result.len(), 3);
    }

    #[test]
    fn test_join() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let arr = Value::array(&arena, ArrayFlags::empty());
        arr.push(Value::string(&arena, "a"));
        arr.push(Value::string(&arena, "b"));
        arr.push(Value::string(&arena, "c"));
        let sep = Value::string(&arena, "-");
        let result = fn_join(c, &[arr, sep]).unwrap();
        assert_eq!(result.as_str().as_ref(), "a-b-c");
    }

    #[test]
    fn test_replace_string() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello world");
        let pat = Value::string(&arena, "world");
        let rep = Value::string(&arena, "rust");
        let result = fn_replace(c, &[s, pat, rep]).unwrap();
        assert_eq!(result.as_str().as_ref(), "hello rust");
    }

    #[test]
    fn test_base64_roundtrip() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello");
        let encoded = fn_base64_encode(c.clone(), &[s]).unwrap();
        let decoded = fn_base64_decode(c, &[encoded]).unwrap();
        assert_eq!(decoded.as_str().as_ref(), "hello");
    }

    #[test]
    fn test_pad() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hi");
        let w = Value::number(&arena, 5.0);
        let result = fn_pad(c, &[s, w]).unwrap();
        assert_eq!(result.as_str().as_ref(), "hi   ");
    }

    #[test]
    fn test_substring_before_after() {
        let arena = Bump::new();
        let c = ctx(&arena);
        let s = Value::string(&arena, "hello-world");
        let sep = Value::string(&arena, "-");
        let before = fn_substring_before(c.clone(), &[s, sep]).unwrap();
        assert_eq!(before.as_str().as_ref(), "hello");
        let after = fn_substring_after(c, &[s, sep]).unwrap();
        assert_eq!(after.as_str().as_ref(), "world");
    }
}