@@ -47,7 +47,7 @@ mod template;
47
47
48
48
use dashmap:: DashMap ;
49
49
use memchr:: memchr_iter;
50
- use once_cell:: sync:: { Lazy , OnceCell } ;
50
+ use once_cell:: sync:: Lazy ;
51
51
use std:: collections:: HashMap ;
52
52
use std:: time:: { Duration , Instant } ;
53
53
use strip_ansi_escapes:: strip;
@@ -686,10 +686,7 @@ pub enum StringOp {
686
686
/// let template = Template::parse("{split:,:..|filter:\\.txt$|join:\\n}").unwrap();
687
687
/// assert_eq!(template.format("file.txt,readme.md,data.txt").unwrap(), "file.txt\ndata.txt");
688
688
/// ```
689
- Filter {
690
- pattern : String ,
691
- regex : OnceCell < Regex > ,
692
- } ,
689
+ Filter { pattern : String } ,
693
690
694
691
/// Remove list items matching a regex pattern.
695
692
///
@@ -724,10 +721,7 @@ pub enum StringOp {
724
721
/// let template = Template::parse("{split:\\n:..|filter_not:^$|join:\\n}").unwrap();
725
722
/// assert_eq!(template.format("line1\n\nline2\n\nline3").unwrap(), "line1\nline2\nline3");
726
723
/// ```
727
- FilterNot {
728
- pattern : String ,
729
- regex : OnceCell < Regex > ,
730
- } ,
724
+ FilterNot { pattern : String } ,
731
725
732
726
/// Select a range of items from a list.
733
727
///
@@ -899,7 +893,6 @@ pub enum StringOp {
899
893
RegexExtract {
900
894
pattern : String ,
901
895
group : Option < usize > ,
902
- regex : OnceCell < Regex > ,
903
896
} ,
904
897
}
905
898
@@ -1360,21 +1353,17 @@ fn apply_single_operation(
1360
1353
StringOp :: Slice { range } => {
1361
1354
apply_list_operation ( val, |list| apply_range ( & list, range) , "Slice" )
1362
1355
}
1363
- StringOp :: Filter { pattern, regex } => {
1364
- let re = regex. get_or_try_init ( || {
1365
- Regex :: new ( pattern) . map_err ( |e| format ! ( "Invalid regex: {e}" ) )
1366
- } ) ?;
1356
+ StringOp :: Filter { pattern } => {
1357
+ let re = get_cached_regex ( pattern) ?;
1367
1358
match val {
1368
1359
Value :: List ( list) => Ok ( Value :: List (
1369
1360
list. into_iter ( ) . filter ( |s| re. is_match ( s) ) . collect ( ) ,
1370
1361
) ) ,
1371
1362
Value :: Str ( s) => Ok ( Value :: Str ( if re. is_match ( & s) { s } else { String :: new ( ) } ) ) ,
1372
1363
}
1373
1364
}
1374
- StringOp :: FilterNot { pattern, regex } => {
1375
- let re = regex. get_or_try_init ( || {
1376
- Regex :: new ( pattern) . map_err ( |e| format ! ( "Invalid regex: {e}" ) )
1377
- } ) ?;
1365
+ StringOp :: FilterNot { pattern } => {
1366
+ let re = get_cached_regex ( pattern) ?;
1378
1367
match val {
1379
1368
Value :: List ( list) => Ok ( Value :: List (
1380
1369
list. into_iter ( ) . filter ( |s| !re. is_match ( s) ) . collect ( ) ,
@@ -1576,15 +1565,9 @@ fn apply_single_operation(
1576
1565
)
1577
1566
}
1578
1567
}
1579
- StringOp :: RegexExtract {
1580
- pattern,
1581
- group,
1582
- regex,
1583
- } => {
1568
+ StringOp :: RegexExtract { pattern, group } => {
1584
1569
if let Value :: Str ( s) = val {
1585
- let re = regex. get_or_try_init ( || {
1586
- Regex :: new ( pattern) . map_err ( |e| format ! ( "Invalid regex: {e}" ) )
1587
- } ) ?;
1570
+ let re = get_cached_regex ( pattern) ?;
1588
1571
let result = if let Some ( group_idx) = group {
1589
1572
re. captures ( & s)
1590
1573
. and_then ( |caps| caps. get ( * group_idx) )
0 commit comments