Skip to content

Commit 5f2d410

Browse files
committed
auto merge of #5479 : Kimundi/rust/str-dealloc, r=z0w0
This makes the `trim` and `substr` functions return a slice instead of an `~str`, and removes the unnecessary `Trimmable` trait (`StrSlice` already contains the same functionality). Also moves the `ToStr` implementations for the three str types into the str module in anticipation of further untangling.
2 parents d8c0da3 + 0a47cd5 commit 5f2d410

File tree

14 files changed

+116
-110
lines changed

14 files changed

+116
-110
lines changed

src/libcore/core.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub use path::WindowsPath;
198198
pub use path::PosixPath;
199199

200200
pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};
201-
pub use str::{StrSlice, Trimmable};
201+
pub use str::{StrSlice};
202202
pub use container::{Container, Mutable};
203203
pub use vec::{CopyableVector, ImmutableVector};
204204
pub use vec::{ImmutableEqVector, ImmutableCopyableVector};

src/libcore/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use path::Path;
3636
pub use path::PosixPath;
3737
pub use path::WindowsPath;
3838
pub use ptr::Ptr;
39-
pub use str::{StrSlice, Trimmable, OwnedStr};
39+
pub use str::{StrSlice, OwnedStr};
4040
pub use to_bytes::IterBytes;
4141
pub use to_str::ToStr;
4242
pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};

src/libcore/str.rs

Lines changed: 97 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use str;
2929
use u8;
3030
use uint;
3131
use vec;
32+
use to_str::ToStr;
3233

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

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

57+
impl ToStr for ~str {
58+
#[inline(always)]
59+
pure fn to_str(&self) -> ~str { copy *self }
60+
}
61+
impl ToStr for &'self str {
62+
#[inline(always)]
63+
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
64+
}
65+
impl ToStr for @str {
66+
#[inline(always)]
67+
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
68+
}
69+
5670
/**
5771
* Convert a byte to a UTF-8 string
5872
*
@@ -299,12 +313,12 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
299313
* * chars_to_trim - A vector of chars
300314
*
301315
*/
302-
pub pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
303-
if chars_to_trim.is_empty() { return from_slice(s); }
316+
pub pure fn trim_left_chars(s: &'a str, chars_to_trim: &[char]) -> &'a str {
317+
if chars_to_trim.is_empty() { return s; }
304318

305319
match find(s, |c| !chars_to_trim.contains(&c)) {
306-
None => ~"",
307-
Some(first) => unsafe { raw::slice_bytes_unique(s, first, s.len()) }
320+
None => "",
321+
Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
308322
}
309323
}
310324

@@ -317,14 +331,14 @@ pub pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
317331
* * chars_to_trim - A vector of chars
318332
*
319333
*/
320-
pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
321-
if chars_to_trim.is_empty() { return str::from_slice(s); }
334+
pub pure fn trim_right_chars(s: &'a str, chars_to_trim: &[char]) -> &'a str {
335+
if chars_to_trim.is_empty() { return s; }
322336

323337
match rfind(s, |c| !chars_to_trim.contains(&c)) {
324-
None => ~"",
338+
None => "",
325339
Some(last) => {
326340
let next = char_range_at(s, last).next;
327-
unsafe { raw::slice_bytes_unique(s, 0u, next) }
341+
unsafe { raw::slice_bytes(s, 0u, next) }
328342
}
329343
}
330344
}
@@ -338,31 +352,31 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
338352
* * chars_to_trim - A vector of chars
339353
*
340354
*/
341-
pub pure fn trim_chars(s: &str, chars_to_trim: &[char]) -> ~str {
355+
pub pure fn trim_chars(s: &'a str, chars_to_trim: &[char]) -> &'a str {
342356
trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
343357
}
344358

345359
/// Returns a string with leading whitespace removed
346-
pub pure fn trim_left(s: &str) -> ~str {
360+
pub pure fn trim_left(s: &'a str) -> &'a str {
347361
match find(s, |c| !char::is_whitespace(c)) {
348-
None => ~"",
349-
Some(first) => unsafe { raw::slice_bytes_unique(s, first, len(s)) }
362+
None => "",
363+
Some(first) => unsafe { raw::slice_bytes(s, first, len(s)) }
350364
}
351365
}
352366

353367
/// Returns a string with trailing whitespace removed
354-
pub pure fn trim_right(s: &str) -> ~str {
368+
pub pure fn trim_right(s: &'a str) -> &'a str {
355369
match rfind(s, |c| !char::is_whitespace(c)) {
356-
None => ~"",
370+
None => "",
357371
Some(last) => {
358372
let next = char_range_at(s, last).next;
359-
unsafe { raw::slice_bytes_unique(s, 0u, next) }
373+
unsafe { raw::slice_bytes(s, 0u, next) }
360374
}
361375
}
362376
}
363377

364378
/// Returns a string with leading and trailing whitespace removed
365-
pub pure fn trim(s: &str) -> ~str { trim_left(trim_right(s)) }
379+
pub pure fn trim(s: &'a str) -> &'a str { trim_left(trim_right(s)) }
366380

367381
/*
368382
Section: Transforming strings
@@ -407,8 +421,8 @@ pub pure fn chars(s: &str) -> ~[char] {
407421
* Returns a string containing `n` characters starting at byte offset
408422
* `begin`.
409423
*/
410-
pub pure fn substr(s: &str, begin: uint, n: uint) -> ~str {
411-
slice(s, begin, begin + count_bytes(s, begin, n)).to_owned()
424+
pub pure fn substr(s: &'a str, begin: uint, n: uint) -> &'a str {
425+
slice(s, begin, begin + count_bytes(s, begin, n))
412426
}
413427

414428
/**
@@ -2221,25 +2235,6 @@ pub mod raw {
22212235
22222236
}
22232237
2224-
pub trait Trimmable {
2225-
pure fn trim(&self) -> Self;
2226-
pure fn trim_left(&self) -> Self;
2227-
pure fn trim_right(&self) -> Self;
2228-
}
2229-
2230-
/// Extension methods for strings
2231-
impl Trimmable for ~str {
2232-
/// Returns a string with leading and trailing whitespace removed
2233-
#[inline]
2234-
pure fn trim(&self) -> ~str { trim(*self) }
2235-
/// Returns a string with leading whitespace removed
2236-
#[inline]
2237-
pure fn trim_left(&self) -> ~str { trim_left(*self) }
2238-
/// Returns a string with trailing whitespace removed
2239-
#[inline]
2240-
pure fn trim_right(&self) -> ~str { trim_right(*self) }
2241-
}
2242-
22432238
#[cfg(notest)]
22442239
pub mod traits {
22452240
use ops::Add;
@@ -2280,14 +2275,17 @@ pub trait StrSlice {
22802275
pure fn split_char(&self, sep: char) -> ~[~str];
22812276
pure fn split_str(&self, sep: &'a str) -> ~[~str];
22822277
pure fn starts_with(&self, needle: &'a str) -> bool;
2283-
pure fn substr(&self, begin: uint, n: uint) -> ~str;
2278+
pure fn substr(&self, begin: uint, n: uint) -> &'self str;
22842279
pure fn to_lower(&self) -> ~str;
22852280
pure fn to_upper(&self) -> ~str;
22862281
pure fn escape_default(&self) -> ~str;
22872282
pure fn escape_unicode(&self) -> ~str;
2288-
pure fn trim(&self) -> ~str;
2289-
pure fn trim_left(&self) -> ~str;
2290-
pure fn trim_right(&self) -> ~str;
2283+
pure fn trim(&self) -> &'self str;
2284+
pure fn trim_left(&self) -> &'self str;
2285+
pure fn trim_right(&self) -> &'self str;
2286+
pure fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str;
2287+
pure fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str;
2288+
pure fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str;
22912289
pure fn to_owned(&self) -> ~str;
22922290
pure fn to_managed(&self) -> @str;
22932291
pure fn char_at(&self, i: uint) -> char;
@@ -2421,7 +2419,7 @@ impl StrSlice for &'self str {
24212419
* `begin`.
24222420
*/
24232421
#[inline]
2424-
pure fn substr(&self, begin: uint, n: uint) -> ~str {
2422+
pure fn substr(&self, begin: uint, n: uint) -> &'self str {
24252423
substr(*self, begin, n)
24262424
}
24272425
/// Convert a string to lowercase
@@ -2439,13 +2437,27 @@ impl StrSlice for &'self str {
24392437
24402438
/// Returns a string with leading and trailing whitespace removed
24412439
#[inline]
2442-
pure fn trim(&self) -> ~str { trim(*self) }
2440+
pure fn trim(&self) -> &'self str { trim(*self) }
24432441
/// Returns a string with leading whitespace removed
24442442
#[inline]
2445-
pure fn trim_left(&self) -> ~str { trim_left(*self) }
2443+
pure fn trim_left(&self) -> &'self str { trim_left(*self) }
24462444
/// Returns a string with trailing whitespace removed
24472445
#[inline]
2448-
pure fn trim_right(&self) -> ~str { trim_right(*self) }
2446+
pure fn trim_right(&self) -> &'self str { trim_right(*self) }
2447+
2448+
#[inline]
2449+
pure fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str {
2450+
trim_chars(*self, chars_to_trim)
2451+
}
2452+
#[inline]
2453+
pure fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str {
2454+
trim_left_chars(*self, chars_to_trim)
2455+
}
2456+
#[inline]
2457+
pure fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str {
2458+
trim_right_chars(*self, chars_to_trim)
2459+
}
2460+
24492461
24502462
#[inline]
24512463
pure fn to_owned(&self) -> ~str { from_slice(*self) }
@@ -2805,11 +2817,11 @@ mod tests {
28052817
#[test]
28062818
fn test_substr() {
28072819
fn t(a: &str, b: &str, start: int) {
2808-
fail_unless!(substr(a, start as uint, len(b)) == b.to_str());
2820+
fail_unless!(substr(a, start as uint, len(b)) == b);
28092821
}
2810-
t(~"hello", ~"llo", 2);
2811-
t(~"hello", ~"el", 1);
2812-
fail_unless!(~"ะเทศไท" == substr(~"ประเทศไทย中华Việt Nam", 6u, 6u));
2822+
t("hello", "llo", 2);
2823+
t("hello", "el", 1);
2824+
fail_unless!("ะเทศไท" == substr("ประเทศไทย中华Việt Nam", 6u, 6u));
28132825
}
28142826

28152827
#[test]
@@ -3042,62 +3054,62 @@ mod tests {
30423054

30433055
#[test]
30443056
fn test_trim_left_chars() {
3045-
fail_unless!(trim_left_chars(~" *** foo *** ", ~[]) ==
3046-
~" *** foo *** ");
3047-
fail_unless!(trim_left_chars(~" *** foo *** ", ~['*', ' ']) ==
3048-
~"foo *** ");
3049-
fail_unless!(trim_left_chars(~" *** *** ", ~['*', ' ']) == ~"");
3050-
fail_unless!(trim_left_chars(~"foo *** ", ~['*', ' ']) ==
3051-
~"foo *** ");
3057+
fail_unless!(trim_left_chars(" *** foo *** ", ~[]) ==
3058+
" *** foo *** ");
3059+
fail_unless!(trim_left_chars(" *** foo *** ", ~['*', ' ']) ==
3060+
"foo *** ");
3061+
fail_unless!(trim_left_chars(" *** *** ", ~['*', ' ']) == "");
3062+
fail_unless!(trim_left_chars("foo *** ", ~['*', ' ']) ==
3063+
"foo *** ");
30523064
}
30533065

30543066
#[test]
30553067
fn test_trim_right_chars() {
3056-
fail_unless!(trim_right_chars(~" *** foo *** ", ~[]) ==
3057-
~" *** foo *** ");
3058-
fail_unless!(trim_right_chars(~" *** foo *** ", ~['*', ' ']) ==
3059-
~" *** foo");
3060-
fail_unless!(trim_right_chars(~" *** *** ", ~['*', ' ']) == ~"");
3061-
fail_unless!(trim_right_chars(~" *** foo", ~['*', ' ']) ==
3062-
~" *** foo");
3068+
fail_unless!(trim_right_chars(" *** foo *** ", ~[]) ==
3069+
" *** foo *** ");
3070+
fail_unless!(trim_right_chars(" *** foo *** ", ~['*', ' ']) ==
3071+
" *** foo");
3072+
fail_unless!(trim_right_chars(" *** *** ", ~['*', ' ']) == "");
3073+
fail_unless!(trim_right_chars(" *** foo", ~['*', ' ']) ==
3074+
" *** foo");
30633075
}
30643076

30653077
#[test]
30663078
fn test_trim_chars() {
3067-
fail_unless!(trim_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ");
3068-
fail_unless!(trim_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo");
3069-
fail_unless!(trim_chars(~" *** *** ", ~['*', ' ']) == ~"");
3070-
fail_unless!(trim_chars(~"foo", ~['*', ' ']) == ~"foo");
3079+
fail_unless!(trim_chars(" *** foo *** ", ~[]) == " *** foo *** ");
3080+
fail_unless!(trim_chars(" *** foo *** ", ~['*', ' ']) == "foo");
3081+
fail_unless!(trim_chars(" *** *** ", ~['*', ' ']) == "");
3082+
fail_unless!(trim_chars("foo", ~['*', ' ']) == "foo");
30713083
}
30723084

30733085
#[test]
30743086
fn test_trim_left() {
3075-
fail_unless!((trim_left(~"") == ~""));
3076-
fail_unless!((trim_left(~"a") == ~"a"));
3077-
fail_unless!((trim_left(~" ") == ~""));
3078-
fail_unless!((trim_left(~" blah") == ~"blah"));
3079-
fail_unless!((trim_left(~" \u3000 wut") == ~"wut"));
3080-
fail_unless!((trim_left(~"hey ") == ~"hey "));
3087+
fail_unless!((trim_left("") == ""));
3088+
fail_unless!((trim_left("a") == "a"));
3089+
fail_unless!((trim_left(" ") == ""));
3090+
fail_unless!((trim_left(" blah") == "blah"));
3091+
fail_unless!((trim_left(" \u3000 wut") == "wut"));
3092+
fail_unless!((trim_left("hey ") == "hey "));
30813093
}
30823094

30833095
#[test]
30843096
fn test_trim_right() {
3085-
fail_unless!((trim_right(~"") == ~""));
3086-
fail_unless!((trim_right(~"a") == ~"a"));
3087-
fail_unless!((trim_right(~" ") == ~""));
3088-
fail_unless!((trim_right(~"blah ") == ~"blah"));
3089-
fail_unless!((trim_right(~"wut \u3000 ") == ~"wut"));
3090-
fail_unless!((trim_right(~" hey") == ~" hey"));
3097+
fail_unless!((trim_right("") == ""));
3098+
fail_unless!((trim_right("a") == "a"));
3099+
fail_unless!((trim_right(" ") == ""));
3100+
fail_unless!((trim_right("blah ") == "blah"));
3101+
fail_unless!((trim_right("wut \u3000 ") == "wut"));
3102+
fail_unless!((trim_right(" hey") == " hey"));
30913103
}
30923104

30933105
#[test]
30943106
fn test_trim() {
3095-
fail_unless!((trim(~"") == ~""));
3096-
fail_unless!((trim(~"a") == ~"a"));
3097-
fail_unless!((trim(~" ") == ~""));
3098-
fail_unless!((trim(~" blah ") == ~"blah"));
3099-
fail_unless!((trim(~"\nwut \u3000 ") == ~"wut"));
3100-
fail_unless!((trim(~" hey dude ") == ~"hey dude"));
3107+
fail_unless!((trim("") == ""));
3108+
fail_unless!((trim("a") == "a"));
3109+
fail_unless!((trim(" ") == ""));
3110+
fail_unless!((trim(" blah ") == "blah"));
3111+
fail_unless!((trim("\nwut \u3000 ") == "wut"));
3112+
fail_unless!((trim(" hey dude ") == "hey dude"));
31013113
}
31023114

31033115
#[test]

src/libcore/to_str.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ impl ToStr for () {
2828
#[inline(always)]
2929
pure fn to_str(&self) -> ~str { ~"()" }
3030
}
31-
impl ToStr for ~str {
32-
#[inline(always)]
33-
pure fn to_str(&self) -> ~str { copy *self }
34-
}
35-
impl ToStr for &'self str {
36-
#[inline(always)]
37-
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
38-
}
39-
impl ToStr for @str {
40-
#[inline(always)]
41-
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
42-
}
4331

4432
// FIXME #4898: impl for one-tuples
4533

src/libcore/unstable/extfmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ pub mod rt {
537537
let mut unpadded = match cv.precision {
538538
CountImplied => s.to_owned(),
539539
CountIs(max) => if (max as uint) < str::char_len(s) {
540-
str::substr(s, 0, max as uint)
540+
str::substr(s, 0, max as uint).to_owned()
541541
} else {
542542
s.to_owned()
543543
}

src/librustc/middle/trans/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,7 @@ fn trans_assign_op(bcx: block,
18491849
return result_datum.copy_to_datum(bcx, DROP_EXISTING, dst_datum);
18501850
}
18511851

1852+
// NOTE: Mode neccessary here?
18521853
fn shorten(+x: ~str) -> ~str {
1853-
if x.len() > 60 { x.substr(0, 60) } else { x }
1854+
if x.len() > 60 { x.substr(0, 60).to_owned() } else { x }
18541855
}

src/librustdoc/text_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ mod test {
311311
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
312312
let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
313313
let doc = (sectionalize_pass::mk_pass().f)(srv.clone(), doc);
314-
(mk_pass(~"", |s| str::trim(s) ).f)(srv.clone(), doc)
314+
(mk_pass(~"", |s| str::trim(s).to_owned() ).f)(srv.clone(), doc)
315315
}
316316
}
317317
}

src/librustdoc/trim_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use text_pass;
2121
use core::str;
2222

2323
pub fn mk_pass() -> Pass {
24-
text_pass::mk_pass(~"trim", |s| str::trim(s) )
24+
text_pass::mk_pass(~"trim", |s| s.trim().to_owned() )
2525
}
2626

2727
#[test]

src/librustdoc/unindent_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn unindent(s: &str) -> ~str {
7878
};
7979

8080
if !lines.is_empty() {
81-
let unindented = ~[lines.head().trim()]
81+
let unindented = ~[lines.head().trim().to_owned()]
8282
+ do lines.tail().map |line| {
8383
if str::is_whitespace(*line) {
8484
copy *line

0 commit comments

Comments
 (0)