@@ -35,7 +35,7 @@ use str;
35
35
use to_str:: ToStr ;
36
36
use uint;
37
37
use vec;
38
- use vec:: { OwnedVector , OwnedCopyableVector } ;
38
+ use vec:: { OwnedVector , OwnedCopyableVector , ImmutableVector } ;
39
39
40
40
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord , Equiv , TotalEq } ;
41
41
@@ -1249,42 +1249,6 @@ pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str {
1249
1249
result
1250
1250
}
1251
1251
1252
- /// Iterate over the bytes in a string
1253
- #[ inline( always) ]
1254
- pub fn each( s: & str, it: & fn( u8) -> bool ) -> bool {
1255
- eachi( s, |_i, b| it( b) )
1256
- }
1257
-
1258
- /// Iterate over the bytes in a string, with indices
1259
- #[ inline( always) ]
1260
- pub fn eachi( s: & str, it: & fn( uint, u8) -> bool ) -> bool {
1261
- let mut pos = 0 ;
1262
- let len = s. len( ) ;
1263
-
1264
- while pos < len {
1265
- if !it ( pos, s[ pos] ) { return false ; }
1266
- pos += 1 ;
1267
- }
1268
- return true ;
1269
- }
1270
-
1271
- /// Iterate over the bytes in a string in reverse
1272
- #[ inline( always) ]
1273
- pub fn each_reverse ( s : & str , it : & fn ( u8 ) -> bool ) -> bool {
1274
- eachi_reverse ( s, |_i, b| it ( b) )
1275
- }
1276
-
1277
- /// Iterate over the bytes in a string in reverse, with indices
1278
- #[ inline( always) ]
1279
- pub fn eachi_reverse ( s : & str , it : & fn ( uint , u8 ) -> bool ) -> bool {
1280
- let mut pos = s. len ( ) ;
1281
- while pos > 0 {
1282
- pos -= 1 ;
1283
- if !it ( pos, s[ pos] ) { return false ; }
1284
- }
1285
- return true ;
1286
- }
1287
-
1288
1252
/*
1289
1253
Section: Searching
1290
1254
*/
@@ -1604,7 +1568,7 @@ pub fn rfind_between(s: &str, start: uint, end: uint, f: &fn(char) -> bool) -> O
1604
1568
// Utility used by various searching functions
1605
1569
fn match_at< ' a , ' b > ( haystack : & ' a str , needle : & ' b str , at : uint ) -> bool {
1606
1570
let mut i = at;
1607
- for each ( needle) |c| { if haystack[ i] != c { return false ; } i += 1 u; }
1571
+ for needle. bytes_iter ( ) . advance |c| { if haystack[ i] != c { return false; } i += 1 u; }
1608
1572
return true ;
1609
1573
}
1610
1574
@@ -2557,10 +2521,8 @@ pub trait StrSlice<'self> {
2557
2521
fn contains_char(&self, needle: char) -> bool;
2558
2522
fn iter(&self) -> StrCharIterator<'self>;
2559
2523
fn rev_iter(&self) -> StrCharRevIterator<'self>;
2560
- fn each(&self, it: &fn(u8) -> bool) -> bool;
2561
- fn eachi(&self, it: &fn(uint, u8) -> bool) -> bool;
2562
- fn each_reverse(&self, it: &fn(u8) -> bool) -> bool;
2563
- fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool;
2524
+ fn bytes_iter(&self) -> StrBytesIterator<'self>;
2525
+ fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self>;
2564
2526
fn ends_with(&self, needle: &str) -> bool;
2565
2527
fn is_empty(&self) -> bool;
2566
2528
fn is_whitespace(&self) -> bool;
@@ -2628,20 +2590,14 @@ impl<'self> StrSlice<'self> for &'self str {
2628
2590
}
2629
2591
}
2630
2592
2631
- /// Iterate over the bytes in a string
2632
- #[inline]
2633
- fn each(&self, it: &fn(u8) -> bool) -> bool { each(*self, it) }
2634
- /// Iterate over the bytes in a string, with indices
2635
- #[inline]
2636
- fn eachi(&self, it: &fn(uint, u8) -> bool) -> bool { eachi(*self, it) }
2637
- /// Iterate over the bytes in a string
2638
- #[inline]
2639
- fn each_reverse(&self, it: &fn(u8) -> bool) -> bool { each_reverse(*self, it) }
2640
- /// Iterate over the bytes in a string, with indices
2641
- #[inline]
2642
- fn eachi_reverse(&self, it: &fn(uint, u8) -> bool) -> bool {
2643
- eachi_reverse(*self, it)
2593
+ fn bytes_iter(&self) -> StrBytesIterator<'self> {
2594
+ StrBytesIterator { it: as_bytes_slice(*self).iter() }
2644
2595
}
2596
+ fn bytes_rev_iter(&self) -> StrBytesRevIterator<'self> {
2597
+ StrBytesRevIterator { it: as_bytes_slice(*self).rev_iter() }
2598
+ }
2599
+
2600
+
2645
2601
/// Returns true if one string ends with another
2646
2602
#[inline]
2647
2603
fn ends_with(&self, needle: &str) -> bool {
@@ -2832,6 +2788,32 @@ impl<'self> Iterator<char> for StrCharRevIterator<'self> {
2832
2788
}
2833
2789
}
2834
2790
2791
+ /// External iterator for a string's bytes. Use with the `std::iterator`
2792
+ /// module.
2793
+ pub struct StrBytesIterator<'self> {
2794
+ priv it: vec::VecIterator<'self, u8>
2795
+ }
2796
+
2797
+ impl<'self> Iterator<u8> for StrBytesIterator<'self> {
2798
+ #[inline]
2799
+ fn next(&mut self) -> Option<u8> {
2800
+ self.it.next().map_consume(|&x| x)
2801
+ }
2802
+ }
2803
+
2804
+ /// External iterator for a string's bytes in reverse order. Use with
2805
+ /// the `std::iterator` module.
2806
+ pub struct StrBytesRevIterator<'self> {
2807
+ priv it: vec::VecRevIterator<'self, u8>
2808
+ }
2809
+
2810
+ impl<'self> Iterator<u8> for StrBytesRevIterator<'self> {
2811
+ #[inline]
2812
+ fn next(&mut self) -> Option<u8> {
2813
+ self.it.next().map_consume(|&x| x)
2814
+ }
2815
+ }
2816
+
2835
2817
#[cfg(test)]
2836
2818
mod tests {
2837
2819
use iterator::IteratorUtil;
@@ -3922,102 +3904,6 @@ mod tests {
3922
3904
}
3923
3905
}
3924
3906
3925
- #[test]
3926
- fn test_each() {
3927
- let s = ~" ศไทย中华Việt Nam ";
3928
- let v = [
3929
- 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
3930
- 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
3931
- 109
3932
- ];
3933
- let mut pos = 0;
3934
-
3935
- for s.each |b| {
3936
- assert_eq!(b, v[pos]);
3937
- pos += 1;
3938
- }
3939
- }
3940
-
3941
- #[test]
3942
- fn test_each_empty() {
3943
- for " ".each |b| {
3944
- assert_eq!(b, 0u8);
3945
- }
3946
- }
3947
-
3948
- #[test]
3949
- fn test_eachi() {
3950
- let s = ~" ศไทย中华Việt Nam ";
3951
- let v = [
3952
- 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
3953
- 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
3954
- 109
3955
- ];
3956
- let mut pos = 0;
3957
-
3958
- for s.eachi |i, b| {
3959
- assert_eq!(pos, i);
3960
- assert_eq!(b, v[pos]);
3961
- pos += 1;
3962
- }
3963
- }
3964
-
3965
- #[test]
3966
- fn test_eachi_empty() {
3967
- for " ".eachi |i, b| {
3968
- assert_eq!(i, 0);
3969
- assert_eq!(b, 0);
3970
- }
3971
- }
3972
-
3973
- #[test]
3974
- fn test_each_reverse() {
3975
- let s = ~" ศไทย中华Việt Nam ";
3976
- let v = [
3977
- 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
3978
- 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
3979
- 109
3980
- ];
3981
- let mut pos = v.len();
3982
-
3983
- for s.each_reverse |b| {
3984
- pos -= 1;
3985
- assert_eq!(b, v[pos]);
3986
- }
3987
- }
3988
-
3989
- #[test]
3990
- fn test_each_empty_reverse() {
3991
- for " ".each_reverse |b| {
3992
- assert_eq!(b, 0u8);
3993
- }
3994
- }
3995
-
3996
- #[test]
3997
- fn test_eachi_reverse() {
3998
- let s = ~" ศไทย中华Việt Nam ";
3999
- let v = [
4000
- 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
4001
- 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
4002
- 109
4003
- ];
4004
- let mut pos = v.len();
4005
-
4006
- for s.eachi_reverse |i, b| {
4007
- pos -= 1;
4008
- assert_eq!(pos, i);
4009
- assert_eq!(b, v[pos]);
4010
- }
4011
- }
4012
-
4013
- #[test]
4014
- fn test_eachi_reverse_empty() {
4015
- for " ".eachi_reverse |i, b| {
4016
- assert_eq!(i, 0);
4017
- assert_eq!(b, 0);
4018
- }
4019
- }
4020
-
4021
3907
#[test]
4022
3908
fn test_escape_unicode() {
4023
3909
assert_eq!(escape_unicode(" abc"), ~"\\ x61\\ x62\\ x63" ) ;
@@ -4097,4 +3983,36 @@ mod tests {
4097
3983
}
4098
3984
assert_eq!(pos, v.len());
4099
3985
}
3986
+
3987
+ #[test]
3988
+ fn test_bytes_iterator() {
3989
+ let s = ~" ศไทย中华Việt Nam ";
3990
+ let v = [
3991
+ 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
3992
+ 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
3993
+ 109
3994
+ ];
3995
+ let mut pos = 0;
3996
+
3997
+ for s.bytes_iter().advance |b| {
3998
+ assert_eq!(b, v[pos]);
3999
+ pos += 1;
4000
+ }
4001
+ }
4002
+
4003
+ #[test]
4004
+ fn test_bytes_rev_iterator() {
4005
+ let s = ~" ศไทย中华Việt Nam " ;
4006
+ let v = [
4007
+ 224 , 184 , 168 , 224 , 185 , 132 , 224 , 184 , 151 , 224 , 184 , 162 , 228 ,
4008
+ 184 , 173 , 229 , 141 , 142 , 86 , 105 , 225 , 187 , 135 , 116 , 32 , 78 , 97 ,
4009
+ 109
4010
+ ] ;
4011
+ let mut pos = v. len( ) ;
4012
+
4013
+ for s. bytes_rev_iter( ) . advance |b| {
4014
+ pos -= 1 ;
4015
+ assert_eq!( b, v[ pos] ) ;
4016
+ }
4017
+ }
4100
4018
}
0 commit comments