Skip to content

Commit 51cd32c

Browse files
committed
Sync with rust master
1 parent fe958e0 commit 51cd32c

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
//! let re = regex!(r"(\d{4})-(\d{2})-(\d{2})");
104104
//! let text = "2012-03-14, 2013-01-01 and 2014-07-05";
105105
//! for cap in re.captures_iter(text) {
106-
//! println!("Month: {} Day: {} Year: {}", cap.at(2), cap.at(3), cap.at(1));
106+
//! println!("Month: {} Day: {} Year: {}",
107+
//! cap.at(2).unwrap_or(""), cap.at(3).unwrap_or(""),
108+
//! cap.at(1).unwrap_or(""));
107109
//! }
108110
//! // Output:
109111
//! // Month: 03 Day: 14 Year: 2012
@@ -285,7 +287,7 @@
285287
//! # fn main() {
286288
//! let re = regex!(r"(?i)a+(?-i)b+");
287289
//! let cap = re.captures("AaAaAbbBBBb").unwrap();
288-
//! assert_eq!(cap.at(0), "AaAaAbb");
290+
//! assert_eq!(cap.at(0), Some("AaAaAbb"));
289291
//! # }
290292
//! ```
291293
//!

src/parse.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@ impl<'a> Parser<'a> {
838838
// Otherwise, an error will be returned.
839839
// Generally, `allow_start` is only true when you're *not* expecting an
840840
// opening parenthesis.
841-
fn pos_last<F>(&self, allow_start: bool, pred: F)
842-
-> Result<uint, Error>
843-
where F: Fn(&BuildAst) -> bool {
841+
fn pos_last<P>(&self, allow_start: bool, pred: P) -> Result<uint, Error> where
842+
P: FnMut(&BuildAst) -> bool,
843+
{
844844
let from = match self.stack.iter().rev().position(pred) {
845845
Some(i) => i,
846846
None => {
@@ -888,8 +888,9 @@ impl<'a> Parser<'a> {
888888
// build_from combines all AST elements starting at 'from' in the
889889
// parser's stack using 'mk' to combine them. If any such element is not an
890890
// AST then it is popped off the stack and ignored.
891-
fn build_from(&mut self, from: uint, mk: |Ast, Ast| -> Ast)
892-
-> Result<Ast, Error> {
891+
fn build_from<F>(&mut self, from: uint, mut mk: F) -> Result<Ast, Error> where
892+
F: FnMut(Ast, Ast) -> Ast,
893+
{
893894
if from >= self.stack.len() {
894895
return self.err("Empty group or alternate not allowed.")
895896
}

src/re.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
///
@@ -429,11 +429,12 @@ impl Regex {
429429
///
430430
/// ```rust
431431
/// # #![feature(phase)]
432+
/// # #![feature(unboxed_closures)]
432433
/// # extern crate regex; #[phase(plugin)] extern crate regex_macros;
433434
/// # use regex::Captures; fn main() {
434435
/// 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(""))
437438
/// });
438439
/// assert_eq!(result.as_slice(), "Bruce Springsteen");
439440
/// # }
@@ -585,7 +586,7 @@ impl<'t> Replacer for &'t str {
585586
}
586587
}
587588

588-
impl<'t> Replacer for |&Captures|: 't -> String {
589+
impl<F> Replacer for F where F: FnMut(&Captures) -> String {
589590
fn reg_replace<'a>(&'a mut self, caps: &Captures) -> CowString<'a> {
590591
(*self)(caps).into_cow()
591592
}
@@ -711,27 +712,25 @@ impl<'t> Captures<'t> {
711712
Some((self.locs[s].unwrap(), self.locs[e].unwrap()))
712713
}
713714

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> {
718719
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))
723722
}
724723
}
725724

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> {
730729
match self.named {
731-
None => "",
730+
None => None,
732731
Some(ref h) => {
733732
match h.get(name) {
734-
None => "",
733+
None => None,
735734
Some(i) => self.at(*i),
736735
}
737736
}
@@ -767,12 +766,13 @@ impl<'t> Captures<'t> {
767766
// How evil can you get?
768767
// FIXME: Don't use regexes for this. It's completely unnecessary.
769768
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("");
772772
format!("{}{}", pre,
773773
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(),
776776
})
777777
});
778778
let re = Regex::new(r"\$\$").unwrap();
@@ -801,7 +801,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
801801
fn next(&mut self) -> Option<&'t str> {
802802
if self.idx < self.caps.len() {
803803
self.idx += 1;
804-
Some(self.caps.at(self.idx - 1))
804+
Some(self.caps.at(self.idx - 1).unwrap_or(""))
805805
} else {
806806
None
807807
}

src/vm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub use self::StepState::*;
3838

3939
use std::cmp;
4040
use std::mem;
41+
use std::slice::SliceExt;
4142
use compile::{
4243
Program,
4344
Match, OneChar, CharClass, Any, EmptyBegin, EmptyEnd, EmptyWordBoundary,

0 commit comments

Comments
 (0)