Skip to content

Commit 9e60e2e

Browse files
committed
std: convert str::replace to a method.
1 parent 12750c8 commit 9e60e2e

File tree

13 files changed

+64
-67
lines changed

13 files changed

+64
-67
lines changed

src/compiletest/runtest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
171171
if props.pp_exact.is_some() {
172172
// Now we have to care about line endings
173173
let cr = ~"\r";
174-
actual = str::replace(actual, cr, "");
175-
expected = str::replace(expected, cr, "");
174+
actual = actual.replace(cr, "");
175+
expected = expected.replace(cr, "");
176176
}
177177

178178
compare_source(expected, actual);
@@ -238,7 +238,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
238238
// do not optimize debuginfo tests
239239
let mut config = match config.rustcflags {
240240
Some(ref flags) => config {
241-
rustcflags: Some(str::replace(*flags, "-O", "")),
241+
rustcflags: Some(flags.replace("-O", "")),
242242
.. copy *config
243243
},
244244
None => copy *config

src/libextra/rope.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ pub mod node {
564564
use rope::node;
565565

566566
use core::cast;
567-
use core::str;
568567
use core::uint;
569568
use core::vec;
570569

src/libextra/terminfo/searcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// Does not support hashed database, only filesystem!
1313
1414
use core::prelude::*;
15-
use core::{os};
15+
use core::{os, str};
1616
use core::os::getenv;
1717
use core::io::{file_reader, Reader};
1818
use core::iterator::IteratorUtil;

src/librustc/driver/driver.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use core::hashmap::HashMap;
2929
use core::int;
3030
use core::io;
3131
use core::os;
32-
use core::str;
3332
use core::vec;
3433
use extra::getopts::groups::{optopt, optmulti, optflag, optflagopt};
3534
use extra::getopts::{opt_present};
@@ -595,7 +594,7 @@ pub fn build_session_options(binary: @~str,
595594
let flags = vec::append(getopts::opt_strs(matches, level_short),
596595
getopts::opt_strs(matches, level_name));
597596
for flags.each |lint_name| {
598-
let lint_name = str::replace(*lint_name, "-", "_");
597+
let lint_name = lint_name.replace("-", "_");
599598
match lint_dict.find(&lint_name) {
600599
None => {
601600
early_error(demitter, fmt!("unknown %s flag: %s",

src/librustc/middle/lint.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use core::i16;
2323
use core::i32;
2424
use core::i64;
2525
use core::i8;
26-
use core::str;
2726
use core::u16;
2827
use core::u32;
2928
use core::u64;
@@ -375,7 +374,7 @@ impl Context {
375374
fmt!("%s [-%c %s%s]", msg, match level {
376375
warn => 'W', deny => 'D', forbid => 'F',
377376
allow => fail!()
378-
}, str::replace(self.lint_to_str(lint), "_", "-"),
377+
}, self.lint_to_str(lint).replace("_", "-"),
379378
if src == Default { " (default)" } else { "" })
380379
},
381380
Node(src) => {

src/librustc/middle/trans/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,9 @@ pub fn add_comment(bcx: block, text: &str) {
885885
unsafe {
886886
let ccx = bcx.ccx();
887887
if ccx.sess.asm_comments() {
888-
let sanitized = str::replace(text, "$", "");
888+
let sanitized = text.replace("$", "");
889889
let comment_text = ~"# " +
890-
str::replace(sanitized, "\n", "\n\t# ");
890+
sanitized.replace("\n", "\n\t# ");
891891
let asm = str::as_c_str(comment_text, |c| {
892892
str::as_c_str("", |e| {
893893
count_insn(bcx, "inlineasm");

src/librustc/rustc.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Available lint options:
209209
io::println(fmt!(" %s %7.7s %s\n",
210210
padded(max_key, "----"), "-------", "-------"));
211211
for lint_dict.each |k, v| {
212-
let k = str::replace(*k, "_", "-");
212+
let k = k.replace("_", "-");
213213
io::println(fmt!(" %s %7.7s %s",
214214
padded(max_key, k),
215215
match v.default {

src/librustdoc/desc_to_brief_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn first_sentence(s: ~str) -> Option<~str> {
108108
let paras = paragraphs(s);
109109
if !paras.is_empty() {
110110
let first_para = paras.head();
111-
Some(str::replace(first_sentence_(*first_para), "\n", " "))
111+
Some(first_sentence_(*first_para).replace("\n", " "))
112112
} else {
113113
None
114114
}

src/librustdoc/escape_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn mk_pass() -> Pass {
2020
}
2121

2222
fn escape(s: &str) -> ~str {
23-
str::replace(s, "\\", "\\\\")
23+
s.replace("\\", "\\\\")
2424
}
2525

2626
#[test]

src/librustdoc/markdown_index_pass.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,33 +128,33 @@ pub fn pandoc_header_id(header: &str) -> ~str {
128128
return header;
129129
130130
fn remove_formatting(s: &str) -> ~str {
131-
str::replace(s, "`", "")
131+
s.replace("`", "")
132132
}
133133
fn remove_punctuation(s: &str) -> ~str {
134-
let s = str::replace(s, "<", "");
135-
let s = str::replace(s, ">", "");
136-
let s = str::replace(s, "[", "");
137-
let s = str::replace(s, "]", "");
138-
let s = str::replace(s, "(", "");
139-
let s = str::replace(s, ")", "");
140-
let s = str::replace(s, "@~", "");
141-
let s = str::replace(s, "~", "");
142-
let s = str::replace(s, "/", "");
143-
let s = str::replace(s, ":", "");
144-
let s = str::replace(s, "&", "");
145-
let s = str::replace(s, "^", "");
146-
let s = str::replace(s, ",", "");
147-
let s = str::replace(s, "'", "");
148-
let s = str::replace(s, "+", "");
134+
let s = s.replace("<", "");
135+
let s = s.replace(">", "");
136+
let s = s.replace("[", "");
137+
let s = s.replace("]", "");
138+
let s = s.replace("(", "");
139+
let s = s.replace(")", "");
140+
let s = s.replace("@~", "");
141+
let s = s.replace("~", "");
142+
let s = s.replace("/", "");
143+
let s = s.replace(":", "");
144+
let s = s.replace("&", "");
145+
let s = s.replace("^", "");
146+
let s = s.replace(",", "");
147+
let s = s.replace("'", "");
148+
let s = s.replace("+", "");
149149
return s;
150150
}
151151
fn replace_with_hyphens(s: &str) -> ~str {
152152
// Collapse sequences of whitespace to a single dash
153153
// XXX: Hacky implementation here that only covers
154154
// one or two spaces.
155155
let s = s.trim();
156-
let s = str::replace(s, " ", "-");
157-
let s = str::replace(s, " ", "-");
156+
let s = s.replace(" ", "-");
157+
let s = s.replace(" ", "-");
158158
return s;
159159
}
160160
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use

src/librustdoc/markdown_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn make_title(page: doc::Page) -> ~str {
114114
}
115115
};
116116
let title = markdown_pass::header_text(item);
117-
let title = str::replace(title, "`", "");
117+
let title = title.replace("`", "");
118118
return title;
119119
}
120120

src/librustpkg/package_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn normalize(p_: RemotePath) -> LocalPath {
3232
match p.filestem() {
3333
None => LocalPath(p),
3434
Some(st) => {
35-
let replaced = str::replace(st, "-", "_");
35+
let replaced = st.replace("-", "_");
3636
if replaced != st {
3737
LocalPath(p.with_filestem(replaced))
3838
}

src/libstd/str.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -581,30 +581,6 @@ pub fn each_split_within<'a>(ss: &'a str,
581581
return cont;
582582
}
583583

584-
/**
585-
* Replace all occurrences of one string with another
586-
*
587-
* # Arguments
588-
*
589-
* * s - The string containing substrings to replace
590-
* * from - The string to replace
591-
* * to - The replacement string
592-
*
593-
* # Return value
594-
*
595-
* The original string with all occurances of `from` replaced with `to`
596-
*/
597-
pub fn replace(s: &str, from: &str, to: &str) -> ~str {
598-
let mut (result, last_end) = (~"", 0);
599-
for s.matches_index_iter(from).advance |(start, end)| {
600-
result.push_str(unsafe{raw::slice_bytes(s, last_end, start)});
601-
result.push_str(to);
602-
last_end = end;
603-
}
604-
result.push_str(unsafe{raw::slice_bytes(s, last_end, s.len())});
605-
result
606-
}
607-
608584
/*
609585
Section: Comparing strings
610586
*/
@@ -1349,6 +1325,7 @@ pub trait StrSlice<'self> {
13491325
fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str;
13501326
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str;
13511327
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str;
1328+
fn replace(&self, from: &str, to: &str) -> ~str;
13521329
fn to_owned(&self) -> ~str;
13531330
fn to_managed(&self) -> @str;
13541331
fn is_char_boundary(&self, index: uint) -> bool;
@@ -1694,6 +1671,29 @@ impl<'self> StrSlice<'self> for &'self str {
16941671
}
16951672
}
16961673
1674+
/**
1675+
* Replace all occurrences of one string with another
1676+
*
1677+
* # Arguments
1678+
*
1679+
* * from - The string to replace
1680+
* * to - The replacement string
1681+
*
1682+
* # Return value
1683+
*
1684+
* The original string with all occurances of `from` replaced with `to`
1685+
*/
1686+
pub fn replace(&self, from: &str, to: &str) -> ~str {
1687+
let mut (result, last_end) = (~"", 0);
1688+
for self.matches_index_iter(from).advance |(start, end)| {
1689+
result.push_str(unsafe{raw::slice_bytes(*self, last_end, start)});
1690+
result.push_str(to);
1691+
last_end = end;
1692+
}
1693+
result.push_str(unsafe{raw::slice_bytes(*self, last_end, self.len())});
1694+
result
1695+
}
1696+
16971697
/// Copy a slice into a new unique str
16981698
#[inline]
16991699
fn to_owned(&self) -> ~str {
@@ -2592,13 +2592,13 @@ mod tests {
25922592
#[test]
25932593
fn test_replace() {
25942594
let a = "a";
2595-
assert_eq!(replace("", a, "b"), ~"");
2596-
assert_eq!(replace("a", a, "b"), ~"b");
2597-
assert_eq!(replace("ab", a, "b"), ~"bb");
2595+
assert_eq!("".replace(a, "b"), ~"");
2596+
assert_eq!("a".replace(a, "b"), ~"b");
2597+
assert_eq!("ab".replace(a, "b"), ~"bb");
25982598
let test = "test";
2599-
assert!(replace(" test test ", test, "toast") ==
2599+
assert!(" test test ".replace(test, "toast") ==
26002600
~" toast toast ");
2601-
assert_eq!(replace(" test test ", test, ""), ~" ");
2601+
assert_eq!(" test test ".replace(test, ""), ~" ");
26022602
}
26032603
26042604
#[test]
@@ -2608,7 +2608,7 @@ mod tests {
26082608
26092609
let a = ~"ประเ";
26102610
let A = ~"دولة الكويتทศไทย中华";
2611-
assert_eq!(replace(data, a, repl), A);
2611+
assert_eq!(data.replace(a, repl), A);
26122612
}
26132613
26142614
#[test]
@@ -2618,7 +2618,7 @@ mod tests {
26182618
26192619
let b = ~"ะเ";
26202620
let B = ~"ปรدولة الكويتทศไทย中华";
2621-
assert_eq!(replace(data, b, repl), B);
2621+
assert_eq!(data.replace(b, repl), B);
26222622
}
26232623
26242624
#[test]
@@ -2628,7 +2628,7 @@ mod tests {
26282628
26292629
let c = ~"中华";
26302630
let C = ~"ประเทศไทยدولة الكويت";
2631-
assert_eq!(replace(data, c, repl), C);
2631+
assert_eq!(data.replace(c, repl), C);
26322632
}
26332633
26342634
#[test]
@@ -2637,7 +2637,7 @@ mod tests {
26372637
let repl = ~"دولة الكويت";
26382638
26392639
let d = ~"ไท华";
2640-
assert_eq!(replace(data, d, repl), data);
2640+
assert_eq!(data.replace(d, repl), data);
26412641
}
26422642
26432643
#[test]

0 commit comments

Comments
 (0)