(core::str) add find_chars and b2c_pos functions
This commit is contained in:
parent
0e61fe2eea
commit
748b63f63f
1 changed files with 50 additions and 0 deletions
|
@ -908,6 +908,39 @@ fn find(haystack: str, needle: str) -> int {
|
||||||
ret -1;
|
ret -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function: find_chars
|
||||||
|
//
|
||||||
|
// Find the character position of the first instance of the substring,
|
||||||
|
// or return option::none
|
||||||
|
//
|
||||||
|
// FIXME: rename find_chars -> find,
|
||||||
|
// find -> find_bytes
|
||||||
|
fn find_chars(hay: str, pin: str) -> option<uint> {
|
||||||
|
alt find(hay, pin) {
|
||||||
|
-1 { ret option::none; }
|
||||||
|
n { ret option::some(b2c_pos(hay, n as uint)); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function: b2c_pos
|
||||||
|
//
|
||||||
|
// Convert a byte position into a char position
|
||||||
|
// within a given string
|
||||||
|
fn b2c_pos(ss: str, bpos: uint) -> uint {
|
||||||
|
assert bpos < len_bytes(ss);
|
||||||
|
|
||||||
|
let ii = 0u;
|
||||||
|
let cpos = 0u;
|
||||||
|
|
||||||
|
while ii < bpos {
|
||||||
|
let sz = utf8_char_width(ss[ii]);
|
||||||
|
ii += sz;
|
||||||
|
cpos += 1u;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret cpos;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function: contains
|
Function: contains
|
||||||
|
|
||||||
|
@ -1718,6 +1751,23 @@ mod tests {
|
||||||
assert (find(data, "ไท华") == -1);
|
assert (find(data, "ไท华") == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_chars() {
|
||||||
|
let data = "ประเทศไทย中华Việt Nam";
|
||||||
|
assert (find_chars(data, "ประเ") == option::some(0u));
|
||||||
|
assert (find_chars(data, "ะเ") == option::some(2u));
|
||||||
|
assert (find_chars(data, "中华") == option::some(9u));
|
||||||
|
assert (find_chars(data, "ไท华") == option::none);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_b2c_pos() {
|
||||||
|
let data = "ประเทศไทย中华Việt Nam";
|
||||||
|
assert 0u == b2c_pos(data, 0u);
|
||||||
|
assert 2u == b2c_pos(data, 6u);
|
||||||
|
assert 9u == b2c_pos(data, 27u);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_substr() {
|
fn test_substr() {
|
||||||
fn t(a: str, b: str, start: int) {
|
fn t(a: str, b: str, start: int) {
|
||||||
|
|
Loading…
Reference in a new issue