Skip to content

Commit 32ce40a

Browse files
committed
---
yaml --- r: 14261 b: refs/heads/try c: 07ef368 h: refs/heads/master i: 14259: fe9175c v: v3
1 parent 2e3cabc commit 32ce40a

File tree

2 files changed

+38
-65
lines changed

2 files changed

+38
-65
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: c81867474a2cac8fcb646390ae5f3782dda45aae
5+
refs/heads/try: 07ef368c60769be5290fa875ca020f3b2b07d02a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/str.rs

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ export
6969
// Searching
7070
index,
7171
rindex,
72-
//find,
72+
find,
7373
find_bytes,
74-
find_chars,
7574
contains,
7675
starts_with,
7776
ends_with,
@@ -877,58 +876,40 @@ fn rindex(ss: str, cc: char) -> option<uint> {
877876
ret option::none;
878877
}
879878

880-
/*
881-
Function: find
882-
883-
Finds the index of the first matching substring.
884-
Returns -1 if `haystack` does not contain `needle`.
885-
886-
Parameters:
887-
888-
haystack - The string to look in
889-
needle - The string to look for
890-
891-
Returns:
892-
893-
The index of the first occurance of `needle`, or -1 if not found.
894-
895-
FIXME: return an option<char position uint> instead
896-
*/
897-
fn find(haystack: str, needle: str) -> int {
898-
let haystack_len: int = len_bytes(haystack) as int;
899-
let needle_len: int = len_bytes(needle) as int;
900-
if needle_len == 0 { ret 0; }
901-
fn match_at(haystack: str, needle: str, i: int) -> bool {
902-
let j: int = i;
903-
for c: u8 in needle { if haystack[j] != c { ret false; } j += 1; }
879+
//Function: find_bytes
880+
//
881+
// Find the char position of the first instance of one string
882+
// within another, or return option::none
883+
fn find_bytes(haystack: str, needle: str) -> option<uint> {
884+
let haystack_len = len_bytes(haystack);
885+
let needle_len = len_bytes(needle);
886+
887+
if needle_len == 0u { ret option::some(0u); }
888+
if needle_len > haystack_len { ret option::none; }
889+
890+
fn match_at(haystack: str, needle: str, ii: uint) -> bool {
891+
let jj = ii;
892+
for c: u8 in needle { if haystack[jj] != c { ret false; } jj += 1u; }
904893
ret true;
905894
}
906-
let i: int = 0;
907-
while i <= haystack_len - needle_len {
908-
if match_at(haystack, needle, i) { ret i; }
909-
i += 1;
895+
896+
let ii = 0u;
897+
while ii <= haystack_len - needle_len {
898+
if match_at(haystack, needle, ii) { ret option::some(ii); }
899+
ii += 1u;
910900
}
911-
ret -1;
901+
902+
ret option::none;
912903
}
913904

914-
// Function: find_chars
905+
// Function: find
915906
//
916-
// Find the character position of the first instance of the substring,
917-
// or return option::none
918-
//
919-
// FIXME: rename find_chars -> find,
920-
// find -> find_bytes
921-
fn find_chars(hay: str, pin: str) -> option<uint> {
922-
alt find_bytes(hay, pin) {
907+
// Find the char position of the first instance of one string
908+
// within another, or return option::none
909+
fn find(haystack: str, needle: str) -> option<uint> {
910+
alt find_bytes(haystack, needle) {
923911
option::none { ret option::none; }
924-
option::some(nn) { ret option::some(b2c_pos(hay, nn)); }
925-
}
926-
}
927-
928-
fn find_bytes(hay: str, pin: str) -> option<uint> {
929-
alt find(hay, pin) {
930-
-1 { ret option::none; }
931-
nn { ret option::some(nn as uint); }
912+
option::some(nn) { ret option::some(b2c_pos(haystack, nn)); }
932913
}
933914
}
934915

@@ -1741,32 +1722,24 @@ mod tests {
17411722

17421723
#[test]
17431724
fn test_find_bytes() {
1744-
fn t(haystack: str, needle: str, i: int) {
1745-
let j: int = find(haystack, needle);
1746-
log(debug, "searched for " + needle);
1747-
log(debug, j);
1748-
assert (i == j);
1749-
}
1750-
t("this is a simple", "is a", 5);
1751-
t("this is a simple", "is z", -1);
1752-
t("this is a simple", "", 0);
1753-
t("this is a simple", "simple", 10);
1754-
t("this", "simple", -1);
1755-
1725+
// byte positions
17561726
let data = "ประเทศไทย中华Việt Nam";
1727+
assert (find_bytes(data, "") == option::some(0u));
17571728
assert (find_bytes(data, "ประเ") == option::some( 0u));
17581729
assert (find_bytes(data, "ะเ") == option::some( 6u));
17591730
assert (find_bytes(data, "中华") == option::some(27u));
17601731
assert (find_bytes(data, "ไท华") == option::none);
17611732
}
17621733

17631734
#[test]
1764-
fn test_find_chars() {
1735+
fn test_find() {
1736+
// char positions
17651737
let data = "ประเทศไทย中华Việt Nam";
1766-
assert (find_chars(data, "ประเ") == option::some(0u));
1767-
assert (find_chars(data, "ะเ") == option::some(2u));
1768-
assert (find_chars(data, "中华") == option::some(9u));
1769-
assert (find_chars(data, "ไท华") == option::none);
1738+
assert (find(data, "") == option::some(0u));
1739+
assert (find(data, "ประเ") == option::some(0u));
1740+
assert (find(data, "ะเ") == option::some(2u));
1741+
assert (find(data, "中华") == option::some(9u));
1742+
assert (find(data, "ไท华") == option::none);
17701743
}
17711744

17721745
#[test]

0 commit comments

Comments
 (0)