@@ -273,9 +273,9 @@ impl Regex {
273
273
/// let re = regex!(r"'([^']+)'\s+\((\d{4})\)");
274
274
/// let text = "Not my favorite movie: 'Citizen Kane' (1941).";
275
275
/// let caps = re.captures(text).unwrap();
276
- /// assert_eq!(caps.at(1), "Citizen Kane");
277
- /// assert_eq!(caps.at(2), "1941");
278
- /// assert_eq!(caps.at(0), "'Citizen Kane' (1941)");
276
+ /// assert_eq!(caps.at(1), Some( "Citizen Kane") );
277
+ /// assert_eq!(caps.at(2), Some( "1941") );
278
+ /// assert_eq!(caps.at(0), Some( "'Citizen Kane' (1941)") );
279
279
/// # }
280
280
/// ```
281
281
///
@@ -291,9 +291,9 @@ impl Regex {
291
291
/// let re = regex!(r"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)");
292
292
/// let text = "Not my favorite movie: 'Citizen Kane' (1941).";
293
293
/// let caps = re.captures(text).unwrap();
294
- /// assert_eq!(caps.name("title"), "Citizen Kane");
295
- /// assert_eq!(caps.name("year"), "1941");
296
- /// assert_eq!(caps.at(0), "'Citizen Kane' (1941)");
294
+ /// assert_eq!(caps.name("title"), Some( "Citizen Kane") );
295
+ /// assert_eq!(caps.name("year"), Some( "1941") );
296
+ /// assert_eq!(caps.at(0), Some( "'Citizen Kane' (1941)") );
297
297
/// # }
298
298
/// ```
299
299
///
@@ -434,7 +434,7 @@ impl Regex {
434
434
/// # use regex::Captures; fn main() {
435
435
/// let re = regex!(r"([^,\s]+),\s+(\S+)");
436
436
/// let result = re.replace("Springsteen, Bruce", |&: caps: &Captures| {
437
- /// format!("{} {}", caps.at(2), caps.at(1))
437
+ /// format!("{} {}", caps.at(2).unwrap_or("") , caps.at(1).unwrap_or("" ))
438
438
/// });
439
439
/// assert_eq!(result.as_slice(), "Bruce Springsteen");
440
440
/// # }
@@ -712,27 +712,25 @@ impl<'t> Captures<'t> {
712
712
Some ( ( self . locs [ s] . unwrap ( ) , self . locs [ e] . unwrap ( ) ) )
713
713
}
714
714
715
- /// Returns the matched string for the capture group `i`.
716
- /// If `i` isn't a valid capture group or didn't match anything, then the
717
- /// empty string is returned.
718
- pub fn at ( & self , i : uint ) -> & ' t str {
715
+ /// Returns the matched string for the capture group `i`. If `i` isn't
716
+ /// a valid capture group or didn't match anything, then `None` is
717
+ /// returned.
718
+ pub fn at ( & self , i : uint ) -> Option < & ' t str > {
719
719
match self . pos ( i) {
720
- None => "" ,
721
- Some ( ( s, e) ) => {
722
- self . text . slice ( s, e)
723
- }
720
+ None => None ,
721
+ Some ( ( s, e) ) => Some ( self . text . slice ( s, e) )
724
722
}
725
723
}
726
724
727
- /// Returns the matched string for the capture group named `name`.
728
- /// If `name` isn't a valid capture group or didn't match anything, then
729
- /// the empty string is returned.
730
- pub fn name ( & self , name : & str ) -> & ' t str {
725
+ /// Returns the matched string for the capture group named `name`. If
726
+ /// `name` isn't a valid capture group or didn't match anything, then
727
+ /// `None` is returned.
728
+ pub fn name ( & self , name : & str ) -> Option < & ' t str > {
731
729
match self . named {
732
- None => "" ,
730
+ None => None ,
733
731
Some ( ref h) => {
734
732
match h. get ( name) {
735
- None => "" ,
733
+ None => None ,
736
734
Some ( i) => self . at ( * i) ,
737
735
}
738
736
}
@@ -769,11 +767,12 @@ impl<'t> Captures<'t> {
769
767
// FIXME: Don't use regexes for this. It's completely unnecessary.
770
768
let re = Regex :: new ( r"(^|[^$]|\b)\$(\w+)" ) . unwrap ( ) ;
771
769
let text = re. replace_all ( text, |& mut : refs: & Captures | -> String {
772
- let ( pre, name) = ( refs. at ( 1 ) , refs. at ( 2 ) ) ;
770
+ let pre = refs. at ( 1 ) . unwrap_or ( "" ) ;
771
+ let name = refs. at ( 2 ) . unwrap_or ( "" ) ;
773
772
format ! ( "{}{}" , pre,
774
773
match from_str:: <uint>( name. as_slice( ) ) {
775
- None => self . name( name) . to_string( ) ,
776
- Some ( i) => self . at( i) . to_string( ) ,
774
+ None => self . name( name) . unwrap_or ( "" ) . to_string( ) ,
775
+ Some ( i) => self . at( i) . unwrap_or ( "" ) . to_string( ) ,
777
776
} )
778
777
} ) ;
779
778
let re = Regex :: new ( r"\$\$" ) . unwrap ( ) ;
@@ -802,7 +801,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
802
801
fn next ( & mut self ) -> Option < & ' t str > {
803
802
if self . idx < self . caps . len ( ) {
804
803
self . idx += 1 ;
805
- Some ( self . caps . at ( self . idx - 1 ) )
804
+ Some ( self . caps . at ( self . idx - 1 ) . unwrap_or ( "" ) )
806
805
} else {
807
806
None
808
807
}
0 commit comments