@@ -1355,6 +1355,7 @@ pub mod raw {
1355
1355
use libc;
1356
1356
use ptr;
1357
1357
use ptr:: RawPtr ;
1358
+ use option:: { Option , Some , None } ;
1358
1359
use str:: { is_utf8, OwnedStr , StrSlice } ;
1359
1360
use vec;
1360
1361
use vec:: { MutableVector , ImmutableVector , OwnedVector } ;
@@ -1465,22 +1466,28 @@ pub mod raw {
1465
1466
1466
1467
/// Removes the last byte from a string and returns it.
1467
1468
/// The caller must preserve the valid UTF-8 property.
1468
- pub unsafe fn pop_byte ( s : & mut ~str ) -> u8 {
1469
+ pub unsafe fn pop_byte ( s : & mut ~str ) -> Option < u8 > {
1469
1470
let len = s. len ( ) ;
1470
- assert ! ( ( len > 0 u) ) ;
1471
- let b = s[ len - 1 u] ;
1472
- s. set_len ( len - 1 ) ;
1473
- return b;
1471
+ if len == 0 u {
1472
+ return None ;
1473
+ } else {
1474
+ let b = s[ len - 1 u] ;
1475
+ s. set_len ( len - 1 ) ;
1476
+ return Some ( b) ;
1477
+ }
1474
1478
}
1475
1479
1476
1480
/// Removes the first byte from a string and returns it.
1477
1481
/// The caller must preserve the valid UTF-8 property.
1478
- pub unsafe fn shift_byte ( s : & mut ~str ) -> u8 {
1482
+ pub unsafe fn shift_byte ( s : & mut ~str ) -> Option < u8 > {
1479
1483
let len = s. len ( ) ;
1480
- assert ! ( ( len > 0 u) ) ;
1481
- let b = s[ 0 ] ;
1482
- * s = s. slice ( 1 , len) . to_owned ( ) ;
1483
- return b;
1484
+ if len == 0 u {
1485
+ return None ;
1486
+ } else {
1487
+ let b = s[ 0 ] ;
1488
+ * s = s. slice ( 1 , len) . to_owned ( ) ;
1489
+ return Some ( b) ;
1490
+ }
1484
1491
}
1485
1492
1486
1493
/// Access the str in its vector representation.
@@ -2291,7 +2298,7 @@ pub trait StrSlice<'a> {
2291
2298
/// assert_eq!(c, 'ö');
2292
2299
/// assert_eq!(s2, "we 老虎 Léopard");
2293
2300
/// ```
2294
- fn slice_shift_char( & self ) -> ( char , & ' a str ) ;
2301
+ fn slice_shift_char( & self ) -> ( Option < char > , & ' a str ) ;
2295
2302
2296
2303
/// Levenshtein Distance between two strings.
2297
2304
fn lev_distance( & self , t: & str ) -> uint;
@@ -2744,10 +2751,14 @@ impl<'a> StrSlice<'a> for &'a str {
2744
2751
}
2745
2752
2746
2753
#[ inline]
2747
- fn slice_shift_char( & self ) -> ( char , & ' a str ) {
2748
- let CharRange { ch, next} = self . char_range_at( 0 u) ;
2749
- let next_s = unsafe { raw:: slice_bytes( * self , next, self . len( ) ) } ;
2750
- return ( ch, next_s) ;
2754
+ fn slice_shift_char( & self ) -> ( Option <char >, & ' a str ) {
2755
+ if self . is_empty( ) {
2756
+ return ( None , * self ) ;
2757
+ } else {
2758
+ let CharRange { ch, next} = self . char_range_at( 0 u) ;
2759
+ let next_s = unsafe { raw:: slice_bytes( * self , next, self . len( ) ) } ;
2760
+ return ( Some ( ch) , next_s) ;
2761
+ }
2751
2762
}
2752
2763
2753
2764
fn lev_distance( & self , t: & str ) -> uint {
@@ -2815,14 +2826,14 @@ pub trait OwnedStr {
2815
2826
/// # Failure
2816
2827
///
2817
2828
/// If the string does not contain any characters
2818
- fn pop_char( & mut self ) -> char ;
2829
+ fn pop_char( & mut self ) -> Option < char > ;
2819
2830
2820
2831
/// Remove the first character from a string and return it
2821
2832
///
2822
2833
/// # Failure
2823
2834
///
2824
2835
/// If the string does not contain any characters
2825
- fn shift_char( & mut self ) -> char ;
2836
+ fn shift_char( & mut self ) -> Option < char > ;
2826
2837
2827
2838
/// Prepend a char to a string
2828
2839
fn unshift_char( & mut self , ch: char ) ;
@@ -2925,19 +2936,26 @@ impl OwnedStr for ~str {
2925
2936
}
2926
2937
2927
2938
#[ inline]
2928
- fn pop_char( & mut self ) -> char {
2939
+ fn pop_char( & mut self ) -> Option < char > {
2929
2940
let end = self . len( ) ;
2930
- assert!( end > 0 u) ;
2931
- let CharRange { ch, next} = self . char_range_at_reverse( end) ;
2932
- unsafe { self . set_len( next) ; }
2933
- return ch;
2941
+ if end == 0 u {
2942
+ return None ;
2943
+ } else {
2944
+ let CharRange { ch, next} = self . char_range_at_reverse( end) ;
2945
+ unsafe { self . set_len( next) ; }
2946
+ return Some ( ch) ;
2947
+ }
2934
2948
}
2935
2949
2936
2950
#[ inline]
2937
- fn shift_char( & mut self ) -> char {
2938
- let CharRange { ch, next} = self . char_range_at( 0 u) ;
2939
- * self = self . slice( next, self . len( ) ) . to_owned( ) ;
2940
- return ch;
2951
+ fn shift_char( & mut self ) -> Option <char > {
2952
+ if self . is_empty( ) {
2953
+ return None ;
2954
+ } else {
2955
+ let CharRange { ch, next} = self . char_range_at( 0 u) ;
2956
+ * self = self . slice( next, self . len( ) ) . to_owned( ) ;
2957
+ return Some ( ch) ;
2958
+ }
2941
2959
}
2942
2960
2943
2961
#[ inline]
@@ -3148,22 +3166,23 @@ mod tests {
3148
3166
let mut data = ~"ประเทศไทย中华";
3149
3167
let cc = data.pop_char();
3150
3168
assert_eq!(~" ประเทศไทย中", data);
3151
- assert_eq!('华', cc);
3169
+ assert_eq!(Some( '华') , cc);
3152
3170
}
3153
3171
3154
3172
#[test]
3155
3173
fn test_pop_char_2() {
3156
3174
let mut data2 = ~" 华";
3157
3175
let cc2 = data2.pop_char();
3158
3176
assert_eq!(~" ", data2);
3159
- assert_eq!('华', cc2);
3177
+ assert_eq!(Some( '华') , cc2);
3160
3178
}
3161
3179
3162
3180
#[test]
3163
- #[should_fail]
3164
- fn test_pop_char_fail() {
3181
+ fn test_pop_char_empty() {
3165
3182
let mut data = ~" ";
3166
- let _cc3 = data.pop_char();
3183
+ let cc3 = data.pop_char();
3184
+ assert_eq!(~" ", data);
3185
+ assert_eq!(None, cc3);
3167
3186
}
3168
3187
3169
3188
#[test]
@@ -3182,7 +3201,7 @@ mod tests {
3182
3201
let mut data = ~" ประเทศไทย中";
3183
3202
let cc = data.shift_char();
3184
3203
assert_eq!(~" ระเทศไทย中", data);
3185
- assert_eq!('ป', cc);
3204
+ assert_eq!(Some( 'ป') , cc);
3186
3205
}
3187
3206
3188
3207
#[test]
@@ -3611,15 +3630,15 @@ mod tests {
3611
3630
let mut s = ~" ABC ";
3612
3631
let b = unsafe{raw::shift_byte(&mut s)};
3613
3632
assert_eq!(s, ~" BC ");
3614
- assert_eq!(b, 65u8);
3633
+ assert_eq!(b, Some( 65u8) );
3615
3634
}
3616
3635
3617
3636
#[test]
3618
3637
fn test_pop_byte() {
3619
3638
let mut s = ~" ABC ";
3620
3639
let b = unsafe{raw::pop_byte(&mut s)};
3621
3640
assert_eq!(s, ~" AB ");
3622
- assert_eq!(b, 67u8);
3641
+ assert_eq!(b, Some( 67u8) );
3623
3642
}
3624
3643
3625
3644
#[test]
0 commit comments