Skip to content

Commit

Permalink
Remove custom extend_from_slice implementation.
Browse files Browse the repository at this point in the history
This was added because regex 0.1 supports Rust 1.3+. But we can now
assume Rust 1.12+, which has Vec::extend_from_slice. Yay for less unsafe!
  • Loading branch information
BurntSushi committed Dec 30, 2016
1 parent 374f139 commit c4faddf
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ impl Regex {
if limit > 0 && i >= limit {
break
}
extend_from_slice(&mut new, &text[last_match..m.start()]);
extend_from_slice(&mut new, &*rep);
new.extend_from_slice(&text[last_match..m.start()]);
new.extend_from_slice(&rep);
last_match = m.end();
}
extend_from_slice(&mut new, &text[last_match..]);
new.extend_from_slice(&text[last_match..]);
return Cow::Owned(new);
}

Expand All @@ -515,11 +515,11 @@ impl Regex {
}
// unwrap on 0 is OK because captures only reports matches
let m = cap.get(0).unwrap();
extend_from_slice(&mut new, &text[last_match..m.start()]);
new.extend_from_slice(&text[last_match..m.start()]);
rep.replace_append(&cap, &mut new);
last_match = m.end();
}
extend_from_slice(&mut new, &text[last_match..]);
new.extend_from_slice(&text[last_match..]);
Cow::Owned(new)
}
}
Expand Down Expand Up @@ -980,7 +980,7 @@ impl<'a> Replacer for &'a [u8] {

impl<F> Replacer for F where F: FnMut(&Captures) -> Vec<u8> {
fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>) {
extend_from_slice(dst, &(*self)(caps));
dst.extend_from_slice(&(*self)(caps));
}
}

Expand All @@ -996,26 +996,10 @@ pub struct NoExpand<'r>(pub &'r [u8]);

impl<'a> Replacer for NoExpand<'a> {
fn replace_append(&mut self, _: &Captures, dst: &mut Vec<u8>) {
extend_from_slice(dst, self.0);
dst.extend_from_slice(self.0);
}

fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
Some(Cow::Borrowed(self.0))
}
}

/// This hopefully has the same performance characteristics as
/// Vec::extend_from_slice (which was introduced in Rust 1.6), but works on
/// Rust 1.3.
///
/// N.B. Remove this once we do a semver bump. At that point, we'll bump
/// required Rust version to at least 1.6.
fn extend_from_slice(dst: &mut Vec<u8>, src: &[u8]) {
dst.reserve(src.len());
let dst_len = dst.len();
unsafe { dst.set_len(dst_len + src.len()); }
let mut dst = &mut dst[dst_len..dst_len + src.len()];
for i in 0..src.len() {
dst[i] = src[i];
}
}

0 comments on commit c4faddf

Please sign in to comment.