diff options
Diffstat (limited to 'src/libregex')
| -rw-r--r-- | src/libregex/compile.rs | 3 | ||||
| -rw-r--r-- | src/libregex/parse.rs | 13 | ||||
| -rw-r--r-- | src/libregex/re.rs | 7 | ||||
| -rw-r--r-- | src/libregex/test/bench.rs | 23 | ||||
| -rw-r--r-- | src/libregex/vm.rs | 18 |
5 files changed, 36 insertions, 28 deletions
diff --git a/src/libregex/compile.rs b/src/libregex/compile.rs index c361a25bf52..1476e6ab8a7 100644 --- a/src/libregex/compile.rs +++ b/src/libregex/compile.rs @@ -14,6 +14,7 @@ pub use self::Inst::*; use std::cmp; +use std::iter::repeat; use parse; use parse::{ Flags, FLAG_EMPTY, @@ -157,7 +158,7 @@ impl<'r> Compiler<'r> { Capture(cap, name, x) => { let len = self.names.len(); if cap >= len { - self.names.grow(10 + cap - len, None) + self.names.extend(repeat(None).take(10 + cap - len)) } self.names[cap] = name; diff --git a/src/libregex/parse.rs b/src/libregex/parse.rs index 105687b89fd..692a065299c 100644 --- a/src/libregex/parse.rs +++ b/src/libregex/parse.rs @@ -18,7 +18,6 @@ use std::cmp; use std::fmt; use std::iter; use std::num; -use std::slice::BinarySearchResult; /// Static data containing Unicode ranges for general categories and scripts. use unicode::regex::{UNICODE_CLASSES, PERLD, PERLS, PERLW}; @@ -519,8 +518,8 @@ impl<'a> Parser<'a> { }; self.chari = closer; let greed = try!(self.get_next_greedy()); - let inner = String::from_chars( - self.chars[start+1..closer]); + let inner = self.chars[start+1..closer].iter().cloned() + .collect::<String>(); // Parse the min and max values from the regex. let (mut min, mut max): (uint, Option<uint>); @@ -954,7 +953,7 @@ impl<'a> Parser<'a> { } fn slice(&self, start: uint, end: uint) -> String { - String::from_chars(self.chars[start..end]) + self.chars[start..end].iter().cloned().collect() } } @@ -1028,9 +1027,9 @@ fn is_valid_cap(c: char) -> bool { } fn find_class(classes: NamedClasses, name: &str) -> Option<Vec<(char, char)>> { - match classes.binary_search(|&(s, _)| s.cmp(name)) { - BinarySearchResult::Found(i) => Some(classes[i].1.to_vec()), - BinarySearchResult::NotFound(_) => None, + match classes.binary_search_by(|&(s, _)| s.cmp(name)) { + Ok(i) => Some(classes[i].1.to_vec()), + Err(_) => None, } } diff --git a/src/libregex/re.rs b/src/libregex/re.rs index 1b6dcb3a8e2..3171966a596 100644 --- a/src/libregex/re.rs +++ b/src/libregex/re.rs @@ -540,6 +540,7 @@ impl Regex { } +#[deriving(Clone)] pub enum NamesIter<'a> { NamesIterNative(::std::slice::Iter<'a, Option<&'static str>>), NamesIterDynamic(::std::slice::Iter<'a, Option<String>>) @@ -596,6 +597,7 @@ impl<F> Replacer for F where F: FnMut(&Captures) -> String { /// /// `'r` is the lifetime of the compiled expression and `'t` is the lifetime /// of the string being split. +#[deriving(Clone)] pub struct RegexSplits<'r, 't> { finder: FindMatches<'r, 't>, last: uint, @@ -629,6 +631,7 @@ impl<'r, 't> Iterator<&'t str> for RegexSplits<'r, 't> { /// /// `'r` is the lifetime of the compiled expression and `'t` is the lifetime /// of the string being split. +#[deriving(Clone)] pub struct RegexSplitsN<'r, 't> { splits: RegexSplits<'r, 't>, cur: uint, @@ -792,6 +795,7 @@ impl<'t> Captures<'t> { /// expression. /// /// `'t` is the lifetime of the matched text. +#[deriving(Clone)] pub struct SubCaptures<'t> { idx: uint, caps: &'t Captures<'t>, @@ -814,6 +818,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> { /// Positions are byte indices in terms of the original string matched. /// /// `'t` is the lifetime of the matched text. +#[deriving(Clone)] pub struct SubCapturesPos<'t> { idx: uint, caps: &'t Captures<'t>, @@ -837,6 +842,7 @@ impl<'t> Iterator<Option<(uint, uint)>> for SubCapturesPos<'t> { /// /// `'r` is the lifetime of the compiled expression and `'t` is the lifetime /// of the matched string. +#[deriving(Clone)] pub struct FindCaptures<'r, 't> { re: &'r Regex, search: &'t str, @@ -879,6 +885,7 @@ impl<'r, 't> Iterator<Captures<'t>> for FindCaptures<'r, 't> { /// /// `'r` is the lifetime of the compiled expression and `'t` is the lifetime /// of the matched string. +#[deriving(Clone)] pub struct FindMatches<'r, 't> { re: &'r Regex, search: &'t str, diff --git a/src/libregex/test/bench.rs b/src/libregex/test/bench.rs index 0c204f759e6..17521ff7ea5 100644 --- a/src/libregex/test/bench.rs +++ b/src/libregex/test/bench.rs @@ -9,8 +9,9 @@ // except according to those terms. #![allow(non_snake_case)] -use std::rand::{Rng, task_rng}; +use std::rand::{Rng, thread_rng}; use stdtest::Bencher; +use std::iter::repeat; use regex::{Regex, NoExpand}; @@ -22,30 +23,30 @@ fn bench_assert_match(b: &mut Bencher, re: Regex, text: &str) { fn no_exponential(b: &mut Bencher) { let n = 100; let re = Regex::new(format!("{}{}", - "a?".repeat(n), - "a".repeat(n)).as_slice()).unwrap(); - let text = "a".repeat(n); + repeat("a?").take(n).collect::<String>(), + repeat("a").take(n).collect::<String>()).as_slice()).unwrap(); + let text = repeat("a").take(n).collect::<String>(); bench_assert_match(b, re, text.as_slice()); } #[bench] fn literal(b: &mut Bencher) { let re = regex!("y"); - let text = format!("{}y", "x".repeat(50)); + let text = format!("{}y", repeat("x").take(50).collect::<String>()); bench_assert_match(b, re, text.as_slice()); } #[bench] fn not_literal(b: &mut Bencher) { let re = regex!(".y"); - let text = format!("{}y", "x".repeat(50)); + let text = format!("{}y", repeat("x").take(50).collect::<String>()); bench_assert_match(b, re, text.as_slice()); } #[bench] fn match_class(b: &mut Bencher) { let re = regex!("[abcdw]"); - let text = format!("{}w", "xxxx".repeat(20)); + let text = format!("{}w", repeat("xxxx").take(20).collect::<String>()); bench_assert_match(b, re, text.as_slice()); } @@ -53,7 +54,7 @@ fn match_class(b: &mut Bencher) { fn match_class_in_range(b: &mut Bencher) { // 'b' is between 'a' and 'c', so the class range checking doesn't help. let re = regex!("[ac]"); - let text = format!("{}c", "bbbb".repeat(20)); + let text = format!("{}c", repeat("bbbb").take(20).collect::<String>()); bench_assert_match(b, re, text.as_slice()); } @@ -77,7 +78,7 @@ fn anchored_literal_short_non_match(b: &mut Bencher) { #[bench] fn anchored_literal_long_non_match(b: &mut Bencher) { let re = regex!("^zbc(d|e)"); - let text = "abcdefghijklmnopqrstuvwxyz".repeat(15); + let text = repeat("abcdefghijklmnopqrstuvwxyz").take(15).collect::<String>(); b.iter(|| re.is_match(text.as_slice())); } @@ -91,7 +92,7 @@ fn anchored_literal_short_match(b: &mut Bencher) { #[bench] fn anchored_literal_long_match(b: &mut Bencher) { let re = regex!("^.bc(d|e)"); - let text = "abcdefghijklmnopqrstuvwxyz".repeat(15); + let text = repeat("abcdefghijklmnopqrstuvwxyz").take(15).collect::<String>(); b.iter(|| re.is_match(text.as_slice())); } @@ -154,7 +155,7 @@ fn medium() -> Regex { regex!("[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$") } fn hard() -> Regex { regex!("[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$") } fn gen_text(n: uint) -> String { - let mut rng = task_rng(); + let mut rng = thread_rng(); let mut bytes = rng.gen_ascii_chars().map(|n| n as u8).take(n) .collect::<Vec<u8>>(); for (i, b) in bytes.iter_mut().enumerate() { diff --git a/src/libregex/vm.rs b/src/libregex/vm.rs index 0cdafb73b6e..603ca57d15d 100644 --- a/src/libregex/vm.rs +++ b/src/libregex/vm.rs @@ -39,6 +39,7 @@ pub use self::StepState::*; use std::cmp; use std::cmp::Ordering::{mod, Less, Equal, Greater}; use std::mem; +use std::iter::repeat; use std::slice::SliceExt; use compile::{ Program, @@ -122,7 +123,7 @@ impl<'r, 't> Nfa<'r, 't> { let mut clist = &mut Threads::new(self.which, ninsts, ncaps); let mut nlist = &mut Threads::new(self.which, ninsts, ncaps); - let mut groups = Vec::from_elem(ncaps * 2, None); + let mut groups: Vec<_> = repeat(None).take(ncaps * 2).collect(); // Determine if the expression starts with a '^' so we can avoid // simulating .*? @@ -228,8 +229,7 @@ impl<'r, 't> Nfa<'r, 't> { let negate = flags & FLAG_NEGATED > 0; let casei = flags & FLAG_NOCASE > 0; let found = ranges.as_slice(); - let found = found.binary_search(|&rc| class_cmp(casei, c, rc)) - .found().is_some(); + let found = found.binary_search_by(|&rc| class_cmp(casei, c, rc)).is_ok(); if found ^ negate { self.add(nlist, pc+1, caps); } @@ -458,10 +458,10 @@ impl Threads { fn new(which: MatchKind, num_insts: uint, ncaps: uint) -> Threads { Threads { which: which, - queue: Vec::from_fn(num_insts, |_| { - Thread { pc: 0, groups: Vec::from_elem(ncaps * 2, None) } - }), - sparse: Vec::from_elem(num_insts, 0u), + queue: range(0, num_insts).map(|_| { + Thread { pc: 0, groups: repeat(None).take(ncaps * 2).collect() } + }).collect(), + sparse: repeat(0u).take(num_insts).collect(), size: 0, } } @@ -519,7 +519,7 @@ pub fn is_word(c: Option<char>) -> bool { // Try the common ASCII case before invoking binary search. match c { '_' | '0' ... '9' | 'a' ... 'z' | 'A' ... 'Z' => true, - _ => PERLW.binary_search(|&(start, end)| { + _ => PERLW.binary_search_by(|&(start, end)| { if c >= start && c <= end { Equal } else if start > c { @@ -527,7 +527,7 @@ pub fn is_word(c: Option<char>) -> bool { } else { Less } - }).found().is_some() + }).is_ok() } } |
