Skip to content

Commit 8b248ac

Browse files
killerswanmarijnh
authored andcommitted
---
yaml --- r: 11437 b: refs/heads/master c: 2756a61 h: refs/heads/master i: 11435: d729c12 v: v3
1 parent 34e232f commit 8b248ac

File tree

2 files changed

+77
-16
lines changed

2 files changed

+77
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: e1d04e00621672a0e9f5424285f302077a973b37
2+
refs/heads/master: 2756a61e3442c378d10cf4e3bee3d35392623cec
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/str.rs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ export
7272
index_chars,
7373
byte_index,
7474
byte_index_from,
75+
//rindex,
7576
rindex_chars,
76-
find,
77+
find_chars,
7778
find_bytes,
7879
find_from_bytes,
7980
contains,
@@ -861,7 +862,36 @@ Section: Searching
861862

862863
// Function: index
863864
//
864-
// Returns the index of the first matching char
865+
// Returns the byte index of the first matching char
866+
// (as option some/none)
867+
fn index(ss: str, cc: char) -> option<uint> {
868+
index_from(ss, cc, 0u, len_bytes(ss))
869+
}
870+
871+
// Function: index_from
872+
//
873+
// Returns the byte index of the first matching char
874+
// (as option some/none), starting at `nn`
875+
fn index_from(ss: str, cc: char, start: uint, end: uint) -> option<uint> {
876+
let bii = start;
877+
while bii < end {
878+
let {ch, next} = char_range_at(ss, bii);
879+
880+
// found here?
881+
if ch == cc {
882+
ret some(bii);
883+
}
884+
885+
bii = next;
886+
}
887+
888+
// wasn't found
889+
ret none;
890+
}
891+
892+
// Function: index_chars
893+
//
894+
// Returns the char index of the first matching char
865895
// (as option some/none)
866896
fn index_chars(ss: str, cc: char) -> option<uint> {
867897
let bii = 0u;
@@ -887,6 +917,7 @@ fn index_chars(ss: str, cc: char) -> option<uint> {
887917
//
888918
// Returns the index of the first matching byte
889919
// (as option some/none)
920+
// FIXME: delete
890921
fn byte_index(s: str, b: u8) -> option<uint> {
891922
byte_index_from(s, b, 0u, len_bytes(s))
892923
}
@@ -896,15 +927,36 @@ fn byte_index(s: str, b: u8) -> option<uint> {
896927
// Returns the index of the first matching byte within the range [`start`,
897928
// `end`).
898929
// (as option some/none)
930+
// FIXME: delete
899931
fn byte_index_from(s: str, b: u8, start: uint, end: uint) -> option<uint> {
900932
assert end <= len_bytes(s);
901933

902934
str::as_bytes(s) { |v| vec::position_from(v, start, end) { |x| x == b } }
903935
}
904936

937+
// Function: rindex
938+
//
939+
// Returns the byte index of the first matching char
940+
// (as option some/none)
941+
fn rindex(ss: str, cc: char) -> option<uint> {
942+
let bii = len_bytes(ss);
943+
while bii > 0u {
944+
let {ch, prev} = char_range_at_reverse(ss, bii);
945+
bii = prev;
946+
947+
// found here?
948+
if ch == cc {
949+
ret some(bii);
950+
}
951+
}
952+
953+
// wasn't found
954+
ret none;
955+
}
956+
905957
// Function: rindex_chars
906958
//
907-
// Returns the index of the first matching char
959+
// Returns the char index of the first matching char
908960
// (as option some/none)
909961
fn rindex_chars(ss: str, cc: char) -> option<uint> {
910962
let bii = len_bytes(ss);
@@ -926,15 +978,15 @@ fn rindex_chars(ss: str, cc: char) -> option<uint> {
926978

927979
//Function: find_bytes
928980
//
929-
// Find the char position of the first instance of one string
981+
// Find the byte position of the first instance of one string
930982
// within another, or return option::none
931983
fn find_bytes(haystack: str, needle: str) -> option<uint> {
932984
find_from_bytes(haystack, needle, 0u, len_bytes(haystack))
933985
}
934986

935987
//Function: find_from_bytes
936988
//
937-
// Find the char position of the first instance of one string
989+
// Find the byte position of the first instance of one string
938990
// within another, or return option::none
939991
//
940992
// FIXME: Boyer-Moore should be significantly faster
@@ -962,11 +1014,11 @@ fn find_from_bytes(haystack: str, needle: str, start: uint, end:uint)
9621014
ret none;
9631015
}
9641016

965-
// Function: find
1017+
// Function: find_chars
9661018
//
9671019
// Find the char position of the first instance of one string
9681020
// within another, or return option::none
969-
fn find(haystack: str, needle: str) -> option<uint> {
1021+
fn find_chars(haystack: str, needle: str) -> option<uint> {
9701022
alt find_bytes(haystack, needle) {
9711023
none { ret none; }
9721024
some(nn) { ret some(b2c_pos(haystack, nn)); }
@@ -1570,6 +1622,15 @@ mod tests {
15701622
assert ( index_chars("hello", 'z') == none);
15711623
}
15721624

1625+
#[test]
1626+
fn test_rindex() {
1627+
assert rindex("hello", 'l') == some(3u);
1628+
assert rindex("hello", 'o') == some(4u);
1629+
assert rindex("hello", 'h') == some(0u);
1630+
assert rindex("hello", 'z') == none;
1631+
assert rindex("ประเทศไทย中华Việt Nam", '华') == some(30u);
1632+
}
1633+
15731634
#[test]
15741635
fn test_rindex_chars() {
15751636
assert (rindex_chars("hello", 'l') == some(3u));
@@ -1820,17 +1881,17 @@ mod tests {
18201881
}
18211882

18221883
#[test]
1823-
fn test_find() {
1884+
fn test_find_chars() {
18241885
// char positions
1825-
assert (find("banana", "apple pie") == none);
1826-
assert (find("", "") == some(0u));
1886+
assert (find_chars("banana", "apple pie") == none);
1887+
assert (find_chars("", "") == some(0u));
18271888

18281889
let data = "ประเทศไทย中华Việt Nam";
1829-
assert (find(data, "") == some(0u));
1830-
assert (find(data, "ประเ") == some(0u));
1831-
assert (find(data, "ะเ") == some(2u));
1832-
assert (find(data, "中华") == some(9u));
1833-
assert (find(data, "ไท华") == none);
1890+
assert (find_chars(data, "") == some(0u));
1891+
assert (find_chars(data, "ประเ") == some(0u));
1892+
assert (find_chars(data, "ะเ") == some(2u));
1893+
assert (find_chars(data, "中华") == some(9u));
1894+
assert (find_chars(data, "ไท华") == none);
18341895
}
18351896

18361897
#[test]

0 commit comments

Comments
 (0)