17
17
*/
18
18
19
19
use at_vec;
20
- use cast:: transmute;
21
20
use cast;
22
21
use char;
23
22
use char:: Char ;
@@ -192,10 +191,10 @@ impl<'self, S: Str> StrVector for &'self [S] {
192
191
s. reserve( len) ;
193
192
194
193
unsafe {
195
- do as_buf ( s ) |buf, _| {
194
+ do s . as_buf |buf, _| {
196
195
let mut buf = :: cast:: transmute_mut_unsafe( buf) ;
197
196
for self . iter( ) . advance |ss| {
198
- do as_buf ( ss. as_slice( ) ) |ssbuf, sslen| {
197
+ do ss. as_slice( ) . as_buf |ssbuf, sslen| {
199
198
let sslen = sslen - 1 ;
200
199
ptr:: copy_memory( buf, ssbuf, sslen) ;
201
200
buf = buf. offset( sslen) ;
@@ -223,12 +222,12 @@ impl<'self, S: Str> StrVector for &'self [S] {
223
222
s. reserve( len) ;
224
223
225
224
unsafe {
226
- do as_buf ( s ) |buf, _| {
227
- do as_buf ( sep) |sepbuf, seplen| {
225
+ do s . as_buf |buf, _| {
226
+ do sep. as_buf |sepbuf, seplen| {
228
227
let seplen = seplen - 1 ;
229
228
let mut buf = :: cast:: transmute_mut_unsafe( buf) ;
230
229
for self . iter( ) . advance |ss| {
231
- do as_buf ( ss. as_slice( ) ) |ssbuf, sslen| {
230
+ do ss. as_slice( ) . as_buf |ssbuf, sslen| {
232
231
let sslen = sslen - 1 ;
233
232
if first {
234
233
first = false ;
@@ -534,8 +533,8 @@ Section: Comparing strings
534
533
#[ lang="str_eq" ]
535
534
#[ inline]
536
535
pub fn eq_slice ( a : & str , b : & str ) -> bool {
537
- do as_buf ( a ) |ap, alen| {
538
- do as_buf ( b ) |bp, blen| {
536
+ do a . as_buf |ap, alen| {
537
+ do b . as_buf |bp, blen| {
539
538
if ( alen != blen) { false }
540
539
else {
541
540
unsafe {
@@ -551,8 +550,8 @@ pub fn eq_slice(a: &str, b: &str) -> bool {
551
550
#[ cfg( test) ]
552
551
#[ inline]
553
552
pub fn eq_slice ( a : & str , b : & str ) -> bool {
554
- do as_buf ( a ) |ap, alen| {
555
- do as_buf ( b ) |bp, blen| {
553
+ do a . as_buf |ap, alen| {
554
+ do b . as_buf |bp, blen| {
556
555
if ( alen != blen) { false }
557
556
else {
558
557
unsafe {
@@ -799,7 +798,7 @@ pub trait StrUtil {
799
798
impl < ' self > StrUtil for & ' self str {
800
799
#[ inline]
801
800
fn as_c_str < T > ( self , f : & fn ( * libc:: c_char ) -> T ) -> T {
802
- do as_buf ( self ) |buf, len| {
801
+ do self . as_buf |buf, len| {
803
802
// NB: len includes the trailing null.
804
803
assert ! ( len > 0 ) ;
805
804
if unsafe { * ( ptr:: offset ( buf, len-1 ) ) != 0 } {
@@ -819,30 +818,13 @@ pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
819
818
s. as_c_str ( f)
820
819
}
821
820
822
- /**
823
- * Work with the byte buffer and length of a slice.
824
- *
825
- * The given length is one byte longer than the 'official' indexable
826
- * length of the string. This is to permit probing the byte past the
827
- * indexable area for a null byte, as is the case in slices pointing
828
- * to full strings, or suffixes of them.
829
- */
830
- #[ inline]
831
- pub fn as_buf < T > ( s : & str , f : & fn ( * u8 , uint ) -> T ) -> T {
832
- unsafe {
833
- let v : * ( * u8 , uint ) = transmute ( & s) ;
834
- let ( buf, len) = * v;
835
- f ( buf, len)
836
- }
837
- }
838
-
839
821
/// Unsafe operations
840
822
pub mod raw {
841
823
use cast;
842
824
use libc;
843
825
use ptr;
844
826
use str:: raw;
845
- use str:: { as_buf , is_utf8} ;
827
+ use str:: { is_utf8} ;
846
828
use vec;
847
829
use vec:: MutableVector ;
848
830
@@ -931,7 +913,7 @@ pub mod raw {
931
913
* If end is greater than the length of the string.
932
914
*/
933
915
pub unsafe fn slice_bytes_owned( s : & str , begin : uint , end : uint ) -> ~str {
934
- do as_buf ( s ) |sbuf, n| {
916
+ do s . as_buf |sbuf, n| {
935
917
assert ! ( ( begin <= end) ) ;
936
918
assert ! ( ( end <= n) ) ;
937
919
@@ -959,7 +941,7 @@ pub mod raw {
959
941
*/
960
942
#[ inline]
961
943
pub unsafe fn slice_bytes( s: & str, begin: uint, end: uint) -> & str {
962
- do as_buf ( s ) |sbuf, n| {
944
+ do s . as_buf |sbuf, n| {
963
945
assert ! ( ( begin <= end) ) ;
964
946
assert ! ( ( end <= n) ) ;
965
947
@@ -972,7 +954,7 @@ pub mod raw {
972
954
pub unsafe fn push_byte( s: & mut ~str , b: u8) {
973
955
let new_len = s. len ( ) + 1 ;
974
956
s. reserve_at_least ( new_len) ;
975
- do as_buf ( * s ) |buf, len| {
957
+ do s . as_buf |buf, len| {
976
958
let buf: * mut u8 = :: cast:: transmute ( buf) ;
977
959
* ptr:: mut_offset ( buf, len) = b;
978
960
}
@@ -1193,7 +1175,7 @@ impl<'self> Str for @str {
1193
1175
impl<'self> Container for &'self str {
1194
1176
#[inline]
1195
1177
fn len(&self) -> uint {
1196
- do as_buf(* self) |_p, n| { n - 1u }
1178
+ do self.as_buf |_p, n| { n - 1u }
1197
1179
}
1198
1180
#[inline]
1199
1181
fn is_empty(&self) -> bool {
@@ -1287,6 +1269,8 @@ pub trait StrSlice<'self> {
1287
1269
fn lev_distance(&self, t: &str) -> uint;
1288
1270
1289
1271
fn subslice_offset(&self, inner: &str) -> uint;
1272
+
1273
+ fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
1290
1274
}
1291
1275
1292
1276
/// Extension methods for strings
@@ -1909,14 +1893,14 @@ impl<'self> StrSlice<'self> for &'self str {
1909
1893
1910
1894
/// Given a string, make a new string with repeated copies of it.
1911
1895
fn repeat(&self, nn: uint) -> ~str {
1912
- do as_buf(* self) |buf, len| {
1896
+ do self.as_buf |buf, len| {
1913
1897
let mut ret = ~" ";
1914
1898
// ignore the NULL terminator
1915
1899
let len = len - 1;
1916
1900
ret.reserve(nn * len);
1917
1901
1918
1902
unsafe {
1919
- do as_buf( ret) |rbuf, _len| {
1903
+ do ret.as_buf |rbuf, _len| {
1920
1904
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
1921
1905
1922
1906
for nn.times {
@@ -2010,8 +1994,8 @@ impl<'self> StrSlice<'self> for &'self str {
2010
1994
*/
2011
1995
#[inline]
2012
1996
fn subslice_offset(&self, inner: &str) -> uint {
2013
- do as_buf(* self) |a, a_len| {
2014
- do as_buf( inner) |b, b_len| {
1997
+ do self.as_buf |a, a_len| {
1998
+ do inner.as_buf |b, b_len| {
2015
1999
let a_start: uint;
2016
2000
let a_end: uint;
2017
2001
let b_start: uint;
@@ -2027,6 +2011,22 @@ impl<'self> StrSlice<'self> for &'self str {
2027
2011
}
2028
2012
}
2029
2013
2014
+ /**
2015
+ * Work with the byte buffer and length of a slice.
2016
+ *
2017
+ * The given length is one byte longer than the 'official' indexable
2018
+ * length of the string. This is to permit probing the byte past the
2019
+ * indexable area for a null byte, as is the case in slices pointing
2020
+ * to full strings, or suffixes of them.
2021
+ */
2022
+ #[inline]
2023
+ fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T {
2024
+ unsafe {
2025
+ let v: *(*u8, uint) = cast::transmute(self);
2026
+ let (buf, len) = *v;
2027
+ f(buf, len)
2028
+ }
2029
+ }
2030
2030
}
2031
2031
2032
2032
#[allow(missing_doc)]
@@ -2084,8 +2084,8 @@ impl OwnedStr for ~str {
2084
2084
let llen = self.len();
2085
2085
let rlen = rhs.len();
2086
2086
self.reserve(llen + rlen);
2087
- do as_buf(* self) |lbuf, _llen| {
2088
- do as_buf( rhs) |rbuf, _rlen| {
2087
+ do self.as_buf |lbuf, _llen| {
2088
+ do rhs.as_buf |rbuf, _rlen| {
2089
2089
let dst = ptr::offset(lbuf, llen);
2090
2090
let dst = ::cast::transmute_mut_unsafe(dst);
2091
2091
ptr::copy_memory(dst, rbuf, rlen);
@@ -2102,8 +2102,8 @@ impl OwnedStr for ~str {
2102
2102
let llen = self.len();
2103
2103
let rlen = rhs.len();
2104
2104
self.reserve_at_least(llen + rlen);
2105
- do as_buf(* self) |lbuf, _llen| {
2106
- do as_buf( rhs) |rbuf, _rlen| {
2105
+ do self.as_buf |lbuf, _llen| {
2106
+ do rhs.as_buf |rbuf, _rlen| {
2107
2107
let dst = ptr::offset(lbuf, llen);
2108
2108
let dst = ::cast::transmute_mut_unsafe(dst);
2109
2109
ptr::copy_memory(dst, rbuf, rlen);
@@ -2126,7 +2126,7 @@ impl OwnedStr for ~str {
2126
2126
let new_len = len + nb;
2127
2127
self.reserve_at_least(new_len);
2128
2128
let off = len;
2129
- do as_buf(* self) |buf, _len| {
2129
+ do self.as_buf |buf, _len| {
2130
2130
let buf: *mut u8 = ::cast::transmute(buf);
2131
2131
match nb {
2132
2132
1u => {
@@ -3091,28 +3091,28 @@ mod tests {
3091
3091
#[test]
3092
3092
fn test_as_buf() {
3093
3093
let a = " Abcdefg ";
3094
- let b = as_buf(a, |buf, _l| {
3094
+ let b = do a.as_buf |buf, _l| {
3095
3095
assert_eq!(unsafe { *buf }, 65u8);
3096
3096
100
3097
- }) ;
3097
+ };
3098
3098
assert_eq!(b, 100);
3099
3099
}
3100
3100
3101
3101
#[test]
3102
3102
fn test_as_buf_small() {
3103
3103
let a = " A ";
3104
- let b = as_buf(a, |buf, _l| {
3104
+ let b = do a.as_buf |buf, _l| {
3105
3105
assert_eq!(unsafe { *buf }, 65u8);
3106
3106
100
3107
- }) ;
3107
+ };
3108
3108
assert_eq!(b, 100);
3109
3109
}
3110
3110
3111
3111
#[test]
3112
3112
fn test_as_buf2() {
3113
3113
unsafe {
3114
3114
let s = ~" hello";
3115
- let sb = as_buf(s, |b, _l| b);
3115
+ let sb = s. as_buf(|b, _l| b);
3116
3116
let s_cstr = raw::from_buf(sb);
3117
3117
assert_eq!(s_cstr, s);
3118
3118
}
@@ -3121,7 +3121,7 @@ mod tests {
3121
3121
#[test]
3122
3122
fn test_as_buf_3() {
3123
3123
let a = ~" hello";
3124
- do as_buf(a) |buf, len| {
3124
+ do a.as_buf |buf, len| {
3125
3125
unsafe {
3126
3126
assert_eq!(a[0], 'h' as u8);
3127
3127
assert_eq!(*buf, 'h' as u8);
0 commit comments