Skip to content

Commit f632f46

Browse files
committed
std: convert str::trim* to methods.
1 parent 1553874 commit f632f46

File tree

5 files changed

+94
-114
lines changed

5 files changed

+94
-114
lines changed

src/libextra/num/bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl ToStrRadix for BigUint {
524524
let s = uint::to_str_radix(*n as uint, radix);
525525
str::from_chars(vec::from_elem(l - s.len(), '0')) + s
526526
}));
527-
str::trim_left_chars(s, ['0']).to_owned()
527+
s.trim_left_chars(['0']).to_owned()
528528
}
529529
}
530530
}

src/librustdoc/markdown_index_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn pandoc_header_id(header: &str) -> ~str {
152152
// Collapse sequences of whitespace to a single dash
153153
// XXX: Hacky implementation here that only covers
154154
// one or two spaces.
155-
let s = str::trim(s);
155+
let s = s.trim();
156156
let s = str::replace(s, " ", "-");
157157
let s = str::replace(s, " ", "-");
158158
return s;

src/librustdoc/text_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod test {
157157
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
158158
let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
159159
let doc = (sectionalize_pass::mk_pass().f)(srv.clone(), doc);
160-
(mk_pass(~"", |s| str::trim(s).to_owned() ).f)(srv.clone(), doc)
160+
(mk_pass(~"", |s| s.trim().to_owned() ).f)(srv.clone(), doc)
161161
}
162162
}
163163

src/librusti/rusti.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
318318
match get_line(use_rl, "rusti| ") {
319319
None => fail!("unterminated multiline command :{ .. :}"),
320320
Some(line) => {
321-
if str::trim(line) == ":}" {
321+
if line.trim() == ":}" {
322322
end_multiline = true;
323323
} else {
324324
multiline_cmd += line + "\n";

src/libstd/str.rs

Lines changed: 90 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -413,80 +413,6 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
413413
*s = new_str;
414414
}
415415

416-
/**
417-
* Returns a string with leading `chars_to_trim` removed.
418-
*
419-
* # Arguments
420-
*
421-
* * s - A string
422-
* * chars_to_trim - A vector of chars
423-
*
424-
*/
425-
pub fn trim_left_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
426-
if chars_to_trim.is_empty() { return s; }
427-
428-
match s.find(|c| !chars_to_trim.contains(&c)) {
429-
None => "",
430-
Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
431-
}
432-
}
433-
434-
/**
435-
* Returns a string with trailing `chars_to_trim` removed.
436-
*
437-
* # Arguments
438-
*
439-
* * s - A string
440-
* * chars_to_trim - A vector of chars
441-
*
442-
*/
443-
pub fn trim_right_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
444-
if chars_to_trim.is_empty() { return s; }
445-
446-
match s.rfind(|c| !chars_to_trim.contains(&c)) {
447-
None => "",
448-
Some(last) => {
449-
let next = char_range_at(s, last).next;
450-
unsafe { raw::slice_bytes(s, 0u, next) }
451-
}
452-
}
453-
}
454-
455-
/**
456-
* Returns a string with leading and trailing `chars_to_trim` removed.
457-
*
458-
* # Arguments
459-
*
460-
* * s - A string
461-
* * chars_to_trim - A vector of chars
462-
*
463-
*/
464-
pub fn trim_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
465-
trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
466-
}
467-
468-
/// Returns a string with leading whitespace removed
469-
pub fn trim_left<'a>(s: &'a str) -> &'a str {
470-
match s.find(|c| !char::is_whitespace(c)) {
471-
None => "",
472-
Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
473-
}
474-
}
475-
476-
/// Returns a string with trailing whitespace removed
477-
pub fn trim_right<'a>(s: &'a str) -> &'a str {
478-
match s.rfind(|c| !char::is_whitespace(c)) {
479-
None => "",
480-
Some(last) => {
481-
let next = char_range_at(s, last).next;
482-
unsafe { raw::slice_bytes(s, 0u, next) }
483-
}
484-
}
485-
}
486-
487-
/// Returns a string with leading and trailing whitespace removed
488-
pub fn trim<'a>(s: &'a str) -> &'a str { trim_left(trim_right(s)) }
489-
490416
/*
491417
Section: Transforming strings
492418
*/
@@ -2024,25 +1950,79 @@ impl<'self> StrSlice<'self> for &'self str {
20241950
20251951
/// Returns a string with leading and trailing whitespace removed
20261952
#[inline]
2027-
fn trim(&self) -> &'self str { trim(*self) }
1953+
fn trim(&self) -> &'self str {
1954+
self.trim_left().trim_right()
1955+
}
20281956
/// Returns a string with leading whitespace removed
20291957
#[inline]
2030-
fn trim_left(&self) -> &'self str { trim_left(*self) }
1958+
fn trim_left(&self) -> &'self str {
1959+
match self.find(|c| !char::is_whitespace(c)) {
1960+
None => "",
1961+
Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
1962+
}
1963+
}
20311964
/// Returns a string with trailing whitespace removed
20321965
#[inline]
2033-
fn trim_right(&self) -> &'self str { trim_right(*self) }
1966+
fn trim_right(&self) -> &'self str {
1967+
match self.rfind(|c| !char::is_whitespace(c)) {
1968+
None => "",
1969+
Some(last) => {
1970+
let next = char_range_at(*self, last).next;
1971+
unsafe { raw::slice_bytes(*self, 0u, next) }
1972+
}
1973+
}
1974+
}
20341975
1976+
/**
1977+
* Returns a string with leading and trailing `chars_to_trim` removed.
1978+
*
1979+
* # Arguments
1980+
*
1981+
* * chars_to_trim - A vector of chars
1982+
*
1983+
*/
20351984
#[inline]
20361985
fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str {
2037-
trim_chars(*self, chars_to_trim)
1986+
self.trim_left_chars(chars_to_trim).trim_right_chars(chars_to_trim)
20381987
}
1988+
/**
1989+
* Returns a string with leading `chars_to_trim` removed.
1990+
*
1991+
* # Arguments
1992+
*
1993+
* * s - A string
1994+
* * chars_to_trim - A vector of chars
1995+
*
1996+
*/
20391997
#[inline]
20401998
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str {
2041-
trim_left_chars(*self, chars_to_trim)
1999+
if chars_to_trim.is_empty() { return *self; }
2000+
2001+
match self.find(|c| !chars_to_trim.contains(&c)) {
2002+
None => "",
2003+
Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
2004+
}
20422005
}
2006+
/**
2007+
* Returns a string with trailing `chars_to_trim` removed.
2008+
*
2009+
* # Arguments
2010+
*
2011+
* * s - A string
2012+
* * chars_to_trim - A vector of chars
2013+
*
2014+
*/
20432015
#[inline]
20442016
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str {
2045-
trim_right_chars(*self, chars_to_trim)
2017+
if chars_to_trim.is_empty() { return *self; }
2018+
2019+
match self.rfind(|c| !chars_to_trim.contains(&c)) {
2020+
None => "",
2021+
Some(last) => {
2022+
let next = char_range_at(self, last).next;
2023+
unsafe { raw::slice_bytes(self, 0u, next) }
2024+
}
2025+
}
20462026
}
20472027
20482028
@@ -2754,56 +2734,56 @@ mod tests {
27542734

27552735
#[test]
27562736
fn test_trim_left_chars() {
2757-
assert!(trim_left_chars(" *** foo *** ", []) == " *** foo *** ");
2758-
assert!(trim_left_chars(" *** foo *** ", ['*', ' ']) == "foo *** ");
2759-
assert_eq!(trim_left_chars(" *** *** ", ['*', ' ']), "");
2760-
assert!(trim_left_chars("foo *** ", ['*', ' ']) == "foo *** ");
2737+
assert_eq!(" *** foo *** ".trim_left_chars([]), " *** foo *** ");
2738+
assert_eq!(" *** foo *** ".trim_left_chars(['*', ' ']), "foo *** ");
2739+
assert_eq!(" *** *** ".trim_left_chars(['*', ' ']), "");
2740+
assert_eq!("foo *** ".trim_left_chars(['*', ' ']), "foo *** ");
27612741
}
27622742

27632743
#[test]
27642744
fn test_trim_right_chars() {
2765-
assert!(trim_right_chars(" *** foo *** ", []) == " *** foo *** ");
2766-
assert!(trim_right_chars(" *** foo *** ", ['*', ' ']) == " *** foo");
2767-
assert_eq!(trim_right_chars(" *** *** ", ['*', ' ']), "");
2768-
assert!(trim_right_chars(" *** foo", ['*', ' ']) == " *** foo");
2745+
assert_eq!(" *** foo *** ".trim_right_chars([]), " *** foo *** ");
2746+
assert_eq!(" *** foo *** ".trim_right_chars(['*', ' ']), " *** foo");
2747+
assert_eq!(" *** *** ".trim_right_chars(['*', ' ']), "");
2748+
assert_eq!(" *** foo".trim_right_chars(['*', ' ']), " *** foo");
27692749
}
27702750

27712751
#[test]
27722752
fn test_trim_chars() {
2773-
assert_eq!(trim_chars(" *** foo *** ", []), " *** foo *** ");
2774-
assert_eq!(trim_chars(" *** foo *** ", ['*', ' ']), "foo");
2775-
assert_eq!(trim_chars(" *** *** ", ['*', ' ']), "");
2776-
assert_eq!(trim_chars("foo", ['*', ' ']), "foo");
2753+
assert_eq!(" *** foo *** ".trim_chars([]), " *** foo *** ");
2754+
assert_eq!(" *** foo *** ".trim_chars(['*', ' ']), "foo");
2755+
assert_eq!(" *** *** ".trim_chars(['*', ' ']), "");
2756+
assert_eq!("foo".trim_chars(['*', ' ']), "foo");
27772757
}
27782758

27792759
#[test]
27802760
fn test_trim_left() {
2781-
assert_eq!(trim_left(""), "");
2782-
assert_eq!(trim_left("a"), "a");
2783-
assert_eq!(trim_left(" "), "");
2784-
assert_eq!(trim_left(" blah"), "blah");
2785-
assert_eq!(trim_left(" \u3000 wut"), "wut");
2786-
assert_eq!(trim_left("hey "), "hey ");
2761+
assert_eq!("".trim_left(), "");
2762+
assert_eq!("a".trim_left(), "a");
2763+
assert_eq!(" ".trim_left(), "");
2764+
assert_eq!(" blah".trim_left(), "blah");
2765+
assert_eq!(" \u3000 wut".trim_left(), "wut");
2766+
assert_eq!("hey ".trim_left(), "hey ");
27872767
}
27882768

27892769
#[test]
27902770
fn test_trim_right() {
2791-
assert_eq!(trim_right(""), "");
2792-
assert_eq!(trim_right("a"), "a");
2793-
assert_eq!(trim_right(" "), "");
2794-
assert_eq!(trim_right("blah "), "blah");
2795-
assert_eq!(trim_right("wut \u3000 "), "wut");
2796-
assert_eq!(trim_right(" hey"), " hey");
2771+
assert_eq!("".trim_right(), "");
2772+
assert_eq!("a".trim_right(), "a");
2773+
assert_eq!(" ".trim_right(), "");
2774+
assert_eq!("blah ".trim_right(), "blah");
2775+
assert_eq!("wut \u3000 ".trim_right(), "wut");
2776+
assert_eq!(" hey".trim_right(), " hey");
27972777
}
27982778

27992779
#[test]
28002780
fn test_trim() {
2801-
assert_eq!(trim(""), "");
2802-
assert_eq!(trim("a"), "a");
2803-
assert_eq!(trim(" "), "");
2804-
assert_eq!(trim(" blah "), "blah");
2805-
assert_eq!(trim("\nwut \u3000 "), "wut");
2806-
assert_eq!(trim(" hey dude "), "hey dude");
2781+
assert_eq!("".trim(), "");
2782+
assert_eq!("a".trim(), "a");
2783+
assert_eq!(" ".trim(), "");
2784+
assert_eq!(" blah ".trim(), "blah");
2785+
assert_eq!("\nwut \u3000 ".trim(), "wut");
2786+
assert_eq!(" hey dude ".trim(), "hey dude");
28072787
}
28082788

28092789
#[test]

0 commit comments

Comments
 (0)