Skip to content

Commit

Permalink
Switched over substr and trim functions in str to be non-allocating, …
Browse files Browse the repository at this point in the history
…temporary renamed them to better track use-sites
  • Loading branch information
Kimundi committed Mar 21, 2013
1 parent ed25a67 commit ee2f3d9
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 124 deletions.
4 changes: 2 additions & 2 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1363,11 +1363,11 @@ let crayon_names = crayons.map(|v| crayon_to_str(*v));
let favorite_crayon_name = crayon_names[0];
// Remove whitespace from before and after the string
let new_favorite_crayon_name = favorite_crayon_name.trim();
let new_favorite_crayon_name = favorite_crayon_name.trim_DBGBRWD();
if favorite_crayon_name.len() > 5 {
// Create a substring
println(favorite_crayon_name.substr(0, 5));
println(favorite_crayon_name.substr_DBGBRWD(0, 5));
}
~~~

Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn run_debuginfo_test(config: config, props: TestProps, testfile: &Path) {
// output (in order)
let mut i = 0u;
for str::lines(ProcRes.stdout).each |line| {
if props.check_lines[i].trim() == line.trim() {
if props.check_lines[i].trim_DBGBRWD() == line.trim_DBGBRWD() {
i += 1u;
}
if i == num_check_lines {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub use path::WindowsPath;
pub use path::PosixPath;

pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};
pub use str::{StrSlice, Trimmable};
pub use str::{StrSlice};
pub use container::{Container, Mutable};
pub use vec::{CopyableVector, ImmutableVector};
pub use vec::{ImmutableEqVector, ImmutableCopyableVector};
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use path::Path;
pub use path::PosixPath;
pub use path::WindowsPath;
pub use ptr::Ptr;
pub use str::{StrSlice, Trimmable, OwnedStr};
pub use str::{StrSlice, OwnedStr};
pub use to_bytes::IterBytes;
pub use to_str::ToStr;
pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};
Expand Down
186 changes: 99 additions & 87 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use str;
use u8;
use uint;
use vec;
use to_str::ToStr;

#[cfg(notest)] use cmp::{Eq, Ord};

Expand All @@ -53,6 +54,19 @@ pub pure fn from_slice(s: &str) -> ~str {
unsafe { raw::slice_bytes_unique(s, 0, len(s)) }
}

impl ToStr for ~str {
#[inline(always)]
pure fn to_str(&self) -> ~str { copy *self }
}
impl ToStr for &'self str {
#[inline(always)]
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
}
impl ToStr for @str {
#[inline(always)]
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
}

/**
* Convert a byte to a UTF-8 string
*
Expand Down Expand Up @@ -299,12 +313,12 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
* * chars_to_trim - A vector of chars
*
*/
pub pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
if chars_to_trim.is_empty() { return from_slice(s); }
pub pure fn trim_left_chars_DBGBRWD(s: &'a str, chars_to_trim: &[char]) -> &'a str {
if chars_to_trim.is_empty() { return s; }

match find(s, |c| !chars_to_trim.contains(&c)) {
None => ~"",
Some(first) => unsafe { raw::slice_bytes_unique(s, first, s.len()) }
None => "",
Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
}
}

Expand All @@ -317,14 +331,14 @@ pub pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
* * chars_to_trim - A vector of chars
*
*/
pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
if chars_to_trim.is_empty() { return str::from_slice(s); }
pub pure fn trim_right_chars_DBGBRWD(s: &'a str, chars_to_trim: &[char]) -> &'a str {
if chars_to_trim.is_empty() { return s; }

match rfind(s, |c| !chars_to_trim.contains(&c)) {
None => ~"",
None => "",
Some(last) => {
let next = char_range_at(s, last).next;
unsafe { raw::slice_bytes_unique(s, 0u, next) }
unsafe { raw::slice_bytes(s, 0u, next) }
}
}
}
Expand All @@ -338,31 +352,31 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
* * chars_to_trim - A vector of chars
*
*/
pub pure fn trim_chars(s: &str, chars_to_trim: &[char]) -> ~str {
trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
pub pure fn trim_chars_DBGBRWD(s: &'a str, chars_to_trim: &[char]) -> &'a str {
trim_left_chars_DBGBRWD(trim_right_chars_DBGBRWD(s, chars_to_trim), chars_to_trim)
}

/// Returns a string with leading whitespace removed
pub pure fn trim_left(s: &str) -> ~str {
pub pure fn trim_left_DBGBRWD(s: &'a str) -> &'a str {
match find(s, |c| !char::is_whitespace(c)) {
None => ~"",
Some(first) => unsafe { raw::slice_bytes_unique(s, first, len(s)) }
None => "",
Some(first) => unsafe { raw::slice_bytes(s, first, len(s)) }
}
}

/// Returns a string with trailing whitespace removed
pub pure fn trim_right(s: &str) -> ~str {
pub pure fn trim_right_DBGBRWD(s: &'a str) -> &'a str {
match rfind(s, |c| !char::is_whitespace(c)) {
None => ~"",
None => "",
Some(last) => {
let next = char_range_at(s, last).next;
unsafe { raw::slice_bytes_unique(s, 0u, next) }
unsafe { raw::slice_bytes(s, 0u, next) }
}
}
}

/// Returns a string with leading and trailing whitespace removed
pub pure fn trim(s: &str) -> ~str { trim_left(trim_right(s)) }
pub pure fn trim_DBGBRWD(s: &'a str) -> &'a str { trim_left_DBGBRWD(trim_right_DBGBRWD(s)) }

/*
Section: Transforming strings
Expand Down Expand Up @@ -407,8 +421,8 @@ pub pure fn chars(s: &str) -> ~[char] {
* Returns a string containing `n` characters starting at byte offset
* `begin`.
*/
pub pure fn substr(s: &str, begin: uint, n: uint) -> ~str {
slice(s, begin, begin + count_bytes(s, begin, n)).to_owned()
pub pure fn substr_DBGBRWD(s: &'a str, begin: uint, n: uint) -> &'a str {
slice(s, begin, begin + count_bytes(s, begin, n))
}

/**
Expand Down Expand Up @@ -2221,25 +2235,6 @@ pub mod raw {
}
pub trait Trimmable {
pure fn trim(&self) -> Self;
pure fn trim_left(&self) -> Self;
pure fn trim_right(&self) -> Self;
}
/// Extension methods for strings
impl Trimmable for ~str {
/// Returns a string with leading and trailing whitespace removed
#[inline]
pure fn trim(&self) -> ~str { trim(*self) }
/// Returns a string with leading whitespace removed
#[inline]
pure fn trim_left(&self) -> ~str { trim_left(*self) }
/// Returns a string with trailing whitespace removed
#[inline]
pure fn trim_right(&self) -> ~str { trim_right(*self) }
}
#[cfg(notest)]
pub mod traits {
use ops::Add;
Expand Down Expand Up @@ -2280,14 +2275,17 @@ pub trait StrSlice {
pure fn split_char(&self, sep: char) -> ~[~str];
pure fn split_str(&self, sep: &'a str) -> ~[~str];
pure fn starts_with(&self, needle: &'a str) -> bool;
pure fn substr(&self, begin: uint, n: uint) -> ~str;
pure fn substr_DBGBRWD(&self, begin: uint, n: uint) -> &'self str;
pure fn to_lower(&self) -> ~str;
pure fn to_upper(&self) -> ~str;
pure fn escape_default(&self) -> ~str;
pure fn escape_unicode(&self) -> ~str;
pure fn trim(&self) -> ~str;
pure fn trim_left(&self) -> ~str;
pure fn trim_right(&self) -> ~str;
pure fn trim_DBGBRWD(&self) -> &'self str;
pure fn trim_left_DBGBRWD(&self) -> &'self str;
pure fn trim_right_DBGBRWD(&self) -> &'self str;
pure fn trim_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str;
pure fn trim_left_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str;
pure fn trim_right_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str;
pure fn to_owned(&self) -> ~str;
pure fn to_managed(&self) -> @str;
pure fn char_at(&self, i: uint) -> char;
Expand Down Expand Up @@ -2421,8 +2419,8 @@ impl StrSlice for &'self str {
* `begin`.
*/
#[inline]
pure fn substr(&self, begin: uint, n: uint) -> ~str {
substr(*self, begin, n)
pure fn substr_DBGBRWD(&self, begin: uint, n: uint) -> &'self str {
substr_DBGBRWD(*self, begin, n)
}
/// Convert a string to lowercase
#[inline]
Expand All @@ -2439,13 +2437,27 @@ impl StrSlice for &'self str {
/// Returns a string with leading and trailing whitespace removed
#[inline]
pure fn trim(&self) -> ~str { trim(*self) }
pure fn trim_DBGBRWD(&self) -> &'self str { trim_DBGBRWD(*self) }
/// Returns a string with leading whitespace removed
#[inline]
pure fn trim_left(&self) -> ~str { trim_left(*self) }
pure fn trim_left_DBGBRWD(&self) -> &'self str { trim_left_DBGBRWD(*self) }
/// Returns a string with trailing whitespace removed
#[inline]
pure fn trim_right(&self) -> ~str { trim_right(*self) }
pure fn trim_right_DBGBRWD(&self) -> &'self str { trim_right_DBGBRWD(*self) }
#[inline]
pure fn trim_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str {
trim_chars_DBGBRWD(*self, chars_to_trim)
}
#[inline]
pure fn trim_left_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str {
trim_left_chars_DBGBRWD(*self, chars_to_trim)
}
#[inline]
pure fn trim_right_chars_DBGBRWD(&self, chars_to_trim: &[char]) -> &'self str {
trim_right_chars_DBGBRWD(*self, chars_to_trim)
}
#[inline]
pure fn to_owned(&self) -> ~str { from_slice(*self) }
Expand Down Expand Up @@ -2805,11 +2817,11 @@ mod tests {
#[test]
fn test_substr() {
fn t(a: &str, b: &str, start: int) {
fail_unless!(substr(a, start as uint, len(b)) == b.to_str());
fail_unless!(substr_DBGBRWD(a, start as uint, len(b)) == b);
}
t(~"hello", ~"llo", 2);
t(~"hello", ~"el", 1);
fail_unless!(~"ะเทศไท" == substr(~"ประเทศไทย中华Việt Nam", 6u, 6u));
t("hello", "llo", 2);
t("hello", "el", 1);
fail_unless!("ะเทศไท" == substr_DBGBRWD("ประเทศไทย中华Việt Nam", 6u, 6u));
}

#[test]
Expand Down Expand Up @@ -3042,62 +3054,62 @@ mod tests {

#[test]
fn test_trim_left_chars() {
fail_unless!(trim_left_chars(~" *** foo *** ", ~[]) ==
~" *** foo *** ");
fail_unless!(trim_left_chars(~" *** foo *** ", ~['*', ' ']) ==
~"foo *** ");
fail_unless!(trim_left_chars(~" *** *** ", ~['*', ' ']) == ~"");
fail_unless!(trim_left_chars(~"foo *** ", ~['*', ' ']) ==
~"foo *** ");
fail_unless!(trim_left_chars_DBGBRWD(" *** foo *** ", ~[]) ==
" *** foo *** ");
fail_unless!(trim_left_chars_DBGBRWD(" *** foo *** ", ~['*', ' ']) ==
"foo *** ");
fail_unless!(trim_left_chars_DBGBRWD(" *** *** ", ~['*', ' ']) == "");
fail_unless!(trim_left_chars_DBGBRWD("foo *** ", ~['*', ' ']) ==
"foo *** ");
}

#[test]
fn test_trim_right_chars() {
fail_unless!(trim_right_chars(~" *** foo *** ", ~[]) ==
~" *** foo *** ");
fail_unless!(trim_right_chars(~" *** foo *** ", ~['*', ' ']) ==
~" *** foo");
fail_unless!(trim_right_chars(~" *** *** ", ~['*', ' ']) == ~"");
fail_unless!(trim_right_chars(~" *** foo", ~['*', ' ']) ==
~" *** foo");
fail_unless!(trim_right_chars_DBGBRWD(" *** foo *** ", ~[]) ==
" *** foo *** ");
fail_unless!(trim_right_chars_DBGBRWD(" *** foo *** ", ~['*', ' ']) ==
" *** foo");
fail_unless!(trim_right_chars_DBGBRWD(" *** *** ", ~['*', ' ']) == "");
fail_unless!(trim_right_chars_DBGBRWD(" *** foo", ~['*', ' ']) ==
" *** foo");
}

#[test]
fn test_trim_chars() {
fail_unless!(trim_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ");
fail_unless!(trim_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo");
fail_unless!(trim_chars(~" *** *** ", ~['*', ' ']) == ~"");
fail_unless!(trim_chars(~"foo", ~['*', ' ']) == ~"foo");
fail_unless!(trim_chars_DBGBRWD(" *** foo *** ", ~[]) == " *** foo *** ");
fail_unless!(trim_chars_DBGBRWD(" *** foo *** ", ~['*', ' ']) == "foo");
fail_unless!(trim_chars_DBGBRWD(" *** *** ", ~['*', ' ']) == "");
fail_unless!(trim_chars_DBGBRWD("foo", ~['*', ' ']) == "foo");
}

#[test]
fn test_trim_left() {
fail_unless!((trim_left(~"") == ~""));
fail_unless!((trim_left(~"a") == ~"a"));
fail_unless!((trim_left(~" ") == ~""));
fail_unless!((trim_left(~" blah") == ~"blah"));
fail_unless!((trim_left(~" \u3000 wut") == ~"wut"));
fail_unless!((trim_left(~"hey ") == ~"hey "));
fail_unless!((trim_left_DBGBRWD("") == ""));
fail_unless!((trim_left_DBGBRWD("a") == "a"));
fail_unless!((trim_left_DBGBRWD(" ") == ""));
fail_unless!((trim_left_DBGBRWD(" blah") == "blah"));
fail_unless!((trim_left_DBGBRWD(" \u3000 wut") == "wut"));
fail_unless!((trim_left_DBGBRWD("hey ") == "hey "));
}

#[test]
fn test_trim_right() {
fail_unless!((trim_right(~"") == ~""));
fail_unless!((trim_right(~"a") == ~"a"));
fail_unless!((trim_right(~" ") == ~""));
fail_unless!((trim_right(~"blah ") == ~"blah"));
fail_unless!((trim_right(~"wut \u3000 ") == ~"wut"));
fail_unless!((trim_right(~" hey") == ~" hey"));
fail_unless!((trim_right_DBGBRWD("") == ""));
fail_unless!((trim_right_DBGBRWD("a") == "a"));
fail_unless!((trim_right_DBGBRWD(" ") == ""));
fail_unless!((trim_right_DBGBRWD("blah ") == "blah"));
fail_unless!((trim_right_DBGBRWD("wut \u3000 ") == "wut"));
fail_unless!((trim_right_DBGBRWD(" hey") == " hey"));
}

#[test]
fn test_trim() {
fail_unless!((trim(~"") == ~""));
fail_unless!((trim(~"a") == ~"a"));
fail_unless!((trim(~" ") == ~""));
fail_unless!((trim(~" blah ") == ~"blah"));
fail_unless!((trim(~"\nwut \u3000 ") == ~"wut"));
fail_unless!((trim(~" hey dude ") == ~"hey dude"));
fail_unless!((trim_DBGBRWD("") == ""));
fail_unless!((trim_DBGBRWD("a") == "a"));
fail_unless!((trim_DBGBRWD(" ") == ""));
fail_unless!((trim_DBGBRWD(" blah ") == "blah"));
fail_unless!((trim_DBGBRWD("\nwut \u3000 ") == "wut"));
fail_unless!((trim_DBGBRWD(" hey dude ") == "hey dude"));
}

#[test]
Expand Down
12 changes: 0 additions & 12 deletions src/libcore/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ impl ToStr for () {
#[inline(always)]
pure fn to_str(&self) -> ~str { ~"()" }
}
impl ToStr for ~str {
#[inline(always)]
pure fn to_str(&self) -> ~str { copy *self }
}
impl ToStr for &'self str {
#[inline(always)]
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
}
impl ToStr for @str {
#[inline(always)]
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
}

// FIXME #4898: impl for one-tuples

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/unstable/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub mod ct {
'o' as u8 => TyOctal,
'f' as u8 => TyFloat,
'?' as u8 => TyPoly,
_ => err(~"unknown type in conversion: " + s.substr(i, 1))
_ => err(~"unknown type in conversion: " + s.substr_DBGBRWD(i, 1))
};
Parsed::new(t, i + 1)
Expand Down Expand Up @@ -537,7 +537,7 @@ pub mod rt {
let mut unpadded = match cv.precision {
CountImplied => s.to_owned(),
CountIs(max) => if (max as uint) < str::char_len(s) {
str::substr(s, 0, max as uint)
str::substr_DBGBRWD(s, 0, max as uint).to_owned()
} else {
s.to_owned()
}
Expand Down
Loading

0 comments on commit ee2f3d9

Please sign in to comment.