Skip to content

Commit 017450a

Browse files
committed
std: replace str::find_str* with a method
1 parent 7281fb9 commit 017450a

File tree

7 files changed

+54
-125
lines changed

7 files changed

+54
-125
lines changed

src/compiletest/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
3131
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
3232
let error_tag = ~"//~";
3333
let mut idx;
34-
match str::find_str(line, error_tag) {
34+
match line.find_str(error_tag) {
3535
None => return ~[],
3636
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
3737
}

src/compiletest/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn parse_name_directive(line: &str, directive: &str) -> bool {
175175
fn parse_name_value_directive(line: &str,
176176
directive: ~str) -> Option<~str> {
177177
let keycolon = directive + ":";
178-
match str::find_str(line, keycolon) {
178+
match line.find_str(keycolon) {
179179
Some(colon) => {
180180
let value = line.slice(colon + keycolon.len(),
181181
line.len()).to_owned();

src/libextra/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ fn should_sort_failures_before_printing_them() {
407407
print_failures(st);
408408
};
409409
410-
let apos = str::find_str(s, "a").get();
411-
let bpos = str::find_str(s, "b").get();
410+
let apos = s.find_str("a").get();
411+
let bpos = s.find_str("b").get();
412412
assert!(apos < bpos);
413413
}
414414

src/librustdoc/markdown_pass.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,10 @@ mod test {
633633
fn d() { }"
634634
);
635635
636-
let idx_a = str::find_str(markdown, "# Module `a`").get();
637-
let idx_b = str::find_str(markdown, "## Function `b`").get();
638-
let idx_c = str::find_str(markdown, "# Module `c`").get();
639-
let idx_d = str::find_str(markdown, "## Function `d`").get();
636+
let idx_a = markdown.find_str("# Module `a`").get();
637+
let idx_b = markdown.find_str("## Function `b`").get();
638+
let idx_c = markdown.find_str("# Module `c`").get();
639+
let idx_d = markdown.find_str("## Function `d`").get();
640640

641641
assert!(idx_b < idx_d);
642642
assert!(idx_d < idx_a);

src/libstd/str.rs

Lines changed: 44 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,88 +1166,6 @@ fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
11661166
return true;
11671167
}
11681168

1169-
/**
1170-
* Returns the byte index of the first matching substring
1171-
*
1172-
* # Arguments
1173-
*
1174-
* * `haystack` - The string to search
1175-
* * `needle` - The string to search for
1176-
*
1177-
* # Return value
1178-
*
1179-
* An `option` containing the byte index of the first matching substring
1180-
* or `none` if there is no match
1181-
*/
1182-
pub fn find_str<'a,'b>(haystack: &'a str, needle: &'b str) -> Option<uint> {
1183-
find_str_between(haystack, needle, 0u, haystack.len())
1184-
}
1185-
1186-
/**
1187-
* Returns the byte index of the first matching substring beginning
1188-
* from a given byte offset
1189-
*
1190-
* # Arguments
1191-
*
1192-
* * `haystack` - The string to search
1193-
* * `needle` - The string to search for
1194-
* * `start` - The byte index to begin searching at, inclusive
1195-
*
1196-
* # Return value
1197-
*
1198-
* An `option` containing the byte index of the last matching character
1199-
* or `none` if there is no match
1200-
*
1201-
* # Failure
1202-
*
1203-
* `start` must be less than or equal to `s.len()`
1204-
*/
1205-
pub fn find_str_from<'a,'b>(haystack: &'a str,
1206-
needle: &'b str,
1207-
start: uint)
1208-
-> Option<uint> {
1209-
find_str_between(haystack, needle, start, haystack.len())
1210-
}
1211-
1212-
/**
1213-
* Returns the byte index of the first matching substring within a given range
1214-
*
1215-
* # Arguments
1216-
*
1217-
* * `haystack` - The string to search
1218-
* * `needle` - The string to search for
1219-
* * `start` - The byte index to begin searching at, inclusive
1220-
* * `end` - The byte index to end searching at, exclusive
1221-
*
1222-
* # Return value
1223-
*
1224-
* An `option` containing the byte index of the first matching character
1225-
* or `none` if there is no match
1226-
*
1227-
* # Failure
1228-
*
1229-
* `start` must be less than or equal to `end` and `end` must be less than
1230-
* or equal to `s.len()`.
1231-
*/
1232-
pub fn find_str_between<'a,'b>(haystack: &'a str,
1233-
needle: &'b str,
1234-
start: uint,
1235-
end:uint)
1236-
-> Option<uint> {
1237-
// See Issue #1932 for why this is a naive search
1238-
assert!(end <= haystack.len());
1239-
let needle_len = needle.len();
1240-
if needle_len == 0u { return Some(start); }
1241-
if needle_len > end { return None; }
1242-
1243-
let mut i = start;
1244-
let e = end - needle_len;
1245-
while i <= e {
1246-
if match_at(haystack, needle, i) { return Some(i); }
1247-
i += 1u;
1248-
}
1249-
return None;
1250-
}
12511169

12521170
/**
12531171
* Returns true if one string contains another
@@ -1258,7 +1176,7 @@ pub fn find_str_between<'a,'b>(haystack: &'a str,
12581176
* * needle - The string to look for
12591177
*/
12601178
pub fn contains<'a,'b>(haystack: &'a str, needle: &'b str) -> bool {
1261-
find_str(haystack, needle).is_some()
1179+
haystack.find_str(needle).is_some()
12621180
}
12631181

12641182
/**
@@ -2096,6 +2014,7 @@ pub trait StrSlice<'self> {
20962014
20972015
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
20982016
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
2017+
fn find_str(&self, &str) -> Option<uint>;
20992018
}
21002019
21012020
/// Extension methods for strings
@@ -2341,6 +2260,28 @@ impl<'self> StrSlice<'self> for &'self str {
23412260
23422261
None
23432262
}
2263+
2264+
/**
2265+
* Returns the byte index of the first matching substring
2266+
*
2267+
* # Arguments
2268+
*
2269+
* * `needle` - The string to search for
2270+
*
2271+
* # Return value
2272+
*
2273+
* `Some` containing the byte index of the first matching substring
2274+
* or `None` if there is no match
2275+
*/
2276+
fn find_str(&self, needle: &str) -> Option<uint> {
2277+
if needle.is_empty() {
2278+
Some(0)
2279+
} else {
2280+
self.matches_index_iter(needle)
2281+
.next()
2282+
.map_consume(|(start, _end)| start)
2283+
}
2284+
}
23442285
}
23452286
23462287
#[allow(missing_doc)]
@@ -2550,43 +2491,31 @@ mod tests {
25502491
#[test]
25512492
fn test_find_str() {
25522493
// byte positions
2553-
assert!(find_str("banana", "apple pie").is_none());
2554-
assert_eq!(find_str("", ""), Some(0u));
2555-
2556-
let data = "ประเทศไทย中华Việt Nam";
2557-
assert_eq!(find_str(data, ""), Some(0u));
2558-
assert_eq!(find_str(data, "ประเ"), Some( 0u));
2559-
assert_eq!(find_str(data, "ะเ"), Some( 6u));
2560-
assert_eq!(find_str(data, "中华"), Some(27u));
2561-
assert!(find_str(data, "ไท华").is_none());
2562-
}
2563-
2564-
#[test]
2565-
fn test_find_str_between() {
2566-
// byte positions
2567-
assert_eq!(find_str_between("", "", 0u, 0u), Some(0u));
2494+
assert_eq!("".find_str(""), Some(0u));
2495+
assert!("banana".find_str("apple pie").is_none());
25682496
25692497
let data = "abcabc";
2570-
assert_eq!(find_str_between(data, "ab", 0u, 6u), Some(0u));
2571-
assert_eq!(find_str_between(data, "ab", 2u, 6u), Some(3u));
2572-
assert!(find_str_between(data, "ab", 2u, 4u).is_none());
2498+
assert_eq!(data.slice(0u, 6u).find_str("ab"), Some(0u));
2499+
assert_eq!(data.slice(2u, 6u).find_str("ab"), Some(3u));
2500+
assert!(data.slice(2u, 4u).find_str("ab").is_none());
25732501
25742502
let mut data = ~"ประเทศไทย中华Việt Nam";
25752503
data = data + data;
2576-
assert_eq!(find_str_between(data, "", 0u, 43u), Some(0u));
2577-
assert_eq!(find_str_between(data, "", 6u, 43u), Some(6u));
2578-
2579-
assert_eq!(find_str_between(data, "ประ", 0u, 43u), Some( 0u));
2580-
assert_eq!(find_str_between(data, "ทศไ", 0u, 43u), Some(12u));
2581-
assert_eq!(find_str_between(data, "ย中", 0u, 43u), Some(24u));
2582-
assert_eq!(find_str_between(data, "iệt", 0u, 43u), Some(34u));
2583-
assert_eq!(find_str_between(data, "Nam", 0u, 43u), Some(40u));
2584-
2585-
assert_eq!(find_str_between(data, "ประ", 43u, 86u), Some(43u));
2586-
assert_eq!(find_str_between(data, "ทศไ", 43u, 86u), Some(55u));
2587-
assert_eq!(find_str_between(data, "ย中", 43u, 86u), Some(67u));
2588-
assert_eq!(find_str_between(data, "iệt", 43u, 86u), Some(77u));
2589-
assert_eq!(find_str_between(data, "Nam", 43u, 86u), Some(83u));
2504+
assert!(data.find_str("ไท华").is_none());
2505+
assert_eq!(data.slice(0u, 43u).find_str(""), Some(0u));
2506+
assert_eq!(data.slice(6u, 43u).find_str(""), Some(6u - 6u));
2507+
2508+
assert_eq!(data.slice(0u, 43u).find_str("ประ"), Some( 0u));
2509+
assert_eq!(data.slice(0u, 43u).find_str("ทศไ"), Some(12u));
2510+
assert_eq!(data.slice(0u, 43u).find_str("ย中"), Some(24u));
2511+
assert_eq!(data.slice(0u, 43u).find_str("iệt"), Some(34u));
2512+
assert_eq!(data.slice(0u, 43u).find_str("Nam"), Some(40u));
2513+
2514+
assert_eq!(data.slice(43u, 86u).find_str("ประ"), Some(43u - 43u));
2515+
assert_eq!(data.slice(43u, 86u).find_str("ทศไ"), Some(55u - 43u));
2516+
assert_eq!(data.slice(43u, 86u).find_str("ย中"), Some(67u - 43u));
2517+
assert_eq!(data.slice(43u, 86u).find_str("iệt"), Some(77u - 43u));
2518+
assert_eq!(data.slice(43u, 86u).find_str("Nam"), Some(83u - 43u));
25902519
}
25912520
25922521
#[test]

src/test/bench/shootout-k-nucleotide-pipes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn main() {
197197
198198
// start processing if this is the one
199199
('>', false) => {
200-
match str::find_str_from(line, ~"THREE", 1u) {
200+
match line.slice_from(1).find_str(~"THREE") {
201201
option::Some(_) => { proc_mode = true; }
202202
option::None => { }
203203
}

src/test/run-pass/option-ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::str;
1212

1313
pub fn main() {
1414
let thing = ~"{{ f }}";
15-
let f = str::find_str(thing, ~"{{");
15+
let f = thing.find_str(~"{{");
1616
1717
if f.is_none() {
1818
println(~"None!");

0 commit comments

Comments
 (0)