(core::str) add find_bytes and export it...

This commit is contained in:
Kevin Cantu 2012-02-12 22:00:56 -08:00
parent 748b63f63f
commit c81867474a
9 changed files with 60 additions and 45 deletions

View file

@ -568,10 +568,11 @@ fn link_binary(sess: session,
// Converts a library file name into a cc -l argument // Converts a library file name into a cc -l argument
fn unlib(config: @session::config, filename: str) -> str unsafe { fn unlib(config: @session::config, filename: str) -> str unsafe {
let rmlib = fn@(filename: str) -> str { let rmlib = fn@(filename: str) -> str {
let found = str::find_bytes(filename, "lib");
if config.os == session::os_macos || if config.os == session::os_macos ||
(config.os == session::os_linux || (config.os == session::os_linux ||
config.os == session::os_freebsd) && config.os == session::os_freebsd) &&
str::find(filename, "lib") == 0 { option::is_some(found) && option::get(found) == 0u {
ret str::unsafe::slice_bytes(filename, 3u, ret str::unsafe::slice_bytes(filename, 3u,
str::len_bytes(filename)); str::len_bytes(filename));
} else { ret filename; } } else { ret filename; }

View file

@ -269,28 +269,28 @@ fn pretty_print_input(sess: session, cfg: ast::crate_cfg, input: str,
} }
fn get_os(triple: str) -> option<session::os> { fn get_os(triple: str) -> option<session::os> {
ret if str::find(triple, "win32") >= 0 || ret if str::contains(triple, "win32") ||
str::find(triple, "mingw32") >= 0 { str::contains(triple, "mingw32") {
some(session::os_win32) some(session::os_win32)
} else if str::find(triple, "darwin") >= 0 { } else if str::contains(triple, "darwin") {
some(session::os_macos) some(session::os_macos)
} else if str::find(triple, "linux") >= 0 { } else if str::contains(triple, "linux") {
some(session::os_linux) some(session::os_linux)
} else if str::find(triple, "freebsd") >= 0 { } else if str::contains(triple, "freebsd") {
some(session::os_freebsd) some(session::os_freebsd)
} else { none }; } else { none };
} }
fn get_arch(triple: str) -> option<session::arch> { fn get_arch(triple: str) -> option<session::arch> {
ret if str::find(triple, "i386") >= 0 || str::find(triple, "i486") >= 0 || ret if str::contains(triple, "i386") || str::contains(triple, "i486") ||
str::find(triple, "i586") >= 0 || str::contains(triple, "i586") ||
str::find(triple, "i686") >= 0 || str::contains(triple, "i686") ||
str::find(triple, "i786") >= 0 { str::contains(triple, "i786") {
some(session::arch_x86) some(session::arch_x86)
} else if str::find(triple, "x86_64") >= 0 { } else if str::contains(triple, "x86_64") {
some(session::arch_x86_64) some(session::arch_x86_64)
} else if str::find(triple, "arm") >= 0 || } else if str::contains(triple, "arm") ||
str::find(triple, "xscale") >= 0 { str::contains(triple, "xscale") {
some(session::arch_arm) some(session::arch_arm)
} else { none }; } else { none };
} }

View file

@ -24,9 +24,11 @@ fn load_errors(testfile: str) -> [expected_error] {
fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe { fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe {
let error_tag = "//!"; let error_tag = "//!";
let idx0 = str::find(line, error_tag); let idx;
if idx0 < 0 { ret []; } alt str::find_bytes(line, error_tag) {
let idx = (idx0 as uint) + str::len_bytes(error_tag); option::none { ret []; }
option::some(nn) { idx = (nn as uint) + str::len_bytes(error_tag); }
}
// "//!^^^ kind msg" denotes a message expected // "//!^^^ kind msg" denotes a message expected
// three lines above current line: // three lines above current line:

View file

@ -100,18 +100,21 @@ fn parse_pp_exact(line: str, testfile: str) -> option<str> {
} }
fn parse_name_directive(line: str, directive: str) -> bool { fn parse_name_directive(line: str, directive: str) -> bool {
str::find(line, directive) >= 0 str::contains(line, directive)
} }
fn parse_name_value_directive(line: str, fn parse_name_value_directive(line: str,
directive: str) -> option<str> unsafe { directive: str) -> option<str> unsafe {
let keycolon = directive + ":"; let keycolon = directive + ":";
if str::find(line, keycolon) >= 0 { alt str::find_bytes(line, keycolon) {
let colon = str::find(line, keycolon) as uint; option::some(colon) {
let value = let value =
str::unsafe::slice_bytes(line, colon + str::len_bytes(keycolon), str::unsafe::slice_bytes(line,
colon + str::len_bytes(keycolon),
str::len_bytes(line)); str::len_bytes(line));
#debug("%s: %s", directive, value); #debug("%s: %s", directive, value);
option::some(value) option::some(value)
} else { option::none } }
option::none { option::none }
}
} }

View file

@ -199,7 +199,7 @@ fn check_error_patterns(props: test_props,
let next_err_idx = 0u; let next_err_idx = 0u;
let next_err_pat = props.error_patterns[next_err_idx]; let next_err_pat = props.error_patterns[next_err_idx];
for line: str in str::split_byte(procres.stderr, '\n' as u8) { for line: str in str::split_byte(procres.stderr, '\n' as u8) {
if str::find(line, next_err_pat) > 0 { if str::contains(line, next_err_pat) {
#debug("found error pattern %s", next_err_pat); #debug("found error pattern %s", next_err_pat);
next_err_idx += 1u; next_err_idx += 1u;
if next_err_idx == vec::len(props.error_patterns) { if next_err_idx == vec::len(props.error_patterns) {

View file

@ -16,7 +16,7 @@ fn write_file(filename: str, content: str) {
} }
fn contains(haystack: str, needle: str) -> bool { fn contains(haystack: str, needle: str) -> bool {
str::find(haystack, needle) != -1 str::contains(haystack, needle)
} }
fn find_rust_files(&files: [str], path: str) { fn find_rust_files(&files: [str], path: str) {

View file

@ -69,7 +69,9 @@ export
// Searching // Searching
index, index,
rindex, rindex,
find, //find,
find_bytes,
find_chars,
contains, contains,
starts_with, starts_with,
ends_with, ends_with,
@ -663,9 +665,10 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe {
unsafe::slice_bytes(s, len_bytes(from), len_bytes(s)), unsafe::slice_bytes(s, len_bytes(from), len_bytes(s)),
from, to); from, to);
} else { } else {
let idx = find(s, from); let idx;
if idx == -1 { alt find_bytes(s, from) {
ret s; option::some(x) { idx = x; }
option::none { ret s; }
} }
let before = unsafe::slice_bytes(s, 0u, idx as uint); let before = unsafe::slice_bytes(s, 0u, idx as uint);
let after = unsafe::slice_bytes(s, idx as uint + len_bytes(from), let after = unsafe::slice_bytes(s, idx as uint + len_bytes(from),
@ -916,9 +919,16 @@ fn find(haystack: str, needle: str) -> int {
// FIXME: rename find_chars -> find, // FIXME: rename find_chars -> find,
// find -> find_bytes // find -> find_bytes
fn find_chars(hay: str, pin: str) -> option<uint> { fn find_chars(hay: str, pin: str) -> option<uint> {
alt find_bytes(hay, pin) {
option::none { ret option::none; }
option::some(nn) { ret option::some(b2c_pos(hay, nn)); }
}
}
fn find_bytes(hay: str, pin: str) -> option<uint> {
alt find(hay, pin) { alt find(hay, pin) {
-1 { ret option::none; } -1 { ret option::none; }
n { ret option::some(b2c_pos(hay, n as uint)); } nn { ret option::some(nn as uint); }
} }
} }
@ -952,7 +962,7 @@ haystack - The string to look in
needle - The string to look for needle - The string to look for
*/ */
fn contains(haystack: str, needle: str) -> bool { fn contains(haystack: str, needle: str) -> bool {
0 <= find(haystack, needle) option::is_some(find_bytes(haystack, needle))
} }
/* /*
@ -1730,7 +1740,7 @@ mod tests {
} }
#[test] #[test]
fn test_find() { fn test_find_bytes() {
fn t(haystack: str, needle: str, i: int) { fn t(haystack: str, needle: str, i: int) {
let j: int = find(haystack, needle); let j: int = find(haystack, needle);
log(debug, "searched for " + needle); log(debug, "searched for " + needle);
@ -1743,12 +1753,11 @@ mod tests {
t("this is a simple", "simple", 10); t("this is a simple", "simple", 10);
t("this", "simple", -1); t("this", "simple", -1);
// FIXME: return option<char> position instead
let data = "ประเทศไทย中华Việt Nam"; let data = "ประเทศไทย中华Việt Nam";
assert (find(data, "ประเ") == 0); assert (find_bytes(data, "ประเ") == option::some( 0u));
assert (find(data, "ะเ") == 6); // byte position assert (find_bytes(data, "ะเ") == option::some( 6u));
assert (find(data, "中华") == 27); // byte position assert (find_bytes(data, "中华") == option::some(27u));
assert (find(data, "ไท华") == -1); assert (find_bytes(data, "ไท华") == option::none);
} }
#[test] #[test]

View file

@ -258,7 +258,7 @@ fn filter_tests(opts: test_opts,
fn filter_fn(test: test_desc, filter_str: str) -> fn filter_fn(test: test_desc, filter_str: str) ->
option<test_desc> { option<test_desc> {
if str::find(test.name, filter_str) >= 0 { if str::contains(test.name, filter_str) {
ret option::some(test); ret option::some(test);
} else { ret option::none; } } else { ret option::none; }
} }

View file

@ -56,10 +56,10 @@ fn should_write_modules_last() {
fn d() { }" fn d() { }"
); );
let idx_a = str::find(markdown, "# Module `a`"); let idx_a = option::get(str::find_bytes(markdown, "# Module `a`"));
let idx_b = str::find(markdown, "## Function `b`"); let idx_b = option::get(str::find_bytes(markdown, "## Function `b`"));
let idx_c = str::find(markdown, "# Module `c`"); let idx_c = option::get(str::find_bytes(markdown, "# Module `c`"));
let idx_d = str::find(markdown, "## Function `d`"); let idx_d = option::get(str::find_bytes(markdown, "## Function `d`"));
assert idx_b < idx_d; assert idx_b < idx_d;
assert idx_d < idx_a; assert idx_d < idx_a;