@@ -496,7 +496,8 @@ impl<W: CodeUnitWidth> Regex<W> {
496
496
& self . capture_names_idx
497
497
}
498
498
499
- /// Replace the first instance of
499
+ /// Replace the first match in the subject string with the replacement
500
+ /// If `extended` is true, enable PCRE2's extended replacement syntax.
500
501
pub fn replace < ' s > (
501
502
& self ,
502
503
subject : & ' s [ W :: SubjectChar ] ,
@@ -505,13 +506,12 @@ impl<W: CodeUnitWidth> Regex<W> {
505
506
) -> Result < Cow < ' s , [ W :: SubjectChar ] > , Error >
506
507
where
507
508
[ <W as CodeUnitWidth >:: PCRE2_CHAR ] : ToOwned ,
508
- W :: PCRE2_CHAR : TryInto < W :: SubjectChar > ,
509
- <<W as CodeUnitWidth >:: PCRE2_CHAR as TryInto < <W as CodeUnitWidth >:: SubjectChar > >:: Error :
510
- std:: fmt:: Debug ,
511
509
{
512
510
self . replace_impl ( subject, replacement, false , extended)
513
511
}
514
512
513
+ /// Replace all non-overlapping matches in the subject string with the replacement
514
+ /// If `extended` is true, enable PCRE2's extended replacement syntax.
515
515
pub fn replace_all < ' s > (
516
516
& self ,
517
517
subject : & ' s [ W :: SubjectChar ] ,
@@ -520,9 +520,6 @@ impl<W: CodeUnitWidth> Regex<W> {
520
520
) -> Result < Cow < ' s , [ W :: SubjectChar ] > , Error >
521
521
where
522
522
[ <W as CodeUnitWidth >:: PCRE2_CHAR ] : ToOwned ,
523
- W :: PCRE2_CHAR : TryInto < W :: SubjectChar > ,
524
- <<W as CodeUnitWidth >:: PCRE2_CHAR as TryInto < <W as CodeUnitWidth >:: SubjectChar > >:: Error :
525
- std:: fmt:: Debug ,
526
523
{
527
524
self . replace_impl ( subject, replacement, true , extended)
528
525
}
@@ -537,13 +534,10 @@ impl<W: CodeUnitWidth> Regex<W> {
537
534
) -> Result < Cow < ' s , [ W :: SubjectChar ] > , Error >
538
535
where
539
536
[ <W as CodeUnitWidth >:: PCRE2_CHAR ] : ToOwned ,
540
- W :: PCRE2_CHAR : TryInto < W :: SubjectChar > ,
541
- <<W as CodeUnitWidth >:: PCRE2_CHAR as TryInto < <W as CodeUnitWidth >:: SubjectChar > >:: Error :
542
- std:: fmt:: Debug ,
543
537
{
544
538
let mut options: u32 = 0 ;
545
539
options |= PCRE2_SUBSTITUTE_OVERFLOW_LENGTH ;
546
- // TODO: this should probably be configurabe from user-side
540
+ // TODO: this should probably be configurable from user-side
547
541
options |= PCRE2_SUBSTITUTE_UNSET_EMPTY ;
548
542
if extended {
549
543
options |= PCRE2_SUBSTITUTE_EXTENDED ;
@@ -585,16 +579,21 @@ impl<W: CodeUnitWidth> Regex<W> {
585
579
0 => Cow :: Borrowed ( subject) ,
586
580
_ => {
587
581
// +1 to account for null terminator
588
- unsafe { output. set_len ( capacity + 1 ) } ;
582
+ unsafe { output. set_len ( capacity + 1 ) } ;
583
+
584
+ // All inputs contained valid chars, so we expect all outputs to as well.
585
+ let to_char = |c : W :: PCRE2_CHAR | -> W :: SubjectChar {
586
+ c. try_into ( )
587
+ . unwrap_or_else ( |_| panic ! ( "all output expected to be valid chars" ) )
588
+ } ;
589
589
590
590
// this is really just a type cast
591
591
let x: Vec < W :: SubjectChar > = output
592
592
. into_iter ( )
593
- . map ( W :: PCRE2_CHAR :: try_into )
593
+ . map ( to_char )
594
594
// we don't want to return the null terminator
595
595
. take ( capacity)
596
- . collect :: < Result < Vec < W :: SubjectChar > , _ > > ( )
597
- . expect ( "PCRE2 returned invalid characters" ) ;
596
+ . collect :: < Vec < W :: SubjectChar > > ( ) ;
598
597
599
598
Cow :: Owned ( x)
600
599
}
0 commit comments