(core::str) rename find_chars -> find
This commit is contained in:
@@ -69,9 +69,8 @@ export
|
|||||||
// Searching
|
// Searching
|
||||||
index,
|
index,
|
||||||
rindex,
|
rindex,
|
||||||
//find,
|
find,
|
||||||
find_bytes,
|
find_bytes,
|
||||||
find_chars,
|
|
||||||
contains,
|
contains,
|
||||||
starts_with,
|
starts_with,
|
||||||
ends_with,
|
ends_with,
|
||||||
@@ -877,58 +876,40 @@ fn rindex(ss: str, cc: char) -> option<uint> {
|
|||||||
ret option::none;
|
ret option::none;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
//Function: find_bytes
|
||||||
Function: find
|
//
|
||||||
|
// Find the char position of the first instance of one string
|
||||||
|
// within another, or return option::none
|
||||||
|
fn find_bytes(haystack: str, needle: str) -> option<uint> {
|
||||||
|
let haystack_len = len_bytes(haystack);
|
||||||
|
let needle_len = len_bytes(needle);
|
||||||
|
|
||||||
Finds the index of the first matching substring.
|
if needle_len == 0u { ret option::some(0u); }
|
||||||
Returns -1 if `haystack` does not contain `needle`.
|
if needle_len > haystack_len { ret option::none; }
|
||||||
|
|
||||||
Parameters:
|
fn match_at(haystack: str, needle: str, ii: uint) -> bool {
|
||||||
|
let jj = ii;
|
||||||
haystack - The string to look in
|
for c: u8 in needle { if haystack[jj] != c { ret false; } jj += 1u; }
|
||||||
needle - The string to look for
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
|
|
||||||
The index of the first occurance of `needle`, or -1 if not found.
|
|
||||||
|
|
||||||
FIXME: return an option<char position uint> instead
|
|
||||||
*/
|
|
||||||
fn find(haystack: str, needle: str) -> int {
|
|
||||||
let haystack_len: int = len_bytes(haystack) as int;
|
|
||||||
let needle_len: int = len_bytes(needle) as int;
|
|
||||||
if needle_len == 0 { ret 0; }
|
|
||||||
fn match_at(haystack: str, needle: str, i: int) -> bool {
|
|
||||||
let j: int = i;
|
|
||||||
for c: u8 in needle { if haystack[j] != c { ret false; } j += 1; }
|
|
||||||
ret true;
|
ret true;
|
||||||
}
|
}
|
||||||
let i: int = 0;
|
|
||||||
while i <= haystack_len - needle_len {
|
let ii = 0u;
|
||||||
if match_at(haystack, needle, i) { ret i; }
|
while ii <= haystack_len - needle_len {
|
||||||
i += 1;
|
if match_at(haystack, needle, ii) { ret option::some(ii); }
|
||||||
|
ii += 1u;
|
||||||
}
|
}
|
||||||
ret -1;
|
|
||||||
|
ret option::none;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function: find_chars
|
// Function: find
|
||||||
//
|
//
|
||||||
// Find the character position of the first instance of the substring,
|
// Find the char position of the first instance of one string
|
||||||
// or return option::none
|
// within another, or return option::none
|
||||||
//
|
fn find(haystack: str, needle: str) -> option<uint> {
|
||||||
// FIXME: rename find_chars -> find,
|
alt find_bytes(haystack, needle) {
|
||||||
// find -> find_bytes
|
|
||||||
fn find_chars(hay: str, pin: str) -> option<uint> {
|
|
||||||
alt find_bytes(hay, pin) {
|
|
||||||
option::none { ret option::none; }
|
option::none { ret option::none; }
|
||||||
option::some(nn) { ret option::some(b2c_pos(hay, nn)); }
|
option::some(nn) { ret option::some(b2c_pos(haystack, nn)); }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_bytes(hay: str, pin: str) -> option<uint> {
|
|
||||||
alt find(hay, pin) {
|
|
||||||
-1 { ret option::none; }
|
|
||||||
nn { ret option::some(nn as uint); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1741,19 +1722,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_bytes() {
|
fn test_find_bytes() {
|
||||||
fn t(haystack: str, needle: str, i: int) {
|
// byte positions
|
||||||
let j: int = find(haystack, needle);
|
|
||||||
log(debug, "searched for " + needle);
|
|
||||||
log(debug, j);
|
|
||||||
assert (i == j);
|
|
||||||
}
|
|
||||||
t("this is a simple", "is a", 5);
|
|
||||||
t("this is a simple", "is z", -1);
|
|
||||||
t("this is a simple", "", 0);
|
|
||||||
t("this is a simple", "simple", 10);
|
|
||||||
t("this", "simple", -1);
|
|
||||||
|
|
||||||
let data = "ประเทศไทย中华Việt Nam";
|
let data = "ประเทศไทย中华Việt Nam";
|
||||||
|
assert (find_bytes(data, "") == option::some(0u));
|
||||||
assert (find_bytes(data, "ประเ") == option::some( 0u));
|
assert (find_bytes(data, "ประเ") == option::some( 0u));
|
||||||
assert (find_bytes(data, "ะเ") == option::some( 6u));
|
assert (find_bytes(data, "ะเ") == option::some( 6u));
|
||||||
assert (find_bytes(data, "中华") == option::some(27u));
|
assert (find_bytes(data, "中华") == option::some(27u));
|
||||||
@@ -1761,12 +1732,14 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_chars() {
|
fn test_find() {
|
||||||
|
// char positions
|
||||||
let data = "ประเทศไทย中华Việt Nam";
|
let data = "ประเทศไทย中华Việt Nam";
|
||||||
assert (find_chars(data, "ประเ") == option::some(0u));
|
assert (find(data, "") == option::some(0u));
|
||||||
assert (find_chars(data, "ะเ") == option::some(2u));
|
assert (find(data, "ประเ") == option::some(0u));
|
||||||
assert (find_chars(data, "中华") == option::some(9u));
|
assert (find(data, "ะเ") == option::some(2u));
|
||||||
assert (find_chars(data, "ไท华") == option::none);
|
assert (find(data, "中华") == option::some(9u));
|
||||||
|
assert (find(data, "ไท华") == option::none);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user