|
69 | 69 | // Searching
|
70 | 70 | index,
|
71 | 71 | rindex,
|
72 |
| - //find, |
| 72 | + find, |
73 | 73 | find_bytes,
|
74 |
| - find_chars, |
75 | 74 | contains,
|
76 | 75 | starts_with,
|
77 | 76 | ends_with,
|
@@ -877,58 +876,40 @@ fn rindex(ss: str, cc: char) -> option<uint> {
|
877 | 876 | ret option::none;
|
878 | 877 | }
|
879 | 878 |
|
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; } |
904 | 893 | ret true;
|
905 | 894 | }
|
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; |
910 | 900 | }
|
911 |
| - ret -1; |
| 901 | + |
| 902 | + ret option::none; |
912 | 903 | }
|
913 | 904 |
|
914 |
| -// Function: find_chars |
| 905 | +// Function: find |
915 | 906 | //
|
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) { |
923 | 911 | 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)); } |
932 | 913 | }
|
933 | 914 | }
|
934 | 915 |
|
@@ -1741,32 +1722,24 @@ mod tests {
|
1741 | 1722 |
|
1742 | 1723 | #[test]
|
1743 | 1724 | 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 |
1756 | 1726 | let data = "ประเทศไทย中华Việt Nam";
|
| 1727 | + assert (find_bytes(data, "") == option::some(0u)); |
1757 | 1728 | assert (find_bytes(data, "ประเ") == option::some( 0u));
|
1758 | 1729 | assert (find_bytes(data, "ะเ") == option::some( 6u));
|
1759 | 1730 | assert (find_bytes(data, "中华") == option::some(27u));
|
1760 | 1731 | assert (find_bytes(data, "ไท华") == option::none);
|
1761 | 1732 | }
|
1762 | 1733 |
|
1763 | 1734 | #[test]
|
1764 |
| - fn test_find_chars() { |
| 1735 | + fn test_find() { |
| 1736 | + // char positions |
1765 | 1737 | 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); |
1770 | 1743 | }
|
1771 | 1744 |
|
1772 | 1745 | #[test]
|
|
0 commit comments