diff options
author | Andrew Gallant <jamslam@gmail.com> | 2019-06-26 16:47:33 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2019-06-26 16:47:33 -0400 |
commit | b93762ea7a397b54e9ccf589a45a2c6ad91bc390 (patch) | |
tree | 54152ae9fb21c14f95ceefcb35e2d5b2f86295b1 /grep-searcher | |
parent | 34677d262246462ce3027ee57be1869a9dc7730a (diff) |
bstr: update everything to bstr 0.2
Diffstat (limited to 'grep-searcher')
-rw-r--r-- | grep-searcher/Cargo.toml | 2 | ||||
-rw-r--r-- | grep-searcher/examples/search-stdin.rs | 2 | ||||
-rw-r--r-- | grep-searcher/src/line_buffer.rs | 33 | ||||
-rw-r--r-- | grep-searcher/src/lines.rs | 10 | ||||
-rw-r--r-- | grep-searcher/src/searcher/core.rs | 4 | ||||
-rw-r--r-- | grep-searcher/src/testutil.rs | 4 |
6 files changed, 28 insertions, 27 deletions
diff --git a/grep-searcher/Cargo.toml b/grep-searcher/Cargo.toml index 9911c9d7..471d3dfe 100644 --- a/grep-searcher/Cargo.toml +++ b/grep-searcher/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["regex", "grep", "egrep", "search", "pattern"] license = "Unlicense/MIT" [dependencies] -bstr = { version = "0.1.2", default-features = false, features = ["std"] } +bstr = { version = "0.2.0", default-features = false, features = ["std"] } bytecount = "0.5" encoding_rs = "0.8.14" encoding_rs_io = "0.1.6" diff --git a/grep-searcher/examples/search-stdin.rs b/grep-searcher/examples/search-stdin.rs index 9d642973..9ce14915 100644 --- a/grep-searcher/examples/search-stdin.rs +++ b/grep-searcher/examples/search-stdin.rs @@ -17,7 +17,7 @@ fn main() { } } -fn example() -> Result<(), Box<Error>> { +fn example() -> Result<(), Box<dyn Error>> { let pattern = match env::args().nth(1) { Some(pattern) => pattern, None => return Err(From::from(format!( diff --git a/grep-searcher/src/line_buffer.rs b/grep-searcher/src/line_buffer.rs index cc7dd578..4b75fa37 100644 --- a/grep-searcher/src/line_buffer.rs +++ b/grep-searcher/src/line_buffer.rs @@ -1,7 +1,7 @@ use std::cmp; use std::io; -use bstr::{BStr, BString}; +use bstr::ByteSlice; /// The default buffer capacity that we use for the line buffer. pub(crate) const DEFAULT_BUFFER_CAPACITY: usize = 8 * (1<<10); // 8 KB @@ -122,7 +122,7 @@ impl LineBufferBuilder { pub fn build(&self) -> LineBuffer { LineBuffer { config: self.config, - buf: BString::from(vec![0; self.config.capacity]), + buf: vec![0; self.config.capacity], pos: 0, last_lineterm: 0, end: 0, @@ -254,13 +254,14 @@ impl<'b, R: io::Read> LineBufferReader<'b, R> { /// Return the contents of this buffer. pub fn buffer(&self) -> &[u8] { - self.line_buffer.buffer().as_bytes() + self.line_buffer.buffer() } - /// Return the underlying buffer as a byte string. Used for tests only. + /// Return the buffer as a BStr, used for convenient equality checking + /// in tests only. #[cfg(test)] - fn bstr(&self) -> &BStr { - self.line_buffer.buffer() + fn bstr(&self) -> &::bstr::BStr { + self.buffer().as_bstr() } /// Consume the number of bytes provided. This must be less than or equal @@ -289,7 +290,7 @@ pub struct LineBuffer { /// The configuration of this buffer. config: Config, /// The primary buffer with which to hold data. - buf: BString, + buf: Vec<u8>, /// The current position of this buffer. This is always a valid sliceable /// index into `buf`, and its maximum value is the length of `buf`. pos: usize, @@ -352,13 +353,13 @@ impl LineBuffer { } /// Return the contents of this buffer. - fn buffer(&self) -> &BStr { + fn buffer(&self) -> &[u8] { &self.buf[self.pos..self.last_lineterm] } /// Return the contents of the free space beyond the end of the buffer as /// a mutable slice. - fn free_buffer(&mut self) -> &mut BStr { + fn free_buffer(&mut self) -> &mut [u8] { &mut self.buf[self.end..] } @@ -481,7 +482,7 @@ impl LineBuffer { } let roll_len = self.end - self.pos; - self.buf.copy_within(self.pos.., 0); + self.buf.copy_within_str(self.pos.., 0); self.pos = 0; self.last_lineterm = roll_len; self.end = roll_len; @@ -519,7 +520,7 @@ impl LineBuffer { /// Replaces `src` with `replacement` in bytes, and return the offset of the /// first replacement, if one exists. -fn replace_bytes(bytes: &mut BStr, src: u8, replacement: u8) -> Option<usize> { +fn replace_bytes(bytes: &mut [u8], src: u8, replacement: u8) -> Option<usize> { if src == replacement { return None; } @@ -542,7 +543,7 @@ fn replace_bytes(bytes: &mut BStr, src: u8, replacement: u8) -> Option<usize> { #[cfg(test)] mod tests { use std::str; - use bstr::BString; + use bstr::{ByteSlice, ByteVec}; use super::*; const SHERLOCK: &'static str = "\ @@ -563,7 +564,7 @@ and exhibited clearly, with a label attached.\ src: u8, replacement: u8, ) -> (String, Option<usize>) { - let mut dst = BString::from(slice); + let mut dst = Vec::from(slice); let result = replace_bytes(&mut dst, src, replacement); (dst.into_string().unwrap(), result) } @@ -677,12 +678,12 @@ and exhibited clearly, with a label attached.\ let mut linebuf = LineBufferBuilder::new().capacity(1).build(); let mut rdr = LineBufferReader::new(bytes.as_bytes(), &mut linebuf); - let mut got = BString::new(); + let mut got = vec![]; while rdr.fill().unwrap() { - got.push(rdr.buffer()); + got.push_str(rdr.buffer()); rdr.consume_all(); } - assert_eq!(bytes, got); + assert_eq!(bytes, got.as_bstr()); assert_eq!(rdr.absolute_byte_offset(), bytes.len() as u64); assert_eq!(rdr.binary_byte_offset(), None); } diff --git a/grep-searcher/src/lines.rs b/grep-searcher/src/lines.rs index a32f6b72..57680b40 100644 --- a/grep-searcher/src/lines.rs +++ b/grep-searcher/src/lines.rs @@ -2,7 +2,7 @@ A collection of routines for performing operations on lines. */ -use bstr::B; +use bstr::ByteSlice; use bytecount; use grep_matcher::{LineTerminator, Match}; @@ -85,7 +85,7 @@ impl LineStep { #[inline(always)] fn next_impl(&mut self, mut bytes: &[u8]) -> Option<(usize, usize)> { bytes = &bytes[..self.end]; - match B(&bytes[self.pos..]).find_byte(self.line_term) { + match bytes[self.pos..].find_byte(self.line_term) { None => { if self.pos < bytes.len() { let m = (self.pos, bytes.len()); @@ -135,14 +135,14 @@ pub fn locate( line_term: u8, range: Match, ) -> Match { - let line_start = B(&bytes[..range.start()]) + let line_start = bytes[..range.start()] .rfind_byte(line_term) .map_or(0, |i| i + 1); let line_end = if range.end() > line_start && bytes[range.end() - 1] == line_term { range.end() } else { - B(&bytes[range.end()..]) + bytes[range.end()..] .find_byte(line_term) .map_or(bytes.len(), |i| range.end() + i + 1) }; @@ -182,7 +182,7 @@ fn preceding_by_pos( pos -= 1; } loop { - match B(&bytes[..pos]).rfind_byte(line_term) { + match bytes[..pos].rfind_byte(line_term) { None => { return 0; } diff --git a/grep-searcher/src/searcher/core.rs b/grep-searcher/src/searcher/core.rs index dd621bba..d534b663 100644 --- a/grep-searcher/src/searcher/core.rs +++ b/grep-searcher/src/searcher/core.rs @@ -1,6 +1,6 @@ use std::cmp; -use bstr::B; +use bstr::ByteSlice; use grep_matcher::{LineMatchKind, Matcher}; use lines::{self, LineStep}; @@ -161,7 +161,7 @@ impl<'s, M: Matcher, S: Sink> Core<'s, M, S> { BinaryDetection::Convert(b) => b, _ => return Ok(false), }; - if let Some(i) = B(&buf[*range]).find_byte(binary_byte) { + if let Some(i) = buf[*range].find_byte(binary_byte) { let offset = range.start() + i; self.binary_byte_offset = Some(offset); if !self.binary_data(offset as u64)? { diff --git a/grep-searcher/src/testutil.rs b/grep-searcher/src/testutil.rs index 416e4e32..ec7e29ba 100644 --- a/grep-searcher/src/testutil.rs +++ b/grep-searcher/src/testutil.rs @@ -1,7 +1,7 @@ use std::io::{self, Write}; use std::str; -use bstr::B; +use bstr::ByteSlice; use grep_matcher::{ LineMatchKind, LineTerminator, Match, Matcher, NoCaptures, NoError, }; @@ -94,7 +94,7 @@ impl Matcher for RegexMatcher { } // Make it interesting and return the last byte in the current // line. - let i = B(haystack) + let i = haystack .find_byte(self.line_term.unwrap().as_byte()) .map(|i| i) .unwrap_or(haystack.len() - 1); |