about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2012-02-08 11:22:07 -0800
committerBrian Anderson <andersrb@gmail.com>2012-02-08 11:22:07 -0800
commit5f8e0ddccfe4d9797b1e2b293788d023d9916e31 (patch)
tree2ee46cc5d07f11412b104fd1c73fb2be56f1882e
parent526e73d7f882bf9a88fe957661cc2e09291cef5b (diff)
parent3a413aabd4ccb0c0f8d6b3e5f380ac47164a1ee3 (diff)
downloadrust-5f8e0ddccfe4d9797b1e2b293788d023d9916e31.tar.gz
rust-5f8e0ddccfe4d9797b1e2b293788d023d9916e31.zip
Merge pull request #1783 from erickt/master
Adding a str::as_bytes<T>(str, fn([u8] -> T) -> T function
-rw-r--r--src/libcore/str.rs51
1 files changed, 31 insertions, 20 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 7af87f77ecf..39f5d121ad1 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -95,6 +95,7 @@ export
    char_at,
    substr_all,
    escape_char,
+   as_bytes,
    as_buf,
    //buf,
    sbuf,
@@ -390,10 +391,7 @@ Converts a string to a vector of bytes. The result vector is not
 null-terminated.
 */
 fn bytes(s: str) -> [u8] unsafe {
-    let v = ::unsafe::reinterpret_cast(s);
-    let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
-    ::unsafe::leak(v);
-    ret vcopy;
+    as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) }
 }
 
 /*
@@ -1026,12 +1024,12 @@ Returns the length in bytes of a string
 FIXME: rename to 'len_bytes'
 */
 pure fn byte_len(s: str) -> uint unsafe {
-    let v: [u8] = ::unsafe::reinterpret_cast(s);
-    let vlen = vec::len(v);
-    ::unsafe::leak(v);
-    // There should always be a null terminator
-    assert (vlen > 0u);
-    ret vlen - 1u;
+    as_bytes(s) { |v|
+        let vlen = vec::len(v);
+        // There should always be a null terminator
+        assert (vlen > 0u);
+        vlen - 1u
+    }
 }
 
 /*
@@ -1299,24 +1297,39 @@ const max_five_b: uint = 67108864u;
 const tag_six_b: uint = 252u;
 
 /*
-Function: as_buf
+Function: as_bytes
 
 Work with the byte buffer of a string. Allows for unsafe manipulation
 of strings, which is useful for native interop.
 
 Example:
 
-> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
+> let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
 
 */
-fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
+fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
     let v: [u8] = ::unsafe::reinterpret_cast(s);
-    let r = vec::as_buf(v, f);
+    let r = f(v);
     ::unsafe::leak(v);
     r
 }
 
 /*
+Function: as_buf
+
+Work with the byte buffer of a string. Allows for unsafe manipulation
+of strings, which is useful for native interop.
+
+Example:
+
+> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
+
+*/
+fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
+    as_bytes(s) { |v| vec::as_buf(v, f) }
+}
+
+/*
 Type: sbuf
 
 An unsafe buffer of bytes. Corresponds to a C char pointer.
@@ -1373,13 +1386,11 @@ mod unsafe {
        assert (begin <= end);
        assert (end <= byte_len(s));
 
-       let v: [u8] = ::unsafe::reinterpret_cast(s);
-       let v2 = vec::slice(v, begin, end);
+       let v = as_bytes(s) { |v| vec::slice(v, begin, end) };
+       v += [0u8];
+       let s: str = ::unsafe::reinterpret_cast(v);
        ::unsafe::leak(v);
-       v2 += [0u8];
-       let s2: str = ::unsafe::reinterpret_cast(v2);
-       ::unsafe::leak(v2);
-       ret s2;
+       ret s;
    }
 
    /*