@@ -273,9 +273,9 @@ impl Regex {
273273 /// let re = regex!(r"'([^']+)'\s+\((\d{4})\)");
274274 /// let text = "Not my favorite movie: 'Citizen Kane' (1941).";
275275 /// 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)") );
279279 /// # }
280280 /// ```
281281 ///
@@ -291,9 +291,9 @@ impl Regex {
291291 /// let re = regex!(r"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)");
292292 /// let text = "Not my favorite movie: 'Citizen Kane' (1941).";
293293 /// 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)") );
297297 /// # }
298298 /// ```
299299 ///
@@ -434,7 +434,7 @@ impl Regex {
434434 /// # use regex::Captures; fn main() {
435435 /// let re = regex!(r"([^,\s]+),\s+(\S+)");
436436 /// 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("" ))
438438 /// });
439439 /// assert_eq!(result.as_slice(), "Bruce Springsteen");
440440 /// # }
@@ -712,27 +712,25 @@ impl<'t> Captures<'t> {
712712 Some ( ( self . locs [ s] . unwrap ( ) , self . locs [ e] . unwrap ( ) ) )
713713 }
714714
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 > {
719719 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) )
724722 }
725723 }
726724
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 > {
731729 match self . named {
732- None => "" ,
730+ None => None ,
733731 Some ( ref h) => {
734732 match h. get ( name) {
735- None => "" ,
733+ None => None ,
736734 Some ( i) => self . at ( * i) ,
737735 }
738736 }
@@ -769,11 +767,12 @@ impl<'t> Captures<'t> {
769767 // FIXME: Don't use regexes for this. It's completely unnecessary.
770768 let re = Regex :: new ( r"(^|[^$]|\b)\$(\w+)" ) . unwrap ( ) ;
771769 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 ( "" ) ;
773772 format ! ( "{}{}" , pre,
774773 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( ) ,
777776 } )
778777 } ) ;
779778 let re = Regex :: new ( r"\$\$" ) . unwrap ( ) ;
@@ -802,7 +801,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
802801 fn next ( & mut self ) -> Option < & ' t str > {
803802 if self . idx < self . caps . len ( ) {
804803 self . idx += 1 ;
805- Some ( self . caps . at ( self . idx - 1 ) )
804+ Some ( self . caps . at ( self . idx - 1 ) . unwrap_or ( "" ) )
806805 } else {
807806 None
808807 }
0 commit comments