Skip to content

Commit fe9175c

Browse files
committed
---
yaml --- r: 14259 b: refs/heads/try c: 748b63f h: refs/heads/master i: 14257: 5367734 14255: 5ee4033 v: v3
1 parent 0d49d85 commit fe9175c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
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: 0e61fe2eeaff507ed409c432988aed40a9fe9c63
5+
refs/heads/try: 748b63f63f4fb2ac8583900adb5a283990be276b
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/str.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,39 @@ fn find(haystack: str, needle: str) -> int {
908908
ret -1;
909909
}
910910

911+
// Function: find_chars
912+
//
913+
// Find the character position of the first instance of the substring,
914+
// or return option::none
915+
//
916+
// FIXME: rename find_chars -> find,
917+
// find -> find_bytes
918+
fn find_chars(hay: str, pin: str) -> option<uint> {
919+
alt find(hay, pin) {
920+
-1 { ret option::none; }
921+
n { ret option::some(b2c_pos(hay, n as uint)); }
922+
}
923+
}
924+
925+
// Function: b2c_pos
926+
//
927+
// Convert a byte position into a char position
928+
// within a given string
929+
fn b2c_pos(ss: str, bpos: uint) -> uint {
930+
assert bpos < len_bytes(ss);
931+
932+
let ii = 0u;
933+
let cpos = 0u;
934+
935+
while ii < bpos {
936+
let sz = utf8_char_width(ss[ii]);
937+
ii += sz;
938+
cpos += 1u;
939+
}
940+
941+
ret cpos;
942+
}
943+
911944
/*
912945
Function: contains
913946
@@ -1718,6 +1751,23 @@ mod tests {
17181751
assert (find(data, "ไท华") == -1);
17191752
}
17201753

1754+
#[test]
1755+
fn test_find_chars() {
1756+
let data = "ประเทศไทย中华Việt Nam";
1757+
assert (find_chars(data, "ประเ") == option::some(0u));
1758+
assert (find_chars(data, "ะเ") == option::some(2u));
1759+
assert (find_chars(data, "中华") == option::some(9u));
1760+
assert (find_chars(data, "ไท华") == option::none);
1761+
}
1762+
1763+
#[test]
1764+
fn test_b2c_pos() {
1765+
let data = "ประเทศไทย中华Việt Nam";
1766+
assert 0u == b2c_pos(data, 0u);
1767+
assert 2u == b2c_pos(data, 6u);
1768+
assert 9u == b2c_pos(data, 27u);
1769+
}
1770+
17211771
#[test]
17221772
fn test_substr() {
17231773
fn t(a: str, b: str, start: int) {

0 commit comments

Comments
 (0)