]> git.lizzy.rs Git - rust.git/commitdiff
std: move str::as_buf into StrSlice
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Thu, 11 Jul 2013 00:33:11 +0000 (17:33 -0700)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Tue, 23 Jul 2013 23:56:22 +0000 (16:56 -0700)
src/libstd/option.rs
src/libstd/rt/borrowck.rs
src/libstd/str.rs
src/libstd/sys.rs
src/libstd/unstable/lang.rs
src/test/run-pass/c-stack-returning-int64.rs

index f5e5dbb3dbf7ff5db30dddd4fa8d9b62799b9274..9fe590e28eef2a9adb0102c35b655fb24a4fef2b 100644 (file)
@@ -50,7 +50,6 @@
 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);
 }
 
index 60df2d5c11ba2420014ea55a1443449da2a8dbcc..42e545ce1a9711527eb58e345f2a2ffd5e30786e 100644 (file)
@@ -16,7 +16,7 @@
 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)
                 }
             }
index 3987f260065560d5a63593b40e0b7bb7ea781b0f..c16e87000f5152d588e7fb38446f6b7a6b25189a 100644 (file)
@@ -17,7 +17,6 @@
  */
 
 use at_vec;
-use cast::transmute;
 use cast;
 use char;
 use char::Char;
@@ -192,10 +191,10 @@ pub fn concat(&self) -> ~str {
         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 @@ pub fn connect(&self, sep: &str) -> ~str {
         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 @@ pub fn replace(s: &str, from: &str, to: &str) -> ~str {
 #[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 unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
      * 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 unsafe fn slice_bytes_owned(s: &str, begin: uint, end: uint) -> ~str {
      */
     #[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 unsafe fn slice_bytes(s: &str, begin: uint, end: uint) -> &str {
     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 @@ fn as_slice<'a>(&'a self) -> &'a 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 @@ fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint, allow_trailing_
     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 @@ fn find_str(&self, needle: &str) -> Option<uint> {
 
     /// 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 @@ fn lev_distance(&self, t: &str) -> uint {
      */
     #[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 @@ fn subslice_offset(&self, inner: &str) -> uint {
         }
     }
 
+    /**
+     * 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 @@ fn push_str_no_overallocate(&mut self, rhs: &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 @@ fn push_str(&mut self, rhs: &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 @@ fn push_char(&mut self, c: char) {
             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 @@ fn test_as_bytes_fail() {
     #[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 @@ fn test_as_buf_small() {
     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 @@ fn test_as_buf2() {
     #[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);
index d50d90376abe078801687d1234798f208084c7d2..0c497ecef271d55be20e0066e7e27cdd65f0a742 100644 (file)
@@ -18,6 +18,7 @@
 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 @@ fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
 
 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);
index 92357f210a5d2445cffe223409956670427f312e..f8cd08b4bf6144096fceb9c8b1a3dcd8a1468b76 100644 (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);
     }
 }
index d09f8493d805a7a849143ce55e6a12c3ae4af0bd..12fa8066686d5b8fb9df2828ad3698bc625349fe 100644 (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() {