@@ -29,6 +29,7 @@ use str;
2929use u8;
3030use uint;
3131use 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, 0 u, next) }
341+ unsafe { raw:: slice_bytes ( s, 0 u, 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, 0 u, next) }
373+ unsafe { raw:: slice_bytes ( s, 0 u, 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/*
368382Section: 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)]
22442239pub 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 ", 6 u, 6 u) ) ;
2822+ t( "hello", "llo", 2 ) ;
2823+ t( "hello", "el", 1 ) ;
2824+ fail_unless!( "ะเทศไท" == substr( "ประเทศไทย中华Việt Nam ", 6 u, 6 u) ) ;
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( ~ "\n wut \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( "\n wut \u3000 ") == "wut") ) ;
3112+ fail_unless!( ( trim( " hey dude ") == "hey dude") ) ;
31013113 }
31023114
31033115 #[ test]
0 commit comments