std: replace str::find_str* with a method
This commit is contained in:
parent
7281fb948a
commit
017450a611
7 changed files with 52 additions and 123 deletions
|
@ -31,7 +31,7 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
|
||||||
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
|
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
|
||||||
let error_tag = ~"//~";
|
let error_tag = ~"//~";
|
||||||
let mut idx;
|
let mut idx;
|
||||||
match str::find_str(line, error_tag) {
|
match line.find_str(error_tag) {
|
||||||
None => return ~[],
|
None => return ~[],
|
||||||
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
|
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,7 +175,7 @@ fn parse_name_directive(line: &str, directive: &str) -> bool {
|
||||||
fn parse_name_value_directive(line: &str,
|
fn parse_name_value_directive(line: &str,
|
||||||
directive: ~str) -> Option<~str> {
|
directive: ~str) -> Option<~str> {
|
||||||
let keycolon = directive + ":";
|
let keycolon = directive + ":";
|
||||||
match str::find_str(line, keycolon) {
|
match line.find_str(keycolon) {
|
||||||
Some(colon) => {
|
Some(colon) => {
|
||||||
let value = line.slice(colon + keycolon.len(),
|
let value = line.slice(colon + keycolon.len(),
|
||||||
line.len()).to_owned();
|
line.len()).to_owned();
|
||||||
|
|
|
@ -407,8 +407,8 @@ fn should_sort_failures_before_printing_them() {
|
||||||
print_failures(st);
|
print_failures(st);
|
||||||
};
|
};
|
||||||
|
|
||||||
let apos = str::find_str(s, "a").get();
|
let apos = s.find_str("a").get();
|
||||||
let bpos = str::find_str(s, "b").get();
|
let bpos = s.find_str("b").get();
|
||||||
assert!(apos < bpos);
|
assert!(apos < bpos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -633,10 +633,10 @@ mod test {
|
||||||
fn d() { }"
|
fn d() { }"
|
||||||
);
|
);
|
||||||
|
|
||||||
let idx_a = str::find_str(markdown, "# Module `a`").get();
|
let idx_a = markdown.find_str("# Module `a`").get();
|
||||||
let idx_b = str::find_str(markdown, "## Function `b`").get();
|
let idx_b = markdown.find_str("## Function `b`").get();
|
||||||
let idx_c = str::find_str(markdown, "# Module `c`").get();
|
let idx_c = markdown.find_str("# Module `c`").get();
|
||||||
let idx_d = str::find_str(markdown, "## Function `d`").get();
|
let idx_d = markdown.find_str("## Function `d`").get();
|
||||||
|
|
||||||
assert!(idx_b < idx_d);
|
assert!(idx_b < idx_d);
|
||||||
assert!(idx_d < idx_a);
|
assert!(idx_d < idx_a);
|
||||||
|
|
|
@ -1166,88 +1166,6 @@ fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
|
||||||
return true;
|
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<uint> {
|
|
||||||
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<uint> {
|
|
||||||
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<uint> {
|
|
||||||
// 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
|
* 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
|
* * needle - The string to look for
|
||||||
*/
|
*/
|
||||||
pub fn contains<'a,'b>(haystack: &'a str, needle: &'b str) -> bool {
|
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<C: CharEq>(&self, search: C) -> Option<uint>;
|
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
|
||||||
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
|
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
|
||||||
|
fn find_str(&self, &str) -> Option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension methods for strings
|
/// Extension methods for strings
|
||||||
|
@ -2341,6 +2260,28 @@ impl<'self> StrSlice<'self> for &'self str {
|
||||||
|
|
||||||
None
|
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<uint> {
|
||||||
|
if needle.is_empty() {
|
||||||
|
Some(0)
|
||||||
|
} else {
|
||||||
|
self.matches_index_iter(needle)
|
||||||
|
.next()
|
||||||
|
.map_consume(|(start, _end)| start)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(missing_doc)]
|
#[allow(missing_doc)]
|
||||||
|
@ -2550,43 +2491,31 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_str() {
|
fn test_find_str() {
|
||||||
// byte positions
|
// byte positions
|
||||||
assert!(find_str("banana", "apple pie").is_none());
|
assert_eq!("".find_str(""), Some(0u));
|
||||||
assert_eq!(find_str("", ""), Some(0u));
|
assert!("banana".find_str("apple pie").is_none());
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
let data = "abcabc";
|
let data = "abcabc";
|
||||||
assert_eq!(find_str_between(data, "ab", 0u, 6u), Some(0u));
|
assert_eq!(data.slice(0u, 6u).find_str("ab"), Some(0u));
|
||||||
assert_eq!(find_str_between(data, "ab", 2u, 6u), Some(3u));
|
assert_eq!(data.slice(2u, 6u).find_str("ab"), Some(3u));
|
||||||
assert!(find_str_between(data, "ab", 2u, 4u).is_none());
|
assert!(data.slice(2u, 4u).find_str("ab").is_none());
|
||||||
|
|
||||||
let mut data = ~"ประเทศไทย中华Việt Nam";
|
let mut data = ~"ประเทศไทย中华Việt Nam";
|
||||||
data = data + data;
|
data = data + data;
|
||||||
assert_eq!(find_str_between(data, "", 0u, 43u), Some(0u));
|
assert!(data.find_str("ไท华").is_none());
|
||||||
assert_eq!(find_str_between(data, "", 6u, 43u), Some(6u));
|
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!(data.slice(0u, 43u).find_str("ประ"), Some( 0u));
|
||||||
assert_eq!(find_str_between(data, "ทศไ", 0u, 43u), Some(12u));
|
assert_eq!(data.slice(0u, 43u).find_str("ทศไ"), Some(12u));
|
||||||
assert_eq!(find_str_between(data, "ย中", 0u, 43u), Some(24u));
|
assert_eq!(data.slice(0u, 43u).find_str("ย中"), Some(24u));
|
||||||
assert_eq!(find_str_between(data, "iệt", 0u, 43u), Some(34u));
|
assert_eq!(data.slice(0u, 43u).find_str("iệt"), Some(34u));
|
||||||
assert_eq!(find_str_between(data, "Nam", 0u, 43u), Some(40u));
|
assert_eq!(data.slice(0u, 43u).find_str("Nam"), Some(40u));
|
||||||
|
|
||||||
assert_eq!(find_str_between(data, "ประ", 43u, 86u), Some(43u));
|
assert_eq!(data.slice(43u, 86u).find_str("ประ"), Some(43u - 43u));
|
||||||
assert_eq!(find_str_between(data, "ทศไ", 43u, 86u), Some(55u));
|
assert_eq!(data.slice(43u, 86u).find_str("ทศไ"), Some(55u - 43u));
|
||||||
assert_eq!(find_str_between(data, "ย中", 43u, 86u), Some(67u));
|
assert_eq!(data.slice(43u, 86u).find_str("ย中"), Some(67u - 43u));
|
||||||
assert_eq!(find_str_between(data, "iệt", 43u, 86u), Some(77u));
|
assert_eq!(data.slice(43u, 86u).find_str("iệt"), Some(77u - 43u));
|
||||||
assert_eq!(find_str_between(data, "Nam", 43u, 86u), Some(83u));
|
assert_eq!(data.slice(43u, 86u).find_str("Nam"), Some(83u - 43u));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -197,7 +197,7 @@ fn main() {
|
||||||
|
|
||||||
// start processing if this is the one
|
// start processing if this is the one
|
||||||
('>', false) => {
|
('>', false) => {
|
||||||
match str::find_str_from(line, ~"THREE", 1u) {
|
match line.slice_from(1).find_str(~"THREE") {
|
||||||
option::Some(_) => { proc_mode = true; }
|
option::Some(_) => { proc_mode = true; }
|
||||||
option::None => { }
|
option::None => { }
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::str;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let thing = ~"{{ f }}";
|
let thing = ~"{{ f }}";
|
||||||
let f = str::find_str(thing, ~"{{");
|
let f = thing.find_str(~"{{");
|
||||||
|
|
||||||
if f.is_none() {
|
if f.is_none() {
|
||||||
println(~"None!");
|
println(~"None!");
|
||||||
|
|
Loading…
Reference in a new issue