std: convert str::replace to a method.

This commit is contained in:
Huon Wilson 2013-06-11 21:46:40 +10:00
parent 12750c8893
commit 9e60e2e297
13 changed files with 64 additions and 67 deletions

View File

@ -171,8 +171,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
if props.pp_exact.is_some() { if props.pp_exact.is_some() {
// Now we have to care about line endings // Now we have to care about line endings
let cr = ~"\r"; let cr = ~"\r";
actual = str::replace(actual, cr, ""); actual = actual.replace(cr, "");
expected = str::replace(expected, cr, ""); expected = expected.replace(cr, "");
} }
compare_source(expected, actual); compare_source(expected, actual);
@ -238,7 +238,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
// do not optimize debuginfo tests // do not optimize debuginfo tests
let mut config = match config.rustcflags { let mut config = match config.rustcflags {
Some(ref flags) => config { Some(ref flags) => config {
rustcflags: Some(str::replace(*flags, "-O", "")), rustcflags: Some(flags.replace("-O", "")),
.. copy *config .. copy *config
}, },
None => copy *config None => copy *config

View File

@ -564,7 +564,6 @@ pub mod node {
use rope::node; use rope::node;
use core::cast; use core::cast;
use core::str;
use core::uint; use core::uint;
use core::vec; use core::vec;

View File

@ -12,7 +12,7 @@
/// Does not support hashed database, only filesystem! /// Does not support hashed database, only filesystem!
use core::prelude::*; use core::prelude::*;
use core::{os}; use core::{os, str};
use core::os::getenv; use core::os::getenv;
use core::io::{file_reader, Reader}; use core::io::{file_reader, Reader};
use core::iterator::IteratorUtil; use core::iterator::IteratorUtil;

View File

@ -29,7 +29,6 @@ use core::hashmap::HashMap;
use core::int; use core::int;
use core::io; use core::io;
use core::os; use core::os;
use core::str;
use core::vec; use core::vec;
use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt}; use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt};
use extra::getopts::{opt_present}; use extra::getopts::{opt_present};
@ -595,7 +594,7 @@ pub fn build_session_options(binary: @~str,
let flags = vec::append(getopts::opt_strs(matches, level_short), let flags = vec::append(getopts::opt_strs(matches, level_short),
getopts::opt_strs(matches, level_name)); getopts::opt_strs(matches, level_name));
for flags.each |lint_name| { for flags.each |lint_name| {
let lint_name = str::replace(*lint_name, "-", "_"); let lint_name = lint_name.replace("-", "_");
match lint_dict.find(&lint_name) { match lint_dict.find(&lint_name) {
None => { None => {
early_error(demitter, fmt!("unknown %s flag: %s", early_error(demitter, fmt!("unknown %s flag: %s",

View File

@ -23,7 +23,6 @@ use core::i16;
use core::i32; use core::i32;
use core::i64; use core::i64;
use core::i8; use core::i8;
use core::str;
use core::u16; use core::u16;
use core::u32; use core::u32;
use core::u64; use core::u64;
@ -375,7 +374,7 @@ impl Context {
fmt!("%s [-%c %s%s]", msg, match level { fmt!("%s [-%c %s%s]", msg, match level {
warn => 'W', deny => 'D', forbid => 'F', warn => 'W', deny => 'D', forbid => 'F',
allow => fail!() allow => fail!()
}, str::replace(self.lint_to_str(lint), "_", "-"), }, self.lint_to_str(lint).replace("_", "-"),
if src == Default { " (default)" } else { "" }) if src == Default { " (default)" } else { "" })
}, },
Node(src) => { Node(src) => {

View File

@ -885,9 +885,9 @@ pub fn add_comment(bcx: block, text: &str) {
unsafe { unsafe {
let ccx = bcx.ccx(); let ccx = bcx.ccx();
if ccx.sess.asm_comments() { if ccx.sess.asm_comments() {
let sanitized = str::replace(text, "$", ""); let sanitized = text.replace("$", "");
let comment_text = ~"# " + let comment_text = ~"# " +
str::replace(sanitized, "\n", "\n\t# "); sanitized.replace("\n", "\n\t# ");
let asm = str::as_c_str(comment_text, |c| { let asm = str::as_c_str(comment_text, |c| {
str::as_c_str("", |e| { str::as_c_str("", |e| {
count_insn(bcx, "inlineasm"); count_insn(bcx, "inlineasm");

View File

@ -209,7 +209,7 @@ Available lint options:
io::println(fmt!(" %s %7.7s %s\n", io::println(fmt!(" %s %7.7s %s\n",
padded(max_key, "----"), "-------", "-------")); padded(max_key, "----"), "-------", "-------"));
for lint_dict.each |k, v| { for lint_dict.each |k, v| {
let k = str::replace(*k, "_", "-"); let k = k.replace("_", "-");
io::println(fmt!(" %s %7.7s %s", io::println(fmt!(" %s %7.7s %s",
padded(max_key, k), padded(max_key, k),
match v.default { match v.default {

View File

@ -108,7 +108,7 @@ fn first_sentence(s: ~str) -> Option<~str> {
let paras = paragraphs(s); let paras = paragraphs(s);
if !paras.is_empty() { if !paras.is_empty() {
let first_para = paras.head(); let first_para = paras.head();
Some(str::replace(first_sentence_(*first_para), "\n", " ")) Some(first_sentence_(*first_para).replace("\n", " "))
} else { } else {
None None
} }

View File

@ -20,7 +20,7 @@ pub fn mk_pass() -> Pass {
} }
fn escape(s: &str) -> ~str { fn escape(s: &str) -> ~str {
str::replace(s, "\\", "\\\\") s.replace("\\", "\\\\")
} }
#[test] #[test]

View File

@ -128,24 +128,24 @@ pub fn pandoc_header_id(header: &str) -> ~str {
return header; return header;
fn remove_formatting(s: &str) -> ~str { fn remove_formatting(s: &str) -> ~str {
str::replace(s, "`", "") s.replace("`", "")
} }
fn remove_punctuation(s: &str) -> ~str { fn remove_punctuation(s: &str) -> ~str {
let s = str::replace(s, "<", ""); let s = s.replace("<", "");
let s = str::replace(s, ">", ""); let s = s.replace(">", "");
let s = str::replace(s, "[", ""); let s = s.replace("[", "");
let s = str::replace(s, "]", ""); let s = s.replace("]", "");
let s = str::replace(s, "(", ""); let s = s.replace("(", "");
let s = str::replace(s, ")", ""); let s = s.replace(")", "");
let s = str::replace(s, "@~", ""); let s = s.replace("@~", "");
let s = str::replace(s, "~", ""); let s = s.replace("~", "");
let s = str::replace(s, "/", ""); let s = s.replace("/", "");
let s = str::replace(s, ":", ""); let s = s.replace(":", "");
let s = str::replace(s, "&", ""); let s = s.replace("&", "");
let s = str::replace(s, "^", ""); let s = s.replace("^", "");
let s = str::replace(s, ",", ""); let s = s.replace(",", "");
let s = str::replace(s, "'", ""); let s = s.replace("'", "");
let s = str::replace(s, "+", ""); let s = s.replace("+", "");
return s; return s;
} }
fn replace_with_hyphens(s: &str) -> ~str { fn replace_with_hyphens(s: &str) -> ~str {
@ -153,8 +153,8 @@ pub fn pandoc_header_id(header: &str) -> ~str {
// XXX: Hacky implementation here that only covers // XXX: Hacky implementation here that only covers
// one or two spaces. // one or two spaces.
let s = s.trim(); let s = s.trim();
let s = str::replace(s, " ", "-"); let s = s.replace(" ", "-");
let s = str::replace(s, " ", "-"); let s = s.replace(" ", "-");
return s; return s;
} }
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use // FIXME: #4318 Instead of to_ascii and to_str_ascii, could use

View File

@ -114,7 +114,7 @@ fn make_title(page: doc::Page) -> ~str {
} }
}; };
let title = markdown_pass::header_text(item); let title = markdown_pass::header_text(item);
let title = str::replace(title, "`", ""); let title = title.replace("`", "");
return title; return title;
} }

View File

@ -32,7 +32,7 @@ pub fn normalize(p_: RemotePath) -> LocalPath {
match p.filestem() { match p.filestem() {
None => LocalPath(p), None => LocalPath(p),
Some(st) => { Some(st) => {
let replaced = str::replace(st, "-", "_"); let replaced = st.replace("-", "_");
if replaced != st { if replaced != st {
LocalPath(p.with_filestem(replaced)) LocalPath(p.with_filestem(replaced))
} }

View File

@ -581,30 +581,6 @@ pub fn each_split_within<'a>(ss: &'a str,
return cont; return cont;
} }
/**
* Replace all occurrences of one string with another
*
* # Arguments
*
* * s - The string containing substrings to replace
* * from - The string to replace
* * to - The replacement string
*
* # Return value
*
* The original string with all occurances of `from` replaced with `to`
*/
pub fn replace(s: &str, from: &str, to: &str) -> ~str {
let mut (result, last_end) = (~"", 0);
for s.matches_index_iter(from).advance |(start, end)| {
result.push_str(unsafe{raw::slice_bytes(s, last_end, start)});
result.push_str(to);
last_end = end;
}
result.push_str(unsafe{raw::slice_bytes(s, last_end, s.len())});
result
}
/* /*
Section: Comparing strings Section: Comparing strings
*/ */
@ -1349,6 +1325,7 @@ pub trait StrSlice<'self> {
fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str; fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str;
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str; fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str;
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str; fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str;
fn replace(&self, from: &str, to: &str) -> ~str;
fn to_owned(&self) -> ~str; fn to_owned(&self) -> ~str;
fn to_managed(&self) -> @str; fn to_managed(&self) -> @str;
fn is_char_boundary(&self, index: uint) -> bool; fn is_char_boundary(&self, index: uint) -> bool;
@ -1694,6 +1671,29 @@ impl<'self> StrSlice<'self> for &'self str {
} }
} }
/**
* Replace all occurrences of one string with another
*
* # Arguments
*
* * from - The string to replace
* * to - The replacement string
*
* # Return value
*
* The original string with all occurances of `from` replaced with `to`
*/
pub fn replace(&self, from: &str, to: &str) -> ~str {
let mut (result, last_end) = (~"", 0);
for self.matches_index_iter(from).advance |(start, end)| {
result.push_str(unsafe{raw::slice_bytes(*self, last_end, start)});
result.push_str(to);
last_end = end;
}
result.push_str(unsafe{raw::slice_bytes(*self, last_end, self.len())});
result
}
/// Copy a slice into a new unique str /// Copy a slice into a new unique str
#[inline] #[inline]
fn to_owned(&self) -> ~str { fn to_owned(&self) -> ~str {
@ -2592,13 +2592,13 @@ mod tests {
#[test] #[test]
fn test_replace() { fn test_replace() {
let a = "a"; let a = "a";
assert_eq!(replace("", a, "b"), ~""); assert_eq!("".replace(a, "b"), ~"");
assert_eq!(replace("a", a, "b"), ~"b"); assert_eq!("a".replace(a, "b"), ~"b");
assert_eq!(replace("ab", a, "b"), ~"bb"); assert_eq!("ab".replace(a, "b"), ~"bb");
let test = "test"; let test = "test";
assert!(replace(" test test ", test, "toast") == assert!(" test test ".replace(test, "toast") ==
~" toast toast "); ~" toast toast ");
assert_eq!(replace(" test test ", test, ""), ~" "); assert_eq!(" test test ".replace(test, ""), ~" ");
} }
#[test] #[test]
@ -2608,7 +2608,7 @@ mod tests {
let a = ~"ประเ"; let a = ~"ประเ";
let A = ~"دولة الكويتทศไทย中华"; let A = ~"دولة الكويتทศไทย中华";
assert_eq!(replace(data, a, repl), A); assert_eq!(data.replace(a, repl), A);
} }
#[test] #[test]
@ -2618,7 +2618,7 @@ mod tests {
let b = ~"ะเ"; let b = ~"ะเ";
let B = ~"ปรدولة الكويتทศไทย中华"; let B = ~"ปรدولة الكويتทศไทย中华";
assert_eq!(replace(data, b, repl), B); assert_eq!(data.replace(b, repl), B);
} }
#[test] #[test]
@ -2628,7 +2628,7 @@ mod tests {
let c = ~"中华"; let c = ~"中华";
let C = ~"ประเทศไทยدولة الكويت"; let C = ~"ประเทศไทยدولة الكويت";
assert_eq!(replace(data, c, repl), C); assert_eq!(data.replace(c, repl), C);
} }
#[test] #[test]
@ -2637,7 +2637,7 @@ mod tests {
let repl = ~"دولة الكويت"; let repl = ~"دولة الكويت";
let d = ~"ไท华"; let d = ~"ไท华";
assert_eq!(replace(data, d, repl), data); assert_eq!(data.replace(d, repl), data);
} }
#[test] #[test]