Skip to content

Commit 9fdec67

Browse files
committed
std: move str::as_buf into StrSlice
1 parent cfd89c4 commit 9fdec67

File tree

6 files changed

+62
-62
lines changed

6 files changed

+62
-62
lines changed

src/libstd/option.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use iterator::Iterator;
5050
use str::StrSlice;
5151
use clone::DeepClone;
5252

53-
#[cfg(test)] use str;
5453
#[cfg(test)] use iterator::IteratorUtil;
5554

5655
/// The option type
@@ -446,10 +445,10 @@ fn test_unwrap_ptr() {
446445
#[test]
447446
fn test_unwrap_str() {
448447
let x = ~"test";
449-
let addr_x = str::as_buf(x, |buf, _len| buf);
448+
let addr_x = x.as_buf(|buf, _len| buf);
450449
let opt = Some(x);
451450
let y = opt.unwrap();
452-
let addr_y = str::as_buf(y, |buf, _len| buf);
451+
let addr_y = y.as_buf(|buf, _len| buf);
453452
assert_eq!(addr_x, addr_y);
454453
}
455454

src/libstd/rt/borrowck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use managed::raw::BoxRepr;
1616
use option::{Option, None, Some};
1717
use uint;
1818
use str;
19-
use str::OwnedStr;
19+
use str::{OwnedStr, StrSlice};
2020
use sys;
2121
use vec::ImmutableVector;
2222

@@ -76,7 +76,7 @@ unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
7676
match try_take_task_borrow_list() {
7777
None => { // not recording borrows
7878
let msg = "borrowed";
79-
do str::as_buf(msg) |msg_p, _| {
79+
do msg.as_buf |msg_p, _| {
8080
sys::begin_unwind_(msg_p as *c_char, file, line);
8181
}
8282
}
@@ -92,7 +92,7 @@ unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
9292
sep = " and at ";
9393
}
9494
}
95-
do str::as_buf(msg) |msg_p, _| {
95+
do msg.as_buf |msg_p, _| {
9696
sys::begin_unwind_(msg_p as *c_char, file, line)
9797
}
9898
}
@@ -231,7 +231,7 @@ pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
231231
let br = borrow_list.pop();
232232
if br.box != a || br.file != file || br.line != line {
233233
let err = fmt!("wrong borrow found, br=%?", br);
234-
do str::as_buf(err) |msg_p, _| {
234+
do err.as_buf |msg_p, _| {
235235
sys::begin_unwind_(msg_p as *c_char, file, line)
236236
}
237237
}

src/libstd/str.rs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
use at_vec;
20-
use cast::transmute;
2120
use cast;
2221
use char;
2322
use char::Char;
@@ -192,10 +191,10 @@ impl<'self, S: Str> StrVector for &'self [S] {
192191
s.reserve(len);
193192

194193
unsafe {
195-
do as_buf(s) |buf, _| {
194+
do s.as_buf |buf, _| {
196195
let mut buf = ::cast::transmute_mut_unsafe(buf);
197196
for self.iter().advance |ss| {
198-
do as_buf(ss.as_slice()) |ssbuf, sslen| {
197+
do ss.as_slice().as_buf |ssbuf, sslen| {
199198
let sslen = sslen - 1;
200199
ptr::copy_memory(buf, ssbuf, sslen);
201200
buf = buf.offset(sslen);
@@ -223,12 +222,12 @@ impl<'self, S: Str> StrVector for &'self [S] {
223222
s.reserve(len);
224223

225224
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| {
228227
let seplen = seplen - 1;
229228
let mut buf = ::cast::transmute_mut_unsafe(buf);
230229
for self.iter().advance |ss| {
231-
do as_buf(ss.as_slice()) |ssbuf, sslen| {
230+
do ss.as_slice().as_buf |ssbuf, sslen| {
232231
let sslen = sslen - 1;
233232
if first {
234233
first = false;
@@ -534,8 +533,8 @@ Section: Comparing strings
534533
#[lang="str_eq"]
535534
#[inline]
536535
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| {
539538
if (alen != blen) { false }
540539
else {
541540
unsafe {
@@ -551,8 +550,8 @@ pub fn eq_slice(a: &str, b: &str) -> bool {
551550
#[cfg(test)]
552551
#[inline]
553552
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| {
556555
if (alen != blen) { false }
557556
else {
558557
unsafe {
@@ -799,7 +798,7 @@ pub trait StrUtil {
799798
impl<'self> StrUtil for &'self str {
800799
#[inline]
801800
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| {
803802
// NB: len includes the trailing null.
804803
assert!(len > 0);
805804
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 {
819818
s.as_c_str(f)
820819
}
821820

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-
839821
/// Unsafe operations
840822
pub mod raw {
841823
use cast;
842824
use libc;
843825
use ptr;
844826
use str::raw;
845-
use str::{as_buf, is_utf8};
827+
use str::{is_utf8};
846828
use vec;
847829
use vec::MutableVector;
848830

@@ -931,7 +913,7 @@ pub mod raw {
931913
* If end is greater than the length of the string.
932914
*/
933915
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| {
935917
assert!((begin <= end));
936918
assert!((end <= n));
937919

@@ -959,7 +941,7 @@ pub mod raw {
959941
*/
960942
#[inline]
961943
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| {
963945
assert!((begin <= end));
964946
assert!((end <= n));
965947

@@ -972,7 +954,7 @@ pub mod raw {
972954
pub unsafe fn push_byte(s: &mut ~str, b: u8) {
973955
let new_len = s.len() + 1;
974956
s.reserve_at_least(new_len);
975-
do as_buf(*s) |buf, len| {
957+
do s.as_buf |buf, len| {
976958
let buf: *mut u8 = ::cast::transmute(buf);
977959
*ptr::mut_offset(buf, len) = b;
978960
}
@@ -1193,7 +1175,7 @@ impl<'self> Str for @str {
11931175
impl<'self> Container for &'self str {
11941176
#[inline]
11951177
fn len(&self) -> uint {
1196-
do as_buf(*self) |_p, n| { n - 1u }
1178+
do self.as_buf |_p, n| { n - 1u }
11971179
}
11981180
#[inline]
11991181
fn is_empty(&self) -> bool {
@@ -1287,6 +1269,8 @@ pub trait StrSlice<'self> {
12871269
fn lev_distance(&self, t: &str) -> uint;
12881270
12891271
fn subslice_offset(&self, inner: &str) -> uint;
1272+
1273+
fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
12901274
}
12911275
12921276
/// Extension methods for strings
@@ -1909,14 +1893,14 @@ impl<'self> StrSlice<'self> for &'self str {
19091893
19101894
/// Given a string, make a new string with repeated copies of it.
19111895
fn repeat(&self, nn: uint) -> ~str {
1912-
do as_buf(*self) |buf, len| {
1896+
do self.as_buf |buf, len| {
19131897
let mut ret = ~"";
19141898
// ignore the NULL terminator
19151899
let len = len - 1;
19161900
ret.reserve(nn * len);
19171901
19181902
unsafe {
1919-
do as_buf(ret) |rbuf, _len| {
1903+
do ret.as_buf |rbuf, _len| {
19201904
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
19211905
19221906
for nn.times {
@@ -2010,8 +1994,8 @@ impl<'self> StrSlice<'self> for &'self str {
20101994
*/
20111995
#[inline]
20121996
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| {
20151999
let a_start: uint;
20162000
let a_end: uint;
20172001
let b_start: uint;
@@ -2027,6 +2011,22 @@ impl<'self> StrSlice<'self> for &'self str {
20272011
}
20282012
}
20292013
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+
}
20302030
}
20312031
20322032
#[allow(missing_doc)]
@@ -2084,8 +2084,8 @@ impl OwnedStr for ~str {
20842084
let llen = self.len();
20852085
let rlen = rhs.len();
20862086
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| {
20892089
let dst = ptr::offset(lbuf, llen);
20902090
let dst = ::cast::transmute_mut_unsafe(dst);
20912091
ptr::copy_memory(dst, rbuf, rlen);
@@ -2102,8 +2102,8 @@ impl OwnedStr for ~str {
21022102
let llen = self.len();
21032103
let rlen = rhs.len();
21042104
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| {
21072107
let dst = ptr::offset(lbuf, llen);
21082108
let dst = ::cast::transmute_mut_unsafe(dst);
21092109
ptr::copy_memory(dst, rbuf, rlen);
@@ -2126,7 +2126,7 @@ impl OwnedStr for ~str {
21262126
let new_len = len + nb;
21272127
self.reserve_at_least(new_len);
21282128
let off = len;
2129-
do as_buf(*self) |buf, _len| {
2129+
do self.as_buf |buf, _len| {
21302130
let buf: *mut u8 = ::cast::transmute(buf);
21312131
match nb {
21322132
1u => {
@@ -3091,28 +3091,28 @@ mod tests {
30913091
#[test]
30923092
fn test_as_buf() {
30933093
let a = "Abcdefg";
3094-
let b = as_buf(a, |buf, _l| {
3094+
let b = do a.as_buf |buf, _l| {
30953095
assert_eq!(unsafe { *buf }, 65u8);
30963096
100
3097-
});
3097+
};
30983098
assert_eq!(b, 100);
30993099
}
31003100
31013101
#[test]
31023102
fn test_as_buf_small() {
31033103
let a = "A";
3104-
let b = as_buf(a, |buf, _l| {
3104+
let b = do a.as_buf |buf, _l| {
31053105
assert_eq!(unsafe { *buf }, 65u8);
31063106
100
3107-
});
3107+
};
31083108
assert_eq!(b, 100);
31093109
}
31103110
31113111
#[test]
31123112
fn test_as_buf2() {
31133113
unsafe {
31143114
let s = ~"hello";
3115-
let sb = as_buf(s, |b, _l| b);
3115+
let sb = s.as_buf(|b, _l| b);
31163116
let s_cstr = raw::from_buf(sb);
31173117
assert_eq!(s_cstr, s);
31183118
}
@@ -3121,7 +3121,7 @@ mod tests {
31213121
#[test]
31223122
fn test_as_buf_3() {
31233123
let a = ~"hello";
3124-
do as_buf(a) |buf, len| {
3124+
do a.as_buf |buf, len| {
31253125
unsafe {
31263126
assert_eq!(a[0], 'h' as u8);
31273127
assert_eq!(*buf, 'h' as u8);

src/libstd/sys.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use io;
1818
use libc;
1919
use libc::{c_char, size_t};
2020
use repr;
21+
use str::StrSlice;
2122
use str;
2223
use unstable::intrinsics;
2324

@@ -122,8 +123,8 @@ pub trait FailWithCause {
122123

123124
impl FailWithCause for ~str {
124125
fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
125-
do str::as_buf(cause) |msg_buf, _msg_len| {
126-
do str::as_buf(file) |file_buf, _file_len| {
126+
do cause.as_buf |msg_buf, _msg_len| {
127+
do file.as_buf |file_buf, _file_len| {
127128
unsafe {
128129
let msg_buf = cast::transmute(msg_buf);
129130
let file_buf = cast::transmute(file_buf);
@@ -136,8 +137,8 @@ impl FailWithCause for ~str {
136137

137138
impl FailWithCause for &'static str {
138139
fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
139-
do str::as_buf(cause) |msg_buf, _msg_len| {
140-
do str::as_buf(file) |file_buf, _file_len| {
140+
do cause.as_buf |msg_buf, _msg_len| {
141+
do file.as_buf |file_buf, _file_len| {
141142
unsafe {
142143
let msg_buf = cast::transmute(msg_buf);
143144
let file_buf = cast::transmute(file_buf);

src/libstd/unstable/lang.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
5656
index: size_t, len: size_t) {
5757
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
5858
len as int, index as int);
59-
do str::as_buf(msg) |p, _len| {
59+
do msg.as_buf |p, _len| {
6060
fail_(p as *c_char, file, line);
6161
}
6262
}

src/test/run-pass/c-stack-returning-int64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ mod libc {
2020
}
2121

2222
fn atol(s: ~str) -> int {
23-
return str::as_buf(s, { |x, _len| unsafe { libc::atol(x) } });
23+
s.as_buf(|x, _len| unsafe { libc::atol(x) })
2424
}
2525

2626
fn atoll(s: ~str) -> i64 {
27-
return str::as_buf(s, { |x, _len| unsafe { libc::atoll(x) } });
27+
s.as_buf(|x, _len| unsafe { libc::atoll(x) })
2828
}
2929

3030
pub fn main() {

0 commit comments

Comments
 (0)