@@ -29,6 +29,7 @@ use str;
29
29
use u8;
30
30
use uint;
31
31
use vec;
32
+ use to_str:: ToStr ;
32
33
33
34
#[ cfg( notest) ] use cmp:: { Eq , Ord } ;
34
35
@@ -53,6 +54,19 @@ pub pure fn from_slice(s: &str) -> ~str {
53
54
unsafe { raw:: slice_bytes_unique ( s, 0 , len ( s) ) }
54
55
}
55
56
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
+
56
70
/**
57
71
* Convert a byte to a UTF-8 string
58
72
*
@@ -299,12 +313,12 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
299
313
* * chars_to_trim - A vector of chars
300
314
*
301
315
*/
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 ; }
304
318
305
319
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 ( ) ) }
308
322
}
309
323
}
310
324
@@ -317,14 +331,14 @@ pub pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
317
331
* * chars_to_trim - A vector of chars
318
332
*
319
333
*/
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 ; }
322
336
323
337
match rfind ( s, |c| !chars_to_trim. contains ( & c) ) {
324
- None => ~ "",
338
+ None => "" ,
325
339
Some ( last) => {
326
340
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) }
328
342
}
329
343
}
330
344
}
@@ -338,31 +352,31 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
338
352
* * chars_to_trim - A vector of chars
339
353
*
340
354
*/
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 {
342
356
trim_left_chars ( trim_right_chars ( s, chars_to_trim) , chars_to_trim)
343
357
}
344
358
345
359
/// 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 {
347
361
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) ) }
350
364
}
351
365
}
352
366
353
367
/// 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 {
355
369
match rfind ( s, |c| !char:: is_whitespace ( c) ) {
356
- None => ~ "",
370
+ None => "" ,
357
371
Some ( last) => {
358
372
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) }
360
374
}
361
375
}
362
376
}
363
377
364
378
/// 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) ) }
366
380
367
381
/*
368
382
Section: Transforming strings
@@ -407,8 +421,8 @@ pub pure fn chars(s: &str) -> ~[char] {
407
421
* Returns a string containing `n` characters starting at byte offset
408
422
* `begin`.
409
423
*/
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) )
412
426
}
413
427
414
428
/**
@@ -2221,25 +2235,6 @@ pub mod raw {
2221
2235
2222
2236
}
2223
2237
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
-
2243
2238
#[cfg(notest)]
2244
2239
pub mod traits {
2245
2240
use ops::Add;
@@ -2280,14 +2275,17 @@ pub trait StrSlice {
2280
2275
pure fn split_char(&self, sep: char) -> ~[~str];
2281
2276
pure fn split_str(&self, sep: &'a str) -> ~[~str];
2282
2277
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;
2284
2279
pure fn to_lower(&self) -> ~str;
2285
2280
pure fn to_upper(&self) -> ~str;
2286
2281
pure fn escape_default(&self) -> ~str;
2287
2282
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;
2291
2289
pure fn to_owned(&self) -> ~str;
2292
2290
pure fn to_managed(&self) -> @str;
2293
2291
pure fn char_at(&self, i: uint) -> char;
@@ -2421,7 +2419,7 @@ impl StrSlice for &'self str {
2421
2419
* `begin`.
2422
2420
*/
2423
2421
#[inline]
2424
- pure fn substr(&self, begin: uint, n: uint) -> ~ str {
2422
+ pure fn substr(&self, begin: uint, n: uint) -> &'self str {
2425
2423
substr(*self, begin, n)
2426
2424
}
2427
2425
/// Convert a string to lowercase
@@ -2439,13 +2437,27 @@ impl StrSlice for &'self str {
2439
2437
2440
2438
/// Returns a string with leading and trailing whitespace removed
2441
2439
#[inline]
2442
- pure fn trim(&self) -> ~ str { trim(*self) }
2440
+ pure fn trim(&self) -> &'self str { trim(*self) }
2443
2441
/// Returns a string with leading whitespace removed
2444
2442
#[inline]
2445
- pure fn trim_left(&self) -> ~ str { trim_left(*self) }
2443
+ pure fn trim_left(&self) -> &'self str { trim_left(*self) }
2446
2444
/// Returns a string with trailing whitespace removed
2447
2445
#[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
+
2449
2461
2450
2462
#[inline]
2451
2463
pure fn to_owned(&self) -> ~str { from_slice(*self) }
@@ -2805,11 +2817,11 @@ mod tests {
2805
2817
#[ test]
2806
2818
fn test_substr( ) {
2807
2819
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) ;
2809
2821
}
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) ) ;
2813
2825
}
2814
2826
2815
2827
#[ test]
@@ -3042,62 +3054,62 @@ mod tests {
3042
3054
3043
3055
#[ test]
3044
3056
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 * * * ") ;
3052
3064
}
3053
3065
3054
3066
#[ test]
3055
3067
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") ;
3063
3075
}
3064
3076
3065
3077
#[ test]
3066
3078
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") ;
3071
3083
}
3072
3084
3073
3085
#[ test]
3074
3086
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 ") ) ;
3081
3093
}
3082
3094
3083
3095
#[ test]
3084
3096
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") ) ;
3091
3103
}
3092
3104
3093
3105
#[ test]
3094
3106
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") ) ;
3101
3113
}
3102
3114
3103
3115
#[ test]
0 commit comments