From 9e60e2e297cb28ec4812fd3ed6124f44ff28c642 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 11 Jun 2013 21:46:40 +1000 Subject: [PATCH] std: convert str::replace to a method. --- src/compiletest/runtest.rs | 6 +-- src/libextra/rope.rs | 1 - src/libextra/terminfo/searcher.rs | 2 +- src/librustc/driver/driver.rs | 3 +- src/librustc/middle/lint.rs | 3 +- src/librustc/middle/trans/build.rs | 4 +- src/librustc/rustc.rc | 2 +- src/librustdoc/desc_to_brief_pass.rs | 2 +- src/librustdoc/escape_pass.rs | 2 +- src/librustdoc/markdown_index_pass.rs | 36 +++++++-------- src/librustdoc/markdown_pass.rs | 2 +- src/librustpkg/package_path.rs | 2 +- src/libstd/str.rs | 66 +++++++++++++-------------- 13 files changed, 64 insertions(+), 67 deletions(-) diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 6b4f1420c565..c92084781287 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -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 diff --git a/src/libextra/rope.rs b/src/libextra/rope.rs index 0856c256c5b1..099b257380bc 100644 --- a/src/libextra/rope.rs +++ b/src/libextra/rope.rs @@ -564,7 +564,6 @@ pub mod node { use rope::node; use core::cast; - use core::str; use core::uint; use core::vec; diff --git a/src/libextra/terminfo/searcher.rs b/src/libextra/terminfo/searcher.rs index 48a3e1e9c695..1ef410252ab9 100644 --- a/src/libextra/terminfo/searcher.rs +++ b/src/libextra/terminfo/searcher.rs @@ -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; diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 7a631b5fc683..4b7ab6fdfc10 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -29,7 +29,6 @@ use core::hashmap::HashMap; 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", diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 007970067b33..733453be3594 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -23,7 +23,6 @@ use core::i16; 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 @@ impl Context { 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) => { diff --git a/src/librustc/middle/trans/build.rs b/src/librustc/middle/trans/build.rs index 25aa59b852de..af1084518100 100644 --- a/src/librustc/middle/trans/build.rs +++ b/src/librustc/middle/trans/build.rs @@ -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"); diff --git a/src/librustc/rustc.rc b/src/librustc/rustc.rc index adb0f43e2d72..ff19893daad9 100644 --- a/src/librustc/rustc.rc +++ b/src/librustc/rustc.rc @@ -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 { diff --git a/src/librustdoc/desc_to_brief_pass.rs b/src/librustdoc/desc_to_brief_pass.rs index 5fec0882a0ad..c116ccd69869 100644 --- a/src/librustdoc/desc_to_brief_pass.rs +++ b/src/librustdoc/desc_to_brief_pass.rs @@ -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 } diff --git a/src/librustdoc/escape_pass.rs b/src/librustdoc/escape_pass.rs index 045e916b1100..ef07bdfca167 100644 --- a/src/librustdoc/escape_pass.rs +++ b/src/librustdoc/escape_pass.rs @@ -20,7 +20,7 @@ pub fn mk_pass() -> Pass { } fn escape(s: &str) -> ~str { - str::replace(s, "\\", "\\\\") + s.replace("\\", "\\\\") } #[test] diff --git a/src/librustdoc/markdown_index_pass.rs b/src/librustdoc/markdown_index_pass.rs index 36eb5e77ad65..e7def3ebb9bb 100644 --- a/src/librustdoc/markdown_index_pass.rs +++ b/src/librustdoc/markdown_index_pass.rs @@ -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 @@ pub fn pandoc_header_id(header: &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 diff --git a/src/librustdoc/markdown_pass.rs b/src/librustdoc/markdown_pass.rs index b6917f527a1f..6f480d187707 100644 --- a/src/librustdoc/markdown_pass.rs +++ b/src/librustdoc/markdown_pass.rs @@ -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; } diff --git a/src/librustpkg/package_path.rs b/src/librustpkg/package_path.rs index 98ff2751545d..0216b032a6da 100644 --- a/src/librustpkg/package_path.rs +++ b/src/librustpkg/package_path.rs @@ -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)) } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index fdcca339f9bf..0891f1774332 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -581,30 +581,6 @@ pub fn each_split_within<'a>(ss: &'a str, 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 @@ pub trait StrSlice<'self> { 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 @@ 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 #[inline] fn to_owned(&self) -> ~str { @@ -2592,13 +2592,13 @@ mod tests { #[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 @@ mod tests { let a = ~"ประเ"; let A = ~"دولة الكويتทศไทย中华"; - assert_eq!(replace(data, a, repl), A); + assert_eq!(data.replace(a, repl), A); } #[test] @@ -2618,7 +2618,7 @@ mod tests { let b = ~"ะเ"; let B = ~"ปรدولة الكويتทศไทย中华"; - assert_eq!(replace(data, b, repl), B); + assert_eq!(data.replace(b, repl), B); } #[test] @@ -2628,7 +2628,7 @@ mod tests { let c = ~"中华"; let C = ~"ประเทศไทยدولة الكويت"; - assert_eq!(replace(data, c, repl), C); + assert_eq!(data.replace(c, repl), C); } #[test] @@ -2637,7 +2637,7 @@ mod tests { let repl = ~"دولة الكويت"; let d = ~"ไท华"; - assert_eq!(replace(data, d, repl), data); + assert_eq!(data.replace(d, repl), data); } #[test]