std: replace str::{starts,ends}_with with the method.
This commit is contained in:
parent
248b6e38b5
commit
838191c40b
@ -231,11 +231,11 @@ pub fn is_test(config: &config, testfile: &Path) -> bool {
|
||||
let mut valid = false;
|
||||
|
||||
for valid_extensions.each |ext| {
|
||||
if str::ends_with(name, *ext) { valid = true; }
|
||||
if name.ends_with(*ext) { valid = true; }
|
||||
}
|
||||
|
||||
for invalid_prefixes.each |pre| {
|
||||
if str::starts_with(name, *pre) { valid = false; }
|
||||
if name.starts_with(*pre) { valid = false; }
|
||||
}
|
||||
|
||||
return valid;
|
||||
|
@ -112,7 +112,7 @@ fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
|
||||
// Assume that any directives will be found before the first
|
||||
// module or function. This doesn't seem to be an optimization
|
||||
// with a warm page cache. Maybe with a cold one.
|
||||
if str::starts_with(ln, "fn") || str::starts_with(ln, "mod") {
|
||||
if ln.starts_with("fn") || ln.starts_with("mod") {
|
||||
return false;
|
||||
} else { if !(it(ln)) { return false; } }
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
|
||||
if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
|
||||
else { (k,v) }
|
||||
};
|
||||
if str::ends_with(prog, "rustc.exe") {
|
||||
if prog.ends_with("rustc.exe") {
|
||||
env.push((~"RUST_THREADS", ~"1"));
|
||||
}
|
||||
return env;
|
||||
|
@ -364,7 +364,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
|
||||
if !found_flags[i] {
|
||||
debug!("prefix=%s ee.kind=%s ee.msg=%s line=%s",
|
||||
prefixes[i], ee.kind, ee.msg, line);
|
||||
if (str::starts_with(line, prefixes[i]) &&
|
||||
if (line.starts_with(prefixes[i]) &&
|
||||
line.contains(ee.kind) &&
|
||||
line.contains(ee.msg)) {
|
||||
found_flags[i] = true;
|
||||
|
@ -394,7 +394,7 @@ enum Input {
|
||||
// returns userinfo, host, port, and unparsed part, or an error
|
||||
fn get_authority(rawurl: &str) ->
|
||||
Result<(Option<UserInfo>, ~str, Option<~str>, ~str), ~str> {
|
||||
if !str::starts_with(rawurl, "//") {
|
||||
if !raw_url.starts_with("//") {
|
||||
// there is no authority.
|
||||
return Ok((None, ~"", None, rawurl.to_str()));
|
||||
}
|
||||
@ -579,7 +579,7 @@ fn get_path(rawurl: &str, authority: bool) ->
|
||||
}
|
||||
|
||||
if authority {
|
||||
if end != 0 && !str::starts_with(rawurl, "/") {
|
||||
if end != 0 && !rawurl.starts_with("/") {
|
||||
return Err(~"Non-empty path must begin with\
|
||||
'/' in presence of authority.");
|
||||
}
|
||||
@ -592,8 +592,8 @@ fn get_path(rawurl: &str, authority: bool) ->
|
||||
// returns the parsed query and the fragment, if present
|
||||
fn get_query_fragment(rawurl: &str) ->
|
||||
Result<(Query, Option<~str>), ~str> {
|
||||
if !str::starts_with(rawurl, "?") {
|
||||
if str::starts_with(rawurl, "#") {
|
||||
if !rawurl.starts_with("?") {
|
||||
if rawurl.starts_with("#") {
|
||||
let f = decode_component(rawurl.slice(
|
||||
1,
|
||||
rawurl.len()));
|
||||
|
@ -42,7 +42,7 @@ mod tests {
|
||||
fn test_mkdtemp() {
|
||||
let p = mkdtemp(&Path("."), "foobar").unwrap();
|
||||
os::remove_dir(&p);
|
||||
assert!(str::ends_with(p.to_str(), "foobar"));
|
||||
assert!(p.to_str().ends_with("foobar"));
|
||||
}
|
||||
|
||||
// Ideally these would be in core::os but then core would need
|
||||
|
@ -240,7 +240,7 @@ fn test_prefix_rpath() {
|
||||
debug!("test_prefix_path: %s vs. %s",
|
||||
res.to_str(),
|
||||
d.to_str());
|
||||
assert!(str::ends_with(res.to_str(), d.to_str()));
|
||||
assert!(res.to_str().ends_with(d.to_str()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -244,7 +244,7 @@ fn get_cache(cx: @CrateContext) -> metadata_cache {
|
||||
}
|
||||
|
||||
fn get_file_path_and_dir(work_dir: &str, full_path: &str) -> (~str, ~str) {
|
||||
(if str::starts_with(full_path, work_dir) {
|
||||
(if full_path.starts_with(work_dir) {
|
||||
full_path.slice(work_dir.len() + 1u,
|
||||
full_path.len()).to_owned()
|
||||
} else {
|
||||
|
@ -134,7 +134,7 @@ fn first_sentence_(s: &str) -> ~str {
|
||||
str::to_owned(s.slice(0, idx - 1))
|
||||
}
|
||||
_ => {
|
||||
if str::ends_with(s, ".") {
|
||||
if s.ends_with(".") {
|
||||
str::to_owned(s)
|
||||
} else {
|
||||
str::to_owned(s)
|
||||
|
@ -153,7 +153,7 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
|
||||
}
|
||||
|
||||
fn parse_header(line: ~str) -> Option<~str> {
|
||||
if str::starts_with(line, "# ") {
|
||||
if line.starts_with("# ") {
|
||||
Some(line.slice(2u, line.len()).to_owned())
|
||||
} else {
|
||||
None
|
||||
|
@ -1979,7 +1979,7 @@ fn test_write_empty() {
|
||||
fn file_writer_bad_name() {
|
||||
match io::file_writer(&Path("?/?"), []) {
|
||||
result::Err(e) => {
|
||||
assert!(str::starts_with(e, "error opening"));
|
||||
assert!(e.starts_with("error opening"));
|
||||
}
|
||||
result::Ok(_) => fail!()
|
||||
}
|
||||
@ -1989,7 +1989,7 @@ fn file_writer_bad_name() {
|
||||
fn buffered_file_writer_bad_name() {
|
||||
match io::buffered_file_writer(&Path("?/?")) {
|
||||
result::Err(e) => {
|
||||
assert!(str::starts_with(e, "error opening"));
|
||||
assert!(e.starts_with("error opening"));
|
||||
}
|
||||
result::Ok(_) => fail!()
|
||||
}
|
||||
|
@ -977,36 +977,6 @@ fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if one string starts with another
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * haystack - The string to look in
|
||||
* * needle - The string to look for
|
||||
*/
|
||||
pub fn starts_with<'a,'b>(haystack: &'a str, needle: &'b str) -> bool {
|
||||
let (haystack_len, needle_len) = (haystack.len(), needle.len());
|
||||
if needle_len == 0u { true }
|
||||
else if needle_len > haystack_len { false }
|
||||
else { match_at(haystack, needle, 0u) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if one string ends with another
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * haystack - The string to look in
|
||||
* * needle - The string to look for
|
||||
*/
|
||||
pub fn ends_with<'a,'b>(haystack: &'a str, needle: &'b str) -> bool {
|
||||
let (haystack_len, needle_len) = (haystack.len(), needle.len());
|
||||
if needle_len == 0u { true }
|
||||
else if needle_len > haystack_len { false }
|
||||
else { match_at(haystack, needle, haystack_len - needle_len) }
|
||||
}
|
||||
|
||||
/*
|
||||
Section: String properties
|
||||
*/
|
||||
@ -1600,7 +1570,7 @@ fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint, allow_trailing_
|
||||
fn slice(&self, begin: uint, end: uint) -> &'self str;
|
||||
fn slice_from(&self, begin: uint) -> &'self str;
|
||||
fn slice_to(&self, end: uint) -> &'self str;
|
||||
fn starts_with<'a>(&self, needle: &'a str) -> bool;
|
||||
fn starts_with(&self, needle: &str) -> bool;
|
||||
fn substr(&self, begin: uint, n: uint) -> &'self str;
|
||||
fn escape_default(&self) -> ~str;
|
||||
fn escape_unicode(&self) -> ~str;
|
||||
@ -1770,12 +1740,6 @@ fn word_iter(&self) -> WordIterator<'self> {
|
||||
self.split_iter(char::is_whitespace).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
|
||||
/// Returns true if one string ends with another
|
||||
#[inline]
|
||||
fn ends_with(&self, needle: &str) -> bool {
|
||||
ends_with(*self, needle)
|
||||
}
|
||||
/// Returns true if the string has length 0
|
||||
#[inline]
|
||||
fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
@ -1831,11 +1795,21 @@ fn slice_from(&self, begin: uint) -> &'self str {
|
||||
fn slice_to(&self, end: uint) -> &'self str {
|
||||
self.slice(0, end)
|
||||
}
|
||||
/// Checks if `needle` is a prefix of the string.
|
||||
#[inline]
|
||||
/// Returns true if `needle` is a prefix of the string.
|
||||
fn starts_with<'a>(&self, needle: &'a str) -> bool {
|
||||
starts_with(*self, needle)
|
||||
let (self_len, needle_len) = (self.len(), needle.len());
|
||||
if needle_len == 0u { true }
|
||||
else if needle_len > self_len { false }
|
||||
else { match_at(*self, needle, 0u) }
|
||||
}
|
||||
/// Returns true if `needle` is a suffix of the string.
|
||||
pub fn ends_with(&self, needle: &str) -> bool {
|
||||
let (self_len, needle_len) = (self.len(), needle.len());
|
||||
if needle_len == 0u { true }
|
||||
else if needle_len > self_len { false }
|
||||
else { match_at(*self, needle, self_len - needle_len) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a substring of another.
|
||||
*
|
||||
@ -2591,20 +2565,20 @@ fn half_a_million_letter_a() -> ~str {
|
||||
|
||||
#[test]
|
||||
fn test_starts_with() {
|
||||
assert!((starts_with("", "")));
|
||||
assert!((starts_with("abc", "")));
|
||||
assert!((starts_with("abc", "a")));
|
||||
assert!((!starts_with("a", "abc")));
|
||||
assert!((!starts_with("", "abc")));
|
||||
assert!(("".starts_with("")));
|
||||
assert!(("abc".starts_with("")));
|
||||
assert!(("abc".starts_with("a")));
|
||||
assert!((!"a".starts_with("abc")));
|
||||
assert!((!"".starts_with("abc")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ends_with() {
|
||||
assert!((ends_with("", "")));
|
||||
assert!((ends_with("abc", "")));
|
||||
assert!((ends_with("abc", "c")));
|
||||
assert!((!ends_with("a", "abc")));
|
||||
assert!((!ends_with("", "abc")));
|
||||
assert!(("".ends_with("")));
|
||||
assert!(("abc".ends_with("")));
|
||||
assert!(("abc".ends_with("c")));
|
||||
assert!((!"a".ends_with("abc")));
|
||||
assert!((!"".ends_with("abc")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user