提交 9e60e2e2 编写于 作者: H Huon Wilson

std: convert str::replace to a method.

上级 12750c88
......@@ -171,8 +171,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
if props.pp_exact.is_some() {
// Now we have to care about line endings
let cr = ~"\r";
actual = str::replace(actual, cr, "");
expected = str::replace(expected, cr, "");
actual = actual.replace(cr, "");
expected = expected.replace(cr, "");
}
compare_source(expected, actual);
......@@ -238,7 +238,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
// do not optimize debuginfo tests
let mut config = match config.rustcflags {
Some(ref flags) => config {
rustcflags: Some(str::replace(*flags, "-O", "")),
rustcflags: Some(flags.replace("-O", "")),
.. copy *config
},
None => copy *config
......
......@@ -564,7 +564,6 @@ pub mod node {
use rope::node;
use core::cast;
use core::str;
use core::uint;
use core::vec;
......
......@@ -12,7 +12,7 @@
/// Does not support hashed database, only filesystem!
use core::prelude::*;
use core::{os};
use core::{os, str};
use core::os::getenv;
use core::io::{file_reader, Reader};
use core::iterator::IteratorUtil;
......
......@@ -29,7 +29,6 @@
use core::int;
use core::io;
use core::os;
use core::str;
use core::vec;
use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt};
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),
getopts::opt_strs(matches, level_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) {
None => {
early_error(demitter, fmt!("unknown %s flag: %s",
......
......@@ -23,7 +23,6 @@
use core::i32;
use core::i64;
use core::i8;
use core::str;
use core::u16;
use core::u32;
use core::u64;
......@@ -375,7 +374,7 @@ fn span_lint(&self, lint: lint, span: span, msg: &str) {
fmt!("%s [-%c %s%s]", msg, match level {
warn => 'W', deny => 'D', forbid => 'F',
allow => fail!()
}, str::replace(self.lint_to_str(lint), "_", "-"),
}, self.lint_to_str(lint).replace("_", "-"),
if src == Default { " (default)" } else { "" })
},
Node(src) => {
......
......@@ -885,9 +885,9 @@ pub fn add_comment(bcx: block, text: &str) {
unsafe {
let ccx = bcx.ccx();
if ccx.sess.asm_comments() {
let sanitized = str::replace(text, "$", "");
let sanitized = text.replace("$", "");
let comment_text = ~"# " +
str::replace(sanitized, "\n", "\n\t# ");
sanitized.replace("\n", "\n\t# ");
let asm = str::as_c_str(comment_text, |c| {
str::as_c_str("", |e| {
count_insn(bcx, "inlineasm");
......
......@@ -209,7 +209,7 @@ Available lint options:
io::println(fmt!(" %s %7.7s %s\n",
padded(max_key, "----"), "-------", "-------"));
for lint_dict.each |k, v| {
let k = str::replace(*k, "_", "-");
let k = k.replace("_", "-");
io::println(fmt!(" %s %7.7s %s",
padded(max_key, k),
match v.default {
......
......@@ -108,7 +108,7 @@ fn first_sentence(s: ~str) -> Option<~str> {
let paras = paragraphs(s);
if !paras.is_empty() {
let first_para = paras.head();
Some(str::replace(first_sentence_(*first_para), "\n", " "))
Some(first_sentence_(*first_para).replace("\n", " "))
} else {
None
}
......
......@@ -20,7 +20,7 @@ pub fn mk_pass() -> Pass {
}
fn escape(s: &str) -> ~str {
str::replace(s, "\\", "\\\\")
s.replace("\\", "\\\\")
}
#[test]
......
......@@ -128,24 +128,24 @@ pub fn pandoc_header_id(header: &str) -> ~str {
return header;
fn remove_formatting(s: &str) -> ~str {
str::replace(s, "`", "")
s.replace("`", "")
}
fn remove_punctuation(s: &str) -> ~str {
let s = str::replace(s, "<", "");
let s = str::replace(s, ">", "");
let s = str::replace(s, "[", "");
let s = str::replace(s, "]", "");
let s = str::replace(s, "(", "");
let s = str::replace(s, ")", "");
let s = str::replace(s, "@~", "");
let s = str::replace(s, "~", "");
let s = str::replace(s, "/", "");
let s = str::replace(s, ":", "");
let s = str::replace(s, "&", "");
let s = str::replace(s, "^", "");
let s = str::replace(s, ",", "");
let s = str::replace(s, "'", "");
let s = str::replace(s, "+", "");
let s = s.replace("<", "");
let s = s.replace(">", "");
let s = s.replace("[", "");
let s = s.replace("]", "");
let s = s.replace("(", "");
let s = s.replace(")", "");
let s = s.replace("@~", "");
let s = s.replace("~", "");
let s = s.replace("/", "");
let s = s.replace(":", "");
let s = s.replace("&", "");
let s = s.replace("^", "");
let s = s.replace(",", "");
let s = s.replace("'", "");
let s = s.replace("+", "");
return s;
}
fn replace_with_hyphens(s: &str) -> ~str {
......@@ -153,8 +153,8 @@ fn replace_with_hyphens(s: &str) -> ~str {
// XXX: Hacky implementation here that only covers
// one or two spaces.
let s = s.trim();
let s = str::replace(s, " ", "-");
let s = str::replace(s, " ", "-");
let s = s.replace(" ", "-");
let s = s.replace(" ", "-");
return s;
}
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
......
......@@ -114,7 +114,7 @@ fn make_title(page: doc::Page) -> ~str {
}
};
let title = markdown_pass::header_text(item);
let title = str::replace(title, "`", "");
let title = title.replace("`", "");
return title;
}
......
......@@ -32,7 +32,7 @@ pub fn normalize(p_: RemotePath) -> LocalPath {
match p.filestem() {
None => LocalPath(p),
Some(st) => {
let replaced = str::replace(st, "-", "_");
let replaced = st.replace("-", "_");
if replaced != st {
LocalPath(p.with_filestem(replaced))
}
......
......@@ -581,30 +581,6 @@ enum LengthLimit {
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
*/
......@@ -1349,6 +1325,7 @@ fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint, allow_trailing_
fn trim_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 replace(&self, from: &str, to: &str) -> ~str;
fn to_owned(&self) -> ~str;
fn to_managed(&self) -> @str;
fn is_char_boundary(&self, index: uint) -> bool;
......@@ -1694,6 +1671,29 @@ fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'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
#[inline]
fn to_owned(&self) -> ~str {
......@@ -2592,13 +2592,13 @@ fn test_is_empty() {
#[test]
fn test_replace() {
let a = "a";
assert_eq!(replace("", a, "b"), ~"");
assert_eq!(replace("a", a, "b"), ~"b");
assert_eq!(replace("ab", a, "b"), ~"bb");
assert_eq!("".replace(a, "b"), ~"");
assert_eq!("a".replace(a, "b"), ~"b");
assert_eq!("ab".replace(a, "b"), ~"bb");
let test = "test";
assert!(replace(" test test ", test, "toast") ==
assert!(" test test ".replace(test, "toast") ==
~" toast toast ");
assert_eq!(replace(" test test ", test, ""), ~" ");
assert_eq!(" test test ".replace(test, ""), ~" ");
}
#[test]
......@@ -2608,7 +2608,7 @@ fn test_replace_2a() {
let a = ~"ประเ";
let A = ~"دولة الكويتทศไทย中华";
assert_eq!(replace(data, a, repl), A);
assert_eq!(data.replace(a, repl), A);
}
#[test]
......@@ -2618,7 +2618,7 @@ fn test_replace_2b() {
let b = ~"ะเ";
let B = ~"ปรدولة الكويتทศไทย中华";
assert_eq!(replace(data, b, repl), B);
assert_eq!(data.replace(b, repl), B);
}
#[test]
......@@ -2628,7 +2628,7 @@ fn test_replace_2c() {
let c = ~"中华";
let C = ~"ประเทศไทยدولة الكويت";
assert_eq!(replace(data, c, repl), C);
assert_eq!(data.replace(c, repl), C);
}
#[test]
......@@ -2637,7 +2637,7 @@ fn test_replace_2d() {
let repl = ~"دولة الكويت";
let d = ~"ไท华";
assert_eq!(replace(data, d, repl), data);
assert_eq!(data.replace(d, repl), data);
}
#[test]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册