From 017450a6111a430eca86caf4ee6021ed654b552f Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Mon, 10 Jun 2013 16:23:05 +1000 Subject: [PATCH] std: replace str::find_str* with a method --- src/compiletest/errors.rs | 2 +- src/compiletest/header.rs | 2 +- src/libextra/test.rs | 4 +- src/librustdoc/markdown_pass.rs | 8 +- src/libstd/str.rs | 155 +++++------------- src/test/bench/shootout-k-nucleotide-pipes.rs | 2 +- src/test/run-pass/option-ext.rs | 2 +- 7 files changed, 52 insertions(+), 123 deletions(-) diff --git a/src/compiletest/errors.rs b/src/compiletest/errors.rs index 575df0268d6..f524f1424b4 100644 --- a/src/compiletest/errors.rs +++ b/src/compiletest/errors.rs @@ -31,7 +31,7 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] { fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] { let error_tag = ~"//~"; let mut idx; - match str::find_str(line, error_tag) { + match line.find_str(error_tag) { None => return ~[], Some(nn) => { idx = (nn as uint) + error_tag.len(); } } diff --git a/src/compiletest/header.rs b/src/compiletest/header.rs index c61adff0063..0adcb924a49 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -175,7 +175,7 @@ fn parse_name_directive(line: &str, directive: &str) -> bool { fn parse_name_value_directive(line: &str, directive: ~str) -> Option<~str> { let keycolon = directive + ":"; - match str::find_str(line, keycolon) { + match line.find_str(keycolon) { Some(colon) => { let value = line.slice(colon + keycolon.len(), line.len()).to_owned(); diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 6a9751b66e0..36d91382fb2 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -407,8 +407,8 @@ fn should_sort_failures_before_printing_them() { print_failures(st); }; - let apos = str::find_str(s, "a").get(); - let bpos = str::find_str(s, "b").get(); + let apos = s.find_str("a").get(); + let bpos = s.find_str("b").get(); assert!(apos < bpos); } diff --git a/src/librustdoc/markdown_pass.rs b/src/librustdoc/markdown_pass.rs index 7d41b899bc3..6b13c1fe1f6 100644 --- a/src/librustdoc/markdown_pass.rs +++ b/src/librustdoc/markdown_pass.rs @@ -633,10 +633,10 @@ mod test { fn d() { }" ); - let idx_a = str::find_str(markdown, "# Module `a`").get(); - let idx_b = str::find_str(markdown, "## Function `b`").get(); - let idx_c = str::find_str(markdown, "# Module `c`").get(); - let idx_d = str::find_str(markdown, "## Function `d`").get(); + let idx_a = markdown.find_str("# Module `a`").get(); + let idx_b = markdown.find_str("## Function `b`").get(); + let idx_c = markdown.find_str("# Module `c`").get(); + let idx_d = markdown.find_str("## Function `d`").get(); assert!(idx_b < idx_d); assert!(idx_d < idx_a); diff --git a/src/libstd/str.rs b/src/libstd/str.rs index e68b79575e0..98b210dd5a9 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1166,88 +1166,6 @@ fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool { return true; } -/** - * Returns the byte index of the first matching substring - * - * # Arguments - * - * * `haystack` - The string to search - * * `needle` - The string to search for - * - * # Return value - * - * An `option` containing the byte index of the first matching substring - * or `none` if there is no match - */ -pub fn find_str<'a,'b>(haystack: &'a str, needle: &'b str) -> Option { - find_str_between(haystack, needle, 0u, haystack.len()) -} - -/** - * Returns the byte index of the first matching substring beginning - * from a given byte offset - * - * # Arguments - * - * * `haystack` - The string to search - * * `needle` - The string to search for - * * `start` - The byte index to begin searching at, inclusive - * - * # Return value - * - * An `option` containing the byte index of the last matching character - * or `none` if there is no match - * - * # Failure - * - * `start` must be less than or equal to `s.len()` - */ -pub fn find_str_from<'a,'b>(haystack: &'a str, - needle: &'b str, - start: uint) - -> Option { - find_str_between(haystack, needle, start, haystack.len()) -} - -/** - * Returns the byte index of the first matching substring within a given range - * - * # Arguments - * - * * `haystack` - The string to search - * * `needle` - The string to search for - * * `start` - The byte index to begin searching at, inclusive - * * `end` - The byte index to end searching at, exclusive - * - * # Return value - * - * An `option` containing the byte index of the first matching character - * or `none` if there is no match - * - * # Failure - * - * `start` must be less than or equal to `end` and `end` must be less than - * or equal to `s.len()`. - */ -pub fn find_str_between<'a,'b>(haystack: &'a str, - needle: &'b str, - start: uint, - end:uint) - -> Option { - // See Issue #1932 for why this is a naive search - assert!(end <= haystack.len()); - let needle_len = needle.len(); - if needle_len == 0u { return Some(start); } - if needle_len > end { return None; } - - let mut i = start; - let e = end - needle_len; - while i <= e { - if match_at(haystack, needle, i) { return Some(i); } - i += 1u; - } - return None; -} /** * Returns true if one string contains another @@ -1258,7 +1176,7 @@ pub fn find_str_between<'a,'b>(haystack: &'a str, * * needle - The string to look for */ pub fn contains<'a,'b>(haystack: &'a str, needle: &'b str) -> bool { - find_str(haystack, needle).is_some() + haystack.find_str(needle).is_some() } /** @@ -2096,6 +2014,7 @@ pub trait StrSlice<'self> { fn find(&self, search: C) -> Option; fn rfind(&self, search: C) -> Option; + fn find_str(&self, &str) -> Option; } /// Extension methods for strings @@ -2341,6 +2260,28 @@ impl<'self> StrSlice<'self> for &'self str { None } + + /** + * Returns the byte index of the first matching substring + * + * # Arguments + * + * * `needle` - The string to search for + * + * # Return value + * + * `Some` containing the byte index of the first matching substring + * or `None` if there is no match + */ + fn find_str(&self, needle: &str) -> Option { + if needle.is_empty() { + Some(0) + } else { + self.matches_index_iter(needle) + .next() + .map_consume(|(start, _end)| start) + } + } } #[allow(missing_doc)] @@ -2550,43 +2491,31 @@ mod tests { #[test] fn test_find_str() { // byte positions - assert!(find_str("banana", "apple pie").is_none()); - assert_eq!(find_str("", ""), Some(0u)); - - let data = "ประเทศไทย中华Việt Nam"; - assert_eq!(find_str(data, ""), Some(0u)); - assert_eq!(find_str(data, "ประเ"), Some( 0u)); - assert_eq!(find_str(data, "ะเ"), Some( 6u)); - assert_eq!(find_str(data, "中华"), Some(27u)); - assert!(find_str(data, "ไท华").is_none()); - } - - #[test] - fn test_find_str_between() { - // byte positions - assert_eq!(find_str_between("", "", 0u, 0u), Some(0u)); + assert_eq!("".find_str(""), Some(0u)); + assert!("banana".find_str("apple pie").is_none()); let data = "abcabc"; - assert_eq!(find_str_between(data, "ab", 0u, 6u), Some(0u)); - assert_eq!(find_str_between(data, "ab", 2u, 6u), Some(3u)); - assert!(find_str_between(data, "ab", 2u, 4u).is_none()); + assert_eq!(data.slice(0u, 6u).find_str("ab"), Some(0u)); + assert_eq!(data.slice(2u, 6u).find_str("ab"), Some(3u)); + assert!(data.slice(2u, 4u).find_str("ab").is_none()); let mut data = ~"ประเทศไทย中华Việt Nam"; data = data + data; - assert_eq!(find_str_between(data, "", 0u, 43u), Some(0u)); - assert_eq!(find_str_between(data, "", 6u, 43u), Some(6u)); + assert!(data.find_str("ไท华").is_none()); + assert_eq!(data.slice(0u, 43u).find_str(""), Some(0u)); + assert_eq!(data.slice(6u, 43u).find_str(""), Some(6u - 6u)); - assert_eq!(find_str_between(data, "ประ", 0u, 43u), Some( 0u)); - assert_eq!(find_str_between(data, "ทศไ", 0u, 43u), Some(12u)); - assert_eq!(find_str_between(data, "ย中", 0u, 43u), Some(24u)); - assert_eq!(find_str_between(data, "iệt", 0u, 43u), Some(34u)); - assert_eq!(find_str_between(data, "Nam", 0u, 43u), Some(40u)); + assert_eq!(data.slice(0u, 43u).find_str("ประ"), Some( 0u)); + assert_eq!(data.slice(0u, 43u).find_str("ทศไ"), Some(12u)); + assert_eq!(data.slice(0u, 43u).find_str("ย中"), Some(24u)); + assert_eq!(data.slice(0u, 43u).find_str("iệt"), Some(34u)); + assert_eq!(data.slice(0u, 43u).find_str("Nam"), Some(40u)); - assert_eq!(find_str_between(data, "ประ", 43u, 86u), Some(43u)); - assert_eq!(find_str_between(data, "ทศไ", 43u, 86u), Some(55u)); - assert_eq!(find_str_between(data, "ย中", 43u, 86u), Some(67u)); - assert_eq!(find_str_between(data, "iệt", 43u, 86u), Some(77u)); - assert_eq!(find_str_between(data, "Nam", 43u, 86u), Some(83u)); + assert_eq!(data.slice(43u, 86u).find_str("ประ"), Some(43u - 43u)); + assert_eq!(data.slice(43u, 86u).find_str("ทศไ"), Some(55u - 43u)); + assert_eq!(data.slice(43u, 86u).find_str("ย中"), Some(67u - 43u)); + assert_eq!(data.slice(43u, 86u).find_str("iệt"), Some(77u - 43u)); + assert_eq!(data.slice(43u, 86u).find_str("Nam"), Some(83u - 43u)); } #[test] diff --git a/src/test/bench/shootout-k-nucleotide-pipes.rs b/src/test/bench/shootout-k-nucleotide-pipes.rs index ef32b0bb2dc..9ba9b9759fd 100644 --- a/src/test/bench/shootout-k-nucleotide-pipes.rs +++ b/src/test/bench/shootout-k-nucleotide-pipes.rs @@ -197,7 +197,7 @@ fn main() { // start processing if this is the one ('>', false) => { - match str::find_str_from(line, ~"THREE", 1u) { + match line.slice_from(1).find_str(~"THREE") { option::Some(_) => { proc_mode = true; } option::None => { } } diff --git a/src/test/run-pass/option-ext.rs b/src/test/run-pass/option-ext.rs index 7355bde61f3..f37f40935ab 100644 --- a/src/test/run-pass/option-ext.rs +++ b/src/test/run-pass/option-ext.rs @@ -12,7 +12,7 @@ use std::str; pub fn main() { let thing = ~"{{ f }}"; - let f = str::find_str(thing, ~"{{"); + let f = thing.find_str(~"{{"); if f.is_none() { println(~"None!");