std: move str::as_buf into StrSlice

This commit is contained in:
Erick Tryzelaar 2013-07-10 17:33:11 -07:00
parent cfd89c4075
commit 9fdec67a67
6 changed files with 62 additions and 62 deletions

View File

@ -50,7 +50,6 @@ use iterator::Iterator;
use str::StrSlice;
use clone::DeepClone;
#[cfg(test)] use str;
#[cfg(test)] use iterator::IteratorUtil;
/// The option type
@ -446,10 +445,10 @@ fn test_unwrap_ptr() {
#[test]
fn test_unwrap_str() {
let x = ~"test";
let addr_x = str::as_buf(x, |buf, _len| buf);
let addr_x = x.as_buf(|buf, _len| buf);
let opt = Some(x);
let y = opt.unwrap();
let addr_y = str::as_buf(y, |buf, _len| buf);
let addr_y = y.as_buf(|buf, _len| buf);
assert_eq!(addr_x, addr_y);
}

View File

@ -16,7 +16,7 @@ use managed::raw::BoxRepr;
use option::{Option, None, Some};
use uint;
use str;
use str::OwnedStr;
use str::{OwnedStr, StrSlice};
use sys;
use vec::ImmutableVector;
@ -76,7 +76,7 @@ unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
match try_take_task_borrow_list() {
None => { // not recording borrows
let msg = "borrowed";
do str::as_buf(msg) |msg_p, _| {
do msg.as_buf |msg_p, _| {
sys::begin_unwind_(msg_p as *c_char, file, line);
}
}
@ -92,7 +92,7 @@ unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
sep = " and at ";
}
}
do str::as_buf(msg) |msg_p, _| {
do msg.as_buf |msg_p, _| {
sys::begin_unwind_(msg_p as *c_char, file, line)
}
}
@ -231,7 +231,7 @@ pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
let br = borrow_list.pop();
if br.box != a || br.file != file || br.line != line {
let err = fmt!("wrong borrow found, br=%?", br);
do str::as_buf(err) |msg_p, _| {
do err.as_buf |msg_p, _| {
sys::begin_unwind_(msg_p as *c_char, file, line)
}
}

View File

@ -17,7 +17,6 @@
*/
use at_vec;
use cast::transmute;
use cast;
use char;
use char::Char;
@ -192,10 +191,10 @@ impl<'self, S: Str> StrVector for &'self [S] {
s.reserve(len);
unsafe {
do as_buf(s) |buf, _| {
do s.as_buf |buf, _| {
let mut buf = ::cast::transmute_mut_unsafe(buf);
for self.iter().advance |ss| {
do as_buf(ss.as_slice()) |ssbuf, sslen| {
do ss.as_slice().as_buf |ssbuf, sslen| {
let sslen = sslen - 1;
ptr::copy_memory(buf, ssbuf, sslen);
buf = buf.offset(sslen);
@ -223,12 +222,12 @@ impl<'self, S: Str> StrVector for &'self [S] {
s.reserve(len);
unsafe {
do as_buf(s) |buf, _| {
do as_buf(sep) |sepbuf, seplen| {
do s.as_buf |buf, _| {
do sep.as_buf |sepbuf, seplen| {
let seplen = seplen - 1;
let mut buf = ::cast::transmute_mut_unsafe(buf);
for self.iter().advance |ss| {
do as_buf(ss.as_slice()) |ssbuf, sslen| {
do ss.as_slice().as_buf |ssbuf, sslen| {
let sslen = sslen - 1;
if first {
first = false;
@ -534,8 +533,8 @@ Section: Comparing strings
#[lang="str_eq"]
#[inline]
pub fn eq_slice(a: &str, b: &str) -> bool {
do as_buf(a) |ap, alen| {
do as_buf(b) |bp, blen| {
do a.as_buf |ap, alen| {
do b.as_buf |bp, blen| {
if (alen != blen) { false }
else {
unsafe {
@ -551,8 +550,8 @@ pub fn eq_slice(a: &str, b: &str) -> bool {
#[cfg(test)]
#[inline]
pub fn eq_slice(a: &str, b: &str) -> bool {
do as_buf(a) |ap, alen| {
do as_buf(b) |bp, blen| {
do a.as_buf |ap, alen| {
do b.as_buf |bp, blen| {
if (alen != blen) { false }
else {
unsafe {
@ -799,7 +798,7 @@ pub trait StrUtil {
impl<'self> StrUtil for &'self str {
#[inline]
fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T {
do as_buf(self) |buf, len| {
do self.as_buf |buf, len| {
// NB: len includes the trailing null.
assert!(len > 0);
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 {
s.as_c_str(f)
}
/**
* Work with the byte buffer and length of a slice.
*
* The given length is one byte longer than the 'official' indexable
* length of the string. This is to permit probing the byte past the
* indexable area for a null byte, as is the case in slices pointing
* to full strings, or suffixes of them.
*/
#[inline]
pub fn as_buf<T>(s: &str, f: &fn(*u8, uint) -> T) -> T {
unsafe {
let v : *(*u8,uint) = transmute(&s);
let (buf,len) = *v;
f(buf, len)
}
}
/// Unsafe operations
pub mod raw {
use cast;
use libc;
use ptr;
use str::raw;
use str::{as_buf, is_utf8};
use str::{is_utf8};
use vec;
use vec::MutableVector;
@ -931,7 +913,7 @@ pub mod raw {
* If end is greater than the length of the string.
*/
pub unsafe fn slice_bytes_owned(s: &str, begin: uint, end: uint) -> ~str {
do as_buf(s) |sbuf, n| {
do s.as_buf |sbuf, n| {
assert!((begin <= end));
assert!((end <= n));
@ -959,7 +941,7 @@ pub mod raw {
*/
#[inline]
pub unsafe fn slice_bytes(s: &str, begin: uint, end: uint) -> &str {
do as_buf(s) |sbuf, n| {
do s.as_buf |sbuf, n| {
assert!((begin <= end));
assert!((end <= n));
@ -972,7 +954,7 @@ pub mod raw {
pub unsafe fn push_byte(s: &mut ~str, b: u8) {
let new_len = s.len() + 1;
s.reserve_at_least(new_len);
do as_buf(*s) |buf, len| {
do s.as_buf |buf, len| {
let buf: *mut u8 = ::cast::transmute(buf);
*ptr::mut_offset(buf, len) = b;
}
@ -1193,7 +1175,7 @@ impl<'self> Str for @str {
impl<'self> Container for &'self str {
#[inline]
fn len(&self) -> uint {
do as_buf(*self) |_p, n| { n - 1u }
do self.as_buf |_p, n| { n - 1u }
}
#[inline]
fn is_empty(&self) -> bool {
@ -1287,6 +1269,8 @@ pub trait StrSlice<'self> {
fn lev_distance(&self, t: &str) -> uint;
fn subslice_offset(&self, inner: &str) -> uint;
fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
}
/// Extension methods for strings
@ -1909,14 +1893,14 @@ impl<'self> StrSlice<'self> for &'self str {
/// Given a string, make a new string with repeated copies of it.
fn repeat(&self, nn: uint) -> ~str {
do as_buf(*self) |buf, len| {
do self.as_buf |buf, len| {
let mut ret = ~"";
// ignore the NULL terminator
let len = len - 1;
ret.reserve(nn * len);
unsafe {
do as_buf(ret) |rbuf, _len| {
do ret.as_buf |rbuf, _len| {
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
for nn.times {
@ -2010,8 +1994,8 @@ impl<'self> StrSlice<'self> for &'self str {
*/
#[inline]
fn subslice_offset(&self, inner: &str) -> uint {
do as_buf(*self) |a, a_len| {
do as_buf(inner) |b, b_len| {
do self.as_buf |a, a_len| {
do inner.as_buf |b, b_len| {
let a_start: uint;
let a_end: uint;
let b_start: uint;
@ -2027,6 +2011,22 @@ impl<'self> StrSlice<'self> for &'self str {
}
}
/**
* Work with the byte buffer and length of a slice.
*
* The given length is one byte longer than the 'official' indexable
* length of the string. This is to permit probing the byte past the
* indexable area for a null byte, as is the case in slices pointing
* to full strings, or suffixes of them.
*/
#[inline]
fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T {
unsafe {
let v: *(*u8, uint) = cast::transmute(self);
let (buf, len) = *v;
f(buf, len)
}
}
}
#[allow(missing_doc)]
@ -2084,8 +2084,8 @@ impl OwnedStr for ~str {
let llen = self.len();
let rlen = rhs.len();
self.reserve(llen + rlen);
do as_buf(*self) |lbuf, _llen| {
do as_buf(rhs) |rbuf, _rlen| {
do self.as_buf |lbuf, _llen| {
do rhs.as_buf |rbuf, _rlen| {
let dst = ptr::offset(lbuf, llen);
let dst = ::cast::transmute_mut_unsafe(dst);
ptr::copy_memory(dst, rbuf, rlen);
@ -2102,8 +2102,8 @@ impl OwnedStr for ~str {
let llen = self.len();
let rlen = rhs.len();
self.reserve_at_least(llen + rlen);
do as_buf(*self) |lbuf, _llen| {
do as_buf(rhs) |rbuf, _rlen| {
do self.as_buf |lbuf, _llen| {
do rhs.as_buf |rbuf, _rlen| {
let dst = ptr::offset(lbuf, llen);
let dst = ::cast::transmute_mut_unsafe(dst);
ptr::copy_memory(dst, rbuf, rlen);
@ -2126,7 +2126,7 @@ impl OwnedStr for ~str {
let new_len = len + nb;
self.reserve_at_least(new_len);
let off = len;
do as_buf(*self) |buf, _len| {
do self.as_buf |buf, _len| {
let buf: *mut u8 = ::cast::transmute(buf);
match nb {
1u => {
@ -3091,20 +3091,20 @@ mod tests {
#[test]
fn test_as_buf() {
let a = "Abcdefg";
let b = as_buf(a, |buf, _l| {
let b = do a.as_buf |buf, _l| {
assert_eq!(unsafe { *buf }, 65u8);
100
});
};
assert_eq!(b, 100);
}
#[test]
fn test_as_buf_small() {
let a = "A";
let b = as_buf(a, |buf, _l| {
let b = do a.as_buf |buf, _l| {
assert_eq!(unsafe { *buf }, 65u8);
100
});
};
assert_eq!(b, 100);
}
@ -3112,7 +3112,7 @@ mod tests {
fn test_as_buf2() {
unsafe {
let s = ~"hello";
let sb = as_buf(s, |b, _l| b);
let sb = s.as_buf(|b, _l| b);
let s_cstr = raw::from_buf(sb);
assert_eq!(s_cstr, s);
}
@ -3121,7 +3121,7 @@ mod tests {
#[test]
fn test_as_buf_3() {
let a = ~"hello";
do as_buf(a) |buf, len| {
do a.as_buf |buf, len| {
unsafe {
assert_eq!(a[0], 'h' as u8);
assert_eq!(*buf, 'h' as u8);

View File

@ -18,6 +18,7 @@ use io;
use libc;
use libc::{c_char, size_t};
use repr;
use str::StrSlice;
use str;
use unstable::intrinsics;
@ -122,8 +123,8 @@ pub trait FailWithCause {
impl FailWithCause for ~str {
fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
do str::as_buf(cause) |msg_buf, _msg_len| {
do str::as_buf(file) |file_buf, _file_len| {
do cause.as_buf |msg_buf, _msg_len| {
do file.as_buf |file_buf, _file_len| {
unsafe {
let msg_buf = cast::transmute(msg_buf);
let file_buf = cast::transmute(file_buf);
@ -136,8 +137,8 @@ impl FailWithCause for ~str {
impl FailWithCause for &'static str {
fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
do str::as_buf(cause) |msg_buf, _msg_len| {
do str::as_buf(file) |file_buf, _file_len| {
do cause.as_buf |msg_buf, _msg_len| {
do file.as_buf |file_buf, _file_len| {
unsafe {
let msg_buf = cast::transmute(msg_buf);
let file_buf = cast::transmute(file_buf);

View File

@ -56,7 +56,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
index: size_t, len: size_t) {
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
len as int, index as int);
do str::as_buf(msg) |p, _len| {
do msg.as_buf |p, _len| {
fail_(p as *c_char, file, line);
}
}

View File

@ -20,11 +20,11 @@ mod libc {
}
fn atol(s: ~str) -> int {
return str::as_buf(s, { |x, _len| unsafe { libc::atol(x) } });
s.as_buf(|x, _len| unsafe { libc::atol(x) })
}
fn atoll(s: ~str) -> i64 {
return str::as_buf(s, { |x, _len| unsafe { libc::atoll(x) } });
s.as_buf(|x, _len| unsafe { libc::atoll(x) })
}
pub fn main() {