diff options
| author | Marvin Löbel <loebel.marvin@gmail.com> | 2015-02-17 23:47:08 +0100 |
|---|---|---|
| committer | Marvin Löbel <loebel.marvin@gmail.com> | 2015-02-20 00:58:07 +0100 |
| commit | a641996796f0ab11021671c0ce70a3c975bb4e37 (patch) | |
| tree | 5724986b305b79e56e25603688f8f62a37e5838d /src/libcore/str | |
| parent | c1de0a0f9ea9863407363ce31bb698e9988215ee (diff) | |
| download | rust-a641996796f0ab11021671c0ce70a3c975bb4e37.tar.gz rust-a641996796f0ab11021671c0ce70a3c975bb4e37.zip | |
Fix tidy and rebase fallout
Added a few bugfixes and additional testcases
Diffstat (limited to 'src/libcore/str')
| -rw-r--r-- | src/libcore/str/mod.rs | 15 | ||||
| -rw-r--r-- | src/libcore/str/pattern.rs | 27 |
2 files changed, 27 insertions, 15 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index a9308302033..820ad4d8586 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -156,7 +156,6 @@ impl FromStr for bool { /// An error returned when parsing a `bool` from a string fails. #[derive(Debug, Clone, PartialEq)] -#[allow(missing_copy_implementations)] #[stable(feature = "rust1", since = "1.0.0")] pub struct ParseBoolError { _priv: () } @@ -235,7 +234,7 @@ pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str { pub unsafe fn from_c_str(s: *const i8) -> &'static str { let s = s as *const u8; let mut len = 0; - while *s.offset(len as int) != 0 { + while *s.offset(len as isize) != 0 { len += 1; } let v: &'static [u8] = ::mem::transmute(Slice { data: s, len: len }); @@ -258,7 +257,7 @@ impl CharEq for char { fn matches(&mut self, c: char) -> bool { *self == c } #[inline] - fn only_ascii(&self) -> bool { (*self as usize) < 128 } + fn only_ascii(&self) -> bool { (*self as u32) < 128 } } impl<F> CharEq for F where F: FnMut(char) -> bool { @@ -764,7 +763,8 @@ impl TwoWaySearcher { // How far we can jump when we encounter a mismatch is all based on the fact // that (u, v) is a critical factorization for the needle. #[inline] - fn next(&mut self, haystack: &[u8], needle: &[u8], long_period: bool) -> Option<(usize, usize)> { + fn next(&mut self, haystack: &[u8], needle: &[u8], long_period: bool) + -> Option<(usize, usize)> { 'search: loop { // Check that we have room to search in if self.position + needle.len() > haystack.len() { @@ -955,6 +955,7 @@ Section: Comparing strings /// to compare &[u8] byte slices that are not necessarily valid UTF-8. #[inline] fn eq_slice_(a: &str, b: &str) -> bool { + // NOTE: In theory n should be libc::size_t and not usize, but libc is not available here #[allow(improper_ctypes)] extern { fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32; } a.len() == b.len() && unsafe { @@ -1489,7 +1490,7 @@ impl StrExt for str { fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str where P::Searcher: DoubleEndedSearcher<'a> { let mut i = 0; - let mut j = self.len(); + let mut j = 0; let mut matcher = pat.into_searcher(self); if let Some((a, b)) = matcher.next_reject() { i = a; @@ -1507,7 +1508,7 @@ impl StrExt for str { #[inline] fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str { - let mut i = 0; + let mut i = self.len(); let mut matcher = pat.into_searcher(self); if let Some((a, _)) = matcher.next_reject() { i = a; @@ -1521,7 +1522,7 @@ impl StrExt for str { #[inline] fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str where P::Searcher: ReverseSearcher<'a> { - let mut j = self.len(); + let mut j = 0; let mut matcher = pat.into_searcher(self); if let Some((_, b)) = matcher.next_reject_back() { j = b; diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs index 501fc27b376..9cd5510db37 100644 --- a/src/libcore/str/pattern.rs +++ b/src/libcore/str/pattern.rs @@ -58,6 +58,7 @@ pub trait Pattern<'a>: Sized { // Searcher +#[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum SearchStep { Match(usize, usize), Reject(usize, usize), @@ -190,7 +191,7 @@ impl<'a, C: CharEq> DoubleEndedSearcher<'a> for CharEqSearcher<'a, C> {} // Impl for &str -// TODO: Optimize the naive implementation here +// Todo: Optimize the naive implementation here #[derive(Clone)] pub struct StrSearcher<'a, 'b> { @@ -235,13 +236,16 @@ unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> { }, |m: &mut StrSearcher| { // Forward step for nonempty needle - let possible_match = &m.haystack[m.start .. m.start + m.needle.len()]; + // Compare if bytes are equal + let possible_match = &m.haystack.as_bytes()[m.start .. m.start + m.needle.len()]; let current_start = m.start; - if possible_match == m.needle { + if possible_match == m.needle.as_bytes() { m.start += m.needle.len(); SearchStep::Match(current_start, m.start) } else { - m.start += possible_match.chars().next().unwrap().len_utf8(); + // Skip a char + let haystack_suffix = &m.haystack[m.start..]; + m.start += haystack_suffix.chars().next().unwrap().len_utf8(); SearchStep::Reject(current_start, m.start) } }) @@ -262,13 +266,16 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for StrSearcher<'a, 'b> { }, |m: &mut StrSearcher| { // Backward step for nonempty needle - let possible_match = &m.haystack[m.end - m.needle.len() .. m.end]; + // Compare if bytes are equal + let possible_match = &m.haystack.as_bytes()[m.end - m.needle.len() .. m.end]; let current_end = m.end; - if possible_match == m.needle { + if possible_match == m.needle.as_bytes() { m.end -= m.needle.len(); SearchStep::Match(m.end, current_end) } else { - m.end -= possible_match.chars().rev().next().unwrap().len_utf8(); + // Skip a char + let haystack_prefix = &m.haystack[..m.end]; + m.end -= haystack_prefix.chars().rev().next().unwrap().len_utf8(); SearchStep::Reject(m.end, current_end) } }) @@ -290,6 +297,9 @@ where F: FnOnce(&mut StrSearcher) -> SearchStep, } else if m.start + m.needle.len() <= m.end { // Case for needle != "" g(&mut m) + } else if m.start < m.end { + m.done = true; + SearchStep::Reject(m.start, m.end) } else { m.done = true; SearchStep::Done @@ -352,7 +362,8 @@ impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool { use ops::Deref; -impl<'a, 'b, P: 'b + ?Sized, T: Deref<Target = P> + ?Sized> Pattern<'a> for &'b T where &'b P: Pattern<'a> { +impl<'a, 'b, P: 'b + ?Sized, T: Deref<Target = P> + ?Sized> Pattern<'a> for &'b T +where &'b P: Pattern<'a> { type Searcher = <&'b P as Pattern<'a>>::Searcher; associated_items!(<&'b P as Pattern<'a>>::Searcher, s, (&**s)); |
