@@ -191,10 +191,10 @@ impl<'self, S: Str> StrVector for &'self [S] {
191
191
s. reserve( len) ;
192
192
193
193
unsafe {
194
- do s. as_buf |buf, _| {
195
- let mut buf = :: cast :: transmute_mut_unsafe ( buf) ;
194
+ do s. as_mut_buf |buf, _| {
195
+ let mut buf = buf;
196
196
for self . iter( ) . advance |ss| {
197
- do ss. as_slice( ) . as_buf |ssbuf, sslen| {
197
+ do ss. as_slice( ) . as_imm_buf |ssbuf, sslen| {
198
198
let sslen = sslen - 1 ;
199
199
ptr:: copy_memory( buf, ssbuf, sslen) ;
200
200
buf = buf. offset( sslen) ;
@@ -222,12 +222,12 @@ impl<'self, S: Str> StrVector for &'self [S] {
222
222
s. reserve( len) ;
223
223
224
224
unsafe {
225
- do s. as_buf |buf, _| {
226
- do sep. as_buf |sepbuf, seplen| {
225
+ do s. as_mut_buf |buf, _| {
226
+ do sep. as_imm_buf |sepbuf, seplen| {
227
227
let seplen = seplen - 1 ;
228
228
let mut buf = :: cast:: transmute_mut_unsafe( buf) ;
229
229
for self . iter( ) . advance |ss| {
230
- do ss. as_slice( ) . as_buf |ssbuf, sslen| {
230
+ do ss. as_slice( ) . as_imm_buf |ssbuf, sslen| {
231
231
let sslen = sslen - 1 ;
232
232
if first {
233
233
first = false ;
@@ -533,8 +533,8 @@ Section: Comparing strings
533
533
#[ lang="str_eq" ]
534
534
#[ inline]
535
535
pub fn eq_slice ( a : & str , b : & str ) -> bool {
536
- do a. as_buf |ap, alen| {
537
- do b. as_buf |bp, blen| {
536
+ do a. as_imm_buf |ap, alen| {
537
+ do b. as_imm_buf |bp, blen| {
538
538
if ( alen != blen) { false }
539
539
else {
540
540
unsafe {
@@ -550,8 +550,8 @@ pub fn eq_slice(a: &str, b: &str) -> bool {
550
550
#[ cfg( test) ]
551
551
#[ inline]
552
552
pub fn eq_slice ( a : & str , b : & str ) -> bool {
553
- do a. as_buf |ap, alen| {
554
- do b. as_buf |bp, blen| {
553
+ do a. as_imm_buf |ap, alen| {
554
+ do b. as_imm_buf |bp, blen| {
555
555
if ( alen != blen) { false }
556
556
else {
557
557
unsafe {
@@ -868,7 +868,7 @@ pub mod raw {
868
868
* If end is greater than the length of the string.
869
869
*/
870
870
pub unsafe fn slice_bytes_owned( s : & str , begin : uint , end : uint ) -> ~str {
871
- do s. as_buf |sbuf, n| {
871
+ do s. as_imm_buf |sbuf, n| {
872
872
assert ! ( ( begin <= end) ) ;
873
873
assert ! ( ( end <= n) ) ;
874
874
@@ -896,7 +896,7 @@ pub mod raw {
896
896
*/
897
897
#[ inline]
898
898
pub unsafe fn slice_bytes( s: & str, begin: uint, end: uint) -> & str {
899
- do s. as_buf |sbuf, n| {
899
+ do s. as_imm_buf |sbuf, n| {
900
900
assert ! ( ( begin <= end) ) ;
901
901
assert ! ( ( end <= n) ) ;
902
902
@@ -909,8 +909,7 @@ pub mod raw {
909
909
pub unsafe fn push_byte( s: & mut ~str , b: u8) {
910
910
let new_len = s. len ( ) + 1 ;
911
911
s. reserve_at_least ( new_len) ;
912
- do s. as_buf |buf, len| {
913
- let buf: * mut u8 = :: cast:: transmute ( buf) ;
912
+ do s. as_mut_buf |buf, len| {
914
913
* ptr:: mut_offset ( buf, len) = b;
915
914
}
916
915
set_len( & mut * s, new_len) ;
@@ -1130,7 +1129,7 @@ impl<'self> Str for @str {
1130
1129
impl<'self> Container for &'self str {
1131
1130
#[inline]
1132
1131
fn len(&self) -> uint {
1133
- do self.as_buf |_p, n| { n - 1u }
1132
+ do self.as_imm_buf |_p, n| { n - 1u }
1134
1133
}
1135
1134
#[inline]
1136
1135
fn is_empty(&self) -> bool {
@@ -1225,7 +1224,8 @@ pub trait StrSlice<'self> {
1225
1224
1226
1225
fn subslice_offset(&self, inner: &str) -> uint;
1227
1226
1228
- fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
1227
+ fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
1228
+ fn as_mut_buf<T>(&self, f: &fn(*mut u8, uint) -> T) -> T;
1229
1229
fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T;
1230
1230
}
1231
1231
@@ -1849,15 +1849,15 @@ impl<'self> StrSlice<'self> for &'self str {
1849
1849
1850
1850
/// Given a string, make a new string with repeated copies of it.
1851
1851
fn repeat(&self, nn: uint) -> ~str {
1852
- do self.as_buf |buf, len| {
1852
+ do self.as_imm_buf |buf, len| {
1853
1853
let mut ret = ~" ";
1854
1854
// ignore the NULL terminator
1855
1855
let len = len - 1;
1856
1856
ret.reserve(nn * len);
1857
1857
1858
1858
unsafe {
1859
- do ret.as_buf |rbuf, _len| {
1860
- let mut rbuf = ::cast::transmute_mut_unsafe( rbuf) ;
1859
+ do ret.as_mut_buf |rbuf, _len| {
1860
+ let mut rbuf = rbuf;
1861
1861
1862
1862
for nn.times {
1863
1863
ptr::copy_memory(rbuf, buf, len);
@@ -1950,8 +1950,8 @@ impl<'self> StrSlice<'self> for &'self str {
1950
1950
*/
1951
1951
#[inline]
1952
1952
fn subslice_offset(&self, inner: &str) -> uint {
1953
- do self.as_buf |a, a_len| {
1954
- do inner.as_buf |b, b_len| {
1953
+ do self.as_imm_buf |a, a_len| {
1954
+ do inner.as_imm_buf |b, b_len| {
1955
1955
let a_start: uint;
1956
1956
let a_end: uint;
1957
1957
let b_start: uint;
@@ -1976,14 +1976,31 @@ impl<'self> StrSlice<'self> for &'self str {
1976
1976
* to full strings, or suffixes of them.
1977
1977
*/
1978
1978
#[inline]
1979
- fn as_buf <T>(&self, f: &fn(*u8, uint) -> T) -> T {
1979
+ fn as_imm_buf <T>(&self, f: &fn(*u8, uint) -> T) -> T {
1980
1980
unsafe {
1981
1981
let v: *(*u8, uint) = cast::transmute(self);
1982
1982
let (buf, len) = *v;
1983
1983
f(buf, len)
1984
1984
}
1985
1985
}
1986
1986
1987
+ /**
1988
+ * Work with the byte buffer and length of a slice.
1989
+ *
1990
+ * The given length is one byte longer than the 'official' indexable
1991
+ * length of the string. This is to permit probing the byte past the
1992
+ * indexable area for a null byte, as is the case in slices pointing
1993
+ * to full strings, or suffixes of them.
1994
+ */
1995
+ #[inline]
1996
+ fn as_mut_buf<T>(&self, f: &fn(*mut u8, uint) -> T) -> T {
1997
+ unsafe {
1998
+ let v: *(*mut u8, uint) = cast::transmute(self);
1999
+ let (buf, len) = *v;
2000
+ f(buf, len)
2001
+ }
2002
+ }
2003
+
1987
2004
/**
1988
2005
* Work with the byte buffer of a string as a null-terminated C string.
1989
2006
*
@@ -2001,7 +2018,7 @@ impl<'self> StrSlice<'self> for &'self str {
2001
2018
*/
2002
2019
#[inline]
2003
2020
fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
2004
- do self.as_buf |buf, len| {
2021
+ do self.as_imm_buf |buf, len| {
2005
2022
// NB: len includes the trailing null.
2006
2023
assert!(len > 0);
2007
2024
if unsafe { *(ptr::offset(buf, len - 1)) != 0 } {
@@ -2068,8 +2085,8 @@ impl OwnedStr for ~str {
2068
2085
let llen = self.len();
2069
2086
let rlen = rhs.len();
2070
2087
self.reserve(llen + rlen);
2071
- do self.as_buf |lbuf, _llen| {
2072
- do rhs.as_buf |rbuf, _rlen| {
2088
+ do self.as_imm_buf |lbuf, _llen| {
2089
+ do rhs.as_imm_buf |rbuf, _rlen| {
2073
2090
let dst = ptr::offset(lbuf, llen);
2074
2091
let dst = ::cast::transmute_mut_unsafe(dst);
2075
2092
ptr::copy_memory(dst, rbuf, rlen);
@@ -2086,8 +2103,8 @@ impl OwnedStr for ~str {
2086
2103
let llen = self.len();
2087
2104
let rlen = rhs.len();
2088
2105
self.reserve_at_least(llen + rlen);
2089
- do self.as_buf |lbuf, _llen| {
2090
- do rhs.as_buf |rbuf, _rlen| {
2106
+ do self.as_imm_buf |lbuf, _llen| {
2107
+ do rhs.as_imm_buf |rbuf, _rlen| {
2091
2108
let dst = ptr::offset(lbuf, llen);
2092
2109
let dst = ::cast::transmute_mut_unsafe(dst);
2093
2110
ptr::copy_memory(dst, rbuf, rlen);
@@ -2110,8 +2127,7 @@ impl OwnedStr for ~str {
2110
2127
let new_len = len + nb;
2111
2128
self.reserve_at_least(new_len);
2112
2129
let off = len;
2113
- do self.as_buf |buf, _len| {
2114
- let buf: *mut u8 = ::cast::transmute(buf);
2130
+ do self.as_mut_buf |buf, _len| {
2115
2131
match nb {
2116
2132
1u => {
2117
2133
*ptr::mut_offset(buf, off) = code as u8;
@@ -3073,45 +3089,23 @@ mod tests {
3073
3089
}
3074
3090
3075
3091
#[test]
3076
- fn test_as_buf() {
3077
- let a = " Abcdefg ";
3078
- let b = do a.as_buf |buf, _l| {
3079
- assert_eq!(unsafe { *buf }, 65u8);
3080
- 100
3081
- };
3082
- assert_eq!(b, 100);
3083
- }
3084
-
3085
- #[test]
3086
- fn test_as_buf_small() {
3087
- let a = " A ";
3088
- let b = do a.as_buf |buf, _l| {
3089
- assert_eq!(unsafe { *buf }, 65u8);
3090
- 100
3091
- };
3092
- assert_eq!(b, 100);
3093
- }
3094
-
3095
- #[test]
3096
- fn test_as_buf2() {
3097
- unsafe {
3098
- let s = ~" hello";
3099
- let sb = s.as_buf(|b, _l| b);
3100
- let s_cstr = raw::from_buf(sb);
3101
- assert_eq!(s_cstr, s);
3092
+ fn test_as_imm_buf() {
3093
+ do " ".as_imm_buf |buf, len| {
3094
+ assert_eq!(len, 1);
3095
+ unsafe {
3096
+ assert_eq!(*ptr::offset(buf, 0), 0);
3097
+ }
3102
3098
}
3103
- }
3104
3099
3105
- #[test]
3106
- fn test_as_buf_3() {
3107
- let a = ~" hello";
3108
- do a.as_buf |buf, len| {
3100
+ do " hello".as_imm_buf |buf, len| {
3101
+ assert_eq!(len, 6);
3109
3102
unsafe {
3110
- assert_eq!(a[0], 'h' as u8);
3111
- assert_eq!(*buf, 'h' as u8);
3112
- assert_eq!(len, 6u);
3113
- assert_eq!(*ptr::offset(buf,4u), 'o' as u8);
3114
- assert_eq!(*ptr::offset(buf,5u), 0u8);
3103
+ assert_eq!(*ptr::offset(buf, 0), 'h' as u8);
3104
+ assert_eq!(*ptr::offset(buf, 1), 'e' as u8);
3105
+ assert_eq!(*ptr::offset(buf, 2), 'l' as u8);
3106
+ assert_eq!(*ptr::offset(buf, 3), 'l' as u8);
3107
+ assert_eq!(*ptr::offset(buf, 4), 'o' as u8);
3108
+ assert_eq!(*ptr::offset(buf, 5), 0);
3115
3109
}
3116
3110
}
3117
3111
}
0 commit comments