@@ -304,63 +304,6 @@ impl<'self> StrVector for &'self [&'self str] {
304
304
}
305
305
}
306
306
307
- /*
308
- Section: Adding to and removing from a string
309
- */
310
-
311
- /**
312
- * Remove the final character from a string and return it
313
- *
314
- * # Failure
315
- *
316
- * If the string does not contain any characters
317
- */
318
- pub fn pop_char ( s : & mut ~str ) -> char {
319
- let end = s. len ( ) ;
320
- assert ! ( end > 0 u) ;
321
- let CharRange { ch, next} = s. char_range_at_reverse ( end) ;
322
- unsafe { raw:: set_len ( s, next) ; }
323
- return ch;
324
- }
325
-
326
- /**
327
- * Remove the first character from a string and return it
328
- *
329
- * # Failure
330
- *
331
- * If the string does not contain any characters
332
- */
333
- pub fn shift_char ( s : & mut ~str ) -> char {
334
- let CharRange { ch, next} = s. char_range_at ( 0 u) ;
335
- * s = unsafe { raw:: slice_bytes_owned ( * s, next, s. len ( ) ) } ;
336
- return ch;
337
- }
338
-
339
- /**
340
- * Removes the first character from a string slice and returns it. This does
341
- * not allocate a new string; instead, it mutates a slice to point one
342
- * character beyond the character that was shifted.
343
- *
344
- * # Failure
345
- *
346
- * If the string does not contain any characters
347
- */
348
- #[ inline]
349
- pub fn slice_shift_char < ' a > ( s : & ' a str ) -> ( char , & ' a str ) {
350
- let CharRange { ch, next} = s. char_range_at ( 0 u) ;
351
- let next_s = unsafe { raw:: slice_bytes ( s, next, s. len ( ) ) } ;
352
- return ( ch, next_s) ;
353
- }
354
-
355
- /// Prepend a char to a string
356
- pub fn unshift_char ( s : & mut ~str , ch : char ) {
357
- // This could be more efficient.
358
- let mut new_str = ~"";
359
- new_str. push_char ( ch) ;
360
- new_str. push_str ( * s) ;
361
- * s = new_str;
362
- }
363
-
364
307
/*
365
308
Section: Transforming strings
366
309
*/
@@ -1546,6 +1489,8 @@ pub trait StrSlice<'self> {
1546
1489
fn find_str(&self, &str) -> Option<uint>;
1547
1490
1548
1491
fn repeat(&self, nn: uint) -> ~str;
1492
+
1493
+ fn slice_shift_char(&self) -> (char, &'self str);
1549
1494
}
1550
1495
1551
1496
/// Extension methods for strings
@@ -1757,7 +1702,7 @@ impl<'self> StrSlice<'self> for &'self str {
1757
1702
else { match_at(*self, needle, 0u) }
1758
1703
}
1759
1704
/// Returns true if `needle` is a suffix of the string.
1760
- pub fn ends_with(&self, needle: &str) -> bool {
1705
+ fn ends_with(&self, needle: &str) -> bool {
1761
1706
let (self_len, needle_len) = (self.len(), needle.len());
1762
1707
if needle_len == 0u { true }
1763
1708
else if needle_len > self_len { false }
@@ -2085,13 +2030,35 @@ impl<'self> StrSlice<'self> for &'self str {
2085
2030
ret
2086
2031
}
2087
2032
}
2033
+
2034
+ /**
2035
+ * Retrieves the first character from a string slice and returns
2036
+ * it. This does not allocate a new string; instead, it returns a
2037
+ * slice that point one character beyond the character that was
2038
+ * shifted.
2039
+ *
2040
+ * # Failure
2041
+ *
2042
+ * If the string does not contain any characters
2043
+ */
2044
+ #[inline]
2045
+ fn slice_shift_char(&self) -> (char, &'self str) {
2046
+ let CharRange {ch, next} = self.char_range_at(0u);
2047
+ let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
2048
+ return (ch, next_s);
2049
+ }
2050
+
2051
+
2088
2052
}
2089
2053
2090
2054
#[allow(missing_doc)]
2091
2055
pub trait OwnedStr {
2092
2056
fn push_str_no_overallocate(&mut self, rhs: &str);
2093
2057
fn push_str(&mut self, rhs: &str);
2094
2058
fn push_char(&mut self, c: char);
2059
+ fn pop_char(&mut self) -> char;
2060
+ fn shift_char(&mut self) -> char;
2061
+ fn unshift_char(&mut self, ch: char);
2095
2062
fn append(&self, rhs: &str) -> ~str; // FIXME #4850: this should consume self.
2096
2063
fn reserve(&mut self, n: uint);
2097
2064
fn reserve_at_least(&mut self, n: uint);
@@ -2190,6 +2157,43 @@ impl OwnedStr for ~str {
2190
2157
raw::set_len(self, new_len);
2191
2158
}
2192
2159
}
2160
+ /**
2161
+ * Remove the final character from a string and return it
2162
+ *
2163
+ * # Failure
2164
+ *
2165
+ * If the string does not contain any characters
2166
+ */
2167
+ fn pop_char(&mut self) -> char {
2168
+ let end = self.len();
2169
+ assert!(end > 0u);
2170
+ let CharRange {ch, next} = self.char_range_at_reverse(end);
2171
+ unsafe { raw::set_len(self, next); }
2172
+ return ch;
2173
+ }
2174
+
2175
+ /**
2176
+ * Remove the first character from a string and return it
2177
+ *
2178
+ * # Failure
2179
+ *
2180
+ * If the string does not contain any characters
2181
+ */
2182
+ fn shift_char(&mut self) -> char {
2183
+ let CharRange {ch, next} = self.char_range_at(0u);
2184
+ *self = unsafe { raw::slice_bytes_owned(*self, next, self.len()) };
2185
+ return ch;
2186
+ }
2187
+
2188
+ /// Prepend a char to a string
2189
+ fn unshift_char(&mut self, ch: char) {
2190
+ // This could be more efficient.
2191
+ let mut new_str = ~" ";
2192
+ new_str.push_char(ch);
2193
+ new_str.push_str(*self);
2194
+ *self = new_str;
2195
+ }
2196
+
2193
2197
/// Concatenate two strings together.
2194
2198
#[inline]
2195
2199
fn append(&self, rhs: &str) -> ~str {
@@ -2421,15 +2425,15 @@ mod tests {
2421
2425
#[ test]
2422
2426
fn test_pop_char( ) {
2423
2427
let mut data = ~"ประเทศไทย中华";
2424
- let cc = pop_char ( & mut data ) ;
2428
+ let cc = data . pop_char ( ) ;
2425
2429
assert_eq!( ~"ประเทศไทย中", data);
2426
2430
assert_eq!('华', cc);
2427
2431
}
2428
2432
2429
2433
#[test]
2430
2434
fn test_pop_char_2() {
2431
2435
let mut data2 = ~" 华";
2432
- let cc2 = pop_char(&mut data2 );
2436
+ let cc2 = data2. pop_char();
2433
2437
assert_eq!(~" ", data2);
2434
2438
assert_eq!('华', cc2);
2435
2439
}
@@ -2439,7 +2443,29 @@ mod tests {
2439
2443
#[ignore(cfg(windows))]
2440
2444
fn test_pop_char_fail() {
2441
2445
let mut data = ~" ";
2442
- let _cc3 = pop_char(&mut data);
2446
+ let _cc3 = data.pop_char();
2447
+ }
2448
+
2449
+ #[test]
2450
+ fn test_push_char() {
2451
+ let mut data = ~" ประเทศไทย中";
2452
+ data.push_char('华');
2453
+ assert_eq!(~" ประเทศไทย中华", data);
2454
+ }
2455
+
2456
+ #[test]
2457
+ fn test_shift_char() {
2458
+ let mut data = ~" ประเทศไทย中";
2459
+ let cc = data.shift_char();
2460
+ assert_eq!(~" ระเทศไทย中", data);
2461
+ assert_eq!('ป', cc);
2462
+ }
2463
+
2464
+ #[test]
2465
+ fn test_unshift_char() {
2466
+ let mut data = ~" ประเทศไทย中";
2467
+ data.unshift_char('华');
2468
+ assert_eq!(~" 华ประเทศไทย中", data);
2443
2469
}
2444
2470
2445
2471
#[test]
0 commit comments