File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -556,7 +556,6 @@ impl<W: CodeUnitWidth> Regex<W> {
556
556
let mut capacity = 256 ;
557
557
let mut output: Vec < W :: PCRE2_CHAR > = Vec :: with_capacity ( capacity) ;
558
558
capacity = output. capacity ( ) ;
559
- let mut saved_capacity = capacity;
560
559
561
560
let mut rc = unsafe {
562
561
self . code
@@ -569,7 +568,6 @@ impl<W: CodeUnitWidth> Regex<W> {
569
568
return Err ( rc. unwrap_err ( ) ) ;
570
569
}
571
570
capacity = output. capacity ( ) ;
572
- saved_capacity = capacity;
573
571
rc = unsafe {
574
572
self . code . substitute (
575
573
subject,
@@ -583,23 +581,25 @@ impl<W: CodeUnitWidth> Regex<W> {
583
581
}
584
582
}
585
583
586
- Ok ( match rc? {
584
+ let s = match rc? {
587
585
0 => Cow :: Borrowed ( subject) ,
588
586
_ => {
589
587
// +1 to account for null terminator
590
- let result = unsafe {
591
- Vec :: from_raw_parts ( output. as_mut_ptr ( ) , capacity + 1 , saved_capacity)
592
- } ;
593
- std:: mem:: forget ( output) ;
594
- let x: Vec < W :: SubjectChar > = result
588
+ unsafe { output. set_len ( capacity + 1 ) } ;
589
+
590
+ // this is really just a type cast
591
+ let x: Vec < W :: SubjectChar > = output
595
592
. into_iter ( )
596
593
. map ( W :: PCRE2_CHAR :: try_into)
594
+ // we don't want to return the null terminator
595
+ . take ( capacity)
597
596
. collect :: < Result < Vec < W :: SubjectChar > , _ > > ( )
598
597
. expect ( "PCRE2 returned invalid characters" ) ;
599
598
600
599
Cow :: Owned ( x)
601
600
}
602
- } )
601
+ } ;
602
+ Ok ( s)
603
603
}
604
604
}
605
605
You can’t perform that action at this time.
0 commit comments