@@ -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
///
@@ -429,11 +429,12 @@ impl Regex {
429
429
///
430
430
/// ```rust
431
431
/// # #![feature(phase)]
432
+ /// # #![feature(unboxed_closures)]
432
433
/// # extern crate regex; #[phase(plugin)] extern crate regex_macros;
433
434
/// # use regex::Captures; fn main() {
434
435
/// let re = regex!(r"([^,\s]+),\s+(\S+)");
435
- /// let result = re.replace("Springsteen, Bruce", |caps: &Captures| {
436
- /// format!("{} {}", caps.at(2), caps.at(1))
436
+ /// let result = re.replace("Springsteen, Bruce", |&: caps: &Captures| {
437
+ /// format!("{} {}", caps.at(2).unwrap_or("") , caps.at(1).unwrap_or("" ))
437
438
/// });
438
439
/// assert_eq!(result.as_slice(), "Bruce Springsteen");
439
440
/// # }
@@ -585,7 +586,7 @@ impl<'t> Replacer for &'t str {
585
586
}
586
587
}
587
588
588
- impl < ' t > Replacer for | & Captures | : ' t -> String {
589
+ impl < F > Replacer for F where F : FnMut ( & Captures ) -> String {
589
590
fn reg_replace < ' a > ( & ' a mut self , caps : & Captures ) -> CowString < ' a > {
590
591
( * self ) ( caps) . into_cow ( )
591
592
}
@@ -711,27 +712,25 @@ impl<'t> Captures<'t> {
711
712
Some ( ( self . locs [ s] . unwrap ( ) , self . locs [ e] . unwrap ( ) ) )
712
713
}
713
714
714
- /// Returns the matched string for the capture group `i`.
715
- /// If `i` isn't a valid capture group or didn't match anything, then the
716
- /// empty string is returned.
717
- 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 > {
718
719
match self . pos ( i) {
719
- None => "" ,
720
- Some ( ( s, e) ) => {
721
- self . text . slice ( s, e)
722
- }
720
+ None => None ,
721
+ Some ( ( s, e) ) => Some ( self . text . slice ( s, e) )
723
722
}
724
723
}
725
724
726
- /// Returns the matched string for the capture group named `name`.
727
- /// If `name` isn't a valid capture group or didn't match anything, then
728
- /// the empty string is returned.
729
- 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 > {
730
729
match self . named {
731
- None => "" ,
730
+ None => None ,
732
731
Some ( ref h) => {
733
732
match h. get ( name) {
734
- None => "" ,
733
+ None => None ,
735
734
Some ( i) => self . at ( * i) ,
736
735
}
737
736
}
@@ -767,12 +766,13 @@ impl<'t> Captures<'t> {
767
766
// How evil can you get?
768
767
// FIXME: Don't use regexes for this. It's completely unnecessary.
769
768
let re = Regex :: new ( r"(^|[^$]|\b)\$(\w+)" ) . unwrap ( ) ;
770
- let text = re. replace_all ( text, |refs : & Captures | -> String {
771
- let ( pre, name) = ( refs. at ( 1 ) , refs. at ( 2 ) ) ;
769
+ let text = re. replace_all ( text, |& mut : refs: & Captures | -> String {
770
+ let pre = refs. at ( 1 ) . unwrap_or ( "" ) ;
771
+ let name = refs. at ( 2 ) . unwrap_or ( "" ) ;
772
772
format ! ( "{}{}" , pre,
773
773
match from_str:: <uint>( name. as_slice( ) ) {
774
- None => self . name( name) . to_string( ) ,
775
- 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( ) ,
776
776
} )
777
777
} ) ;
778
778
let re = Regex :: new ( r"\$\$" ) . unwrap ( ) ;
@@ -801,7 +801,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
801
801
fn next ( & mut self ) -> Option < & ' t str > {
802
802
if self . idx < self . caps . len ( ) {
803
803
self . idx += 1 ;
804
- Some ( self . caps . at ( self . idx - 1 ) )
804
+ Some ( self . caps . at ( self . idx - 1 ) . unwrap_or ( "" ) )
805
805
} else {
806
806
None
807
807
}
0 commit comments