about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-06-11 13:10:37 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-06-12 12:21:04 +1000
commitefc71a8bdb28fba88d0cc8916b33838bf43b3a8d (patch)
treead0086d4319facd8da21583e19a952a01250bbbd /src/libextra
parentba4a4778cc17c64c33a891a0d2565a1fb04ddffc (diff)
downloadrust-efc71a8bdb28fba88d0cc8916b33838bf43b3a8d.tar.gz
rust-efc71a8bdb28fba88d0cc8916b33838bf43b3a8d.zip
std: unify the str -> [u8] functions as 3 methods: .as_bytes() and .as_bytes_with_null[_consume]().
The first acts on &str and is not nul-terminated, the last two act on strings
that are always null terminated (&'static str, ~str and @str).
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/base64.rs19
-rw-r--r--src/libextra/ebml.rs5
-rw-r--r--src/libextra/fileinput.rs2
-rw-r--r--src/libextra/md4.rs3
-rw-r--r--src/libextra/net_tcp.rs20
-rw-r--r--src/libextra/net_url.rs2
-rw-r--r--src/libextra/num/bigint.rs4
-rw-r--r--src/libextra/sha1.rs4
-rw-r--r--src/libextra/stats.rs1
-rw-r--r--src/libextra/terminfo/parm.rs7
-rw-r--r--src/libextra/treemap.rs8
-rw-r--r--src/libextra/uv_ll.rs4
12 files changed, 37 insertions, 42 deletions
diff --git a/src/libextra/base64.rs b/src/libextra/base64.rs
index 62d4a32b8e2..f4754b3e4cb 100644
--- a/src/libextra/base64.rs
+++ b/src/libextra/base64.rs
@@ -12,7 +12,6 @@
 
 use core::prelude::*;
 
-use core::str;
 use core::vec;
 
 /// A trait for converting a value to base64 encoding.
@@ -111,7 +110,7 @@ impl<'self> ToBase64 for &'self str {
      *
      */
     fn to_base64(&self) -> ~str {
-        str::to_bytes(*self).to_base64()
+        self.as_bytes().to_base64()
     }
 }
 
@@ -224,7 +223,7 @@ impl<'self> FromBase64 for &'self str {
      * ~~~
      */
     fn from_base64(&self) -> ~[u8] {
-        str::to_bytes(*self).from_base64()
+        self.as_bytes().from_base64()
     }
 }
 
@@ -245,12 +244,12 @@ mod tests {
 
     #[test]
     fn test_from_base64() {
-        assert_eq!("".from_base64(), str::to_bytes(""));
-        assert_eq!("Zg==".from_base64(), str::to_bytes("f"));
-        assert_eq!("Zm8=".from_base64(), str::to_bytes("fo"));
-        assert_eq!("Zm9v".from_base64(), str::to_bytes("foo"));
-        assert_eq!("Zm9vYg==".from_base64(), str::to_bytes("foob"));
-        assert_eq!("Zm9vYmE=".from_base64(), str::to_bytes("fooba"))
-        assert_eq!("Zm9vYmFy".from_base64(), str::to_bytes("foobar"));
+        assert_eq!("".from_base64(), "".as_bytes().to_owned());
+        assert_eq!("Zg==".from_base64(), "f".as_bytes().to_owned());
+        assert_eq!("Zm8=".from_base64(), "fo".as_bytes().to_owned());
+        assert_eq!("Zm9v".from_base64(), "foo".as_bytes().to_owned());
+        assert_eq!("Zm9vYg==".from_base64(), "foob".as_bytes().to_owned());
+        assert_eq!("Zm9vYmE=".from_base64(), "fooba".as_bytes().to_owned());
+        assert_eq!("Zm9vYmFy".from_base64(), "foobar".as_bytes().to_owned());
     }
 }
diff --git a/src/libextra/ebml.rs b/src/libextra/ebml.rs
index 233c8042640..dd08f23a7a1 100644
--- a/src/libextra/ebml.rs
+++ b/src/libextra/ebml.rs
@@ -607,7 +607,6 @@ pub mod writer {
 
     use core::cast;
     use core::io;
-    use core::str;
 
     // ebml writing
     pub struct Encoder {
@@ -725,7 +724,7 @@ pub mod writer {
         }
 
         pub fn wr_tagged_str(&mut self, tag_id: uint, v: &str) {
-            str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b));
+            self.wr_tagged_bytes(tag_id, v.as_bytes());
         }
 
         pub fn wr_bytes(&mut self, b: &[u8]) {
@@ -735,7 +734,7 @@ pub mod writer {
 
         pub fn wr_str(&mut self, s: &str) {
             debug!("Write str: %?", s);
-            self.writer.write(str::to_bytes(s));
+            self.writer.write(s.as_bytes());
         }
     }
 
diff --git a/src/libextra/fileinput.rs b/src/libextra/fileinput.rs
index add857ca9ed..345b0e8cff7 100644
--- a/src/libextra/fileinput.rs
+++ b/src/libextra/fileinput.rs
@@ -487,7 +487,7 @@ mod test {
         let mut buf : ~[u8] = vec::from_elem(6, 0u8);
         let count = fi.read(buf, 10);
         assert_eq!(count, 6);
-        assert_eq!(buf, "0\n1\n2\n".to_bytes());
+        assert_eq!(buf, "0\n1\n2\n".as_bytes().to_owned());
         assert!(fi.eof())
         assert_eq!(fi.state().line_num, 3);
     }
diff --git a/src/libextra/md4.rs b/src/libextra/md4.rs
index 01edbbe366d..94793804bb1 100644
--- a/src/libextra/md4.rs
+++ b/src/libextra/md4.rs
@@ -10,7 +10,6 @@
 
 use core::prelude::*;
 
-use core::str;
 use core::uint;
 use core::vec;
 
@@ -129,7 +128,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
 
 /// Calculates the md4 hash of a string, returning the hex-encoded version of
 /// the hash
-pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
+pub fn md4_text(msg: &str) -> ~str { md4_str(msg.as_bytes()) }
 
 #[test]
 fn test_md4() {
diff --git a/src/libextra/net_tcp.rs b/src/libextra/net_tcp.rs
index 3ea085f5e86..d95807f2b91 100644
--- a/src/libextra/net_tcp.rs
+++ b/src/libextra/net_tcp.rs
@@ -1636,7 +1636,7 @@ mod test {
         assert_eq!(net::ip::get_port(&sock.get_peer_addr()), 8887);
 
         // Fulfill the protocol the test server expects
-        let resp_bytes = str::to_bytes("ping");
+        let resp_bytes = "ping".as_bytes().to_owned();
         tcp_write_single(&sock, resp_bytes);
         debug!("message sent");
         sock.read(0u);
@@ -1756,9 +1756,7 @@ mod test {
         buf_write(sock_buf, expected_req);
 
         // so contrived!
-        let actual_resp = do str::as_bytes(&expected_resp.to_str()) |resp_buf| {
-            buf_read(sock_buf, resp_buf.len())
-        };
+        let actual_resp = buf_read(sock_buf, expected_resp.as_bytes().len());
 
         let actual_req = server_result_po.recv();
         debug!("REQ: expected: '%s' actual: '%s'",
@@ -1810,11 +1808,10 @@ mod test {
 
     fn buf_write<W:io::Writer>(w: &W, val: &str) {
         debug!("BUF_WRITE: val len %?", val.len());
-        do str::byte_slice(val) |b_slice| {
-            debug!("BUF_WRITE: b_slice len %?",
-                            b_slice.len());
-            w.write(b_slice)
-        }
+        let b_slice = val.as_bytes();
+        debug!("BUF_WRITE: b_slice len %?",
+               b_slice.len());
+        w.write(b_slice)
     }
 
     fn buf_read<R:io::Reader>(r: &R, len: uint) -> ~str {
@@ -1877,7 +1874,8 @@ mod test {
                             server_ch.send(
                                 str::from_bytes(data));
                             debug!("SERVER: before write");
-                            tcp_write_single(&sock, str::to_bytes(resp_cell2.take()));
+                            let s = resp_cell2.take();
+                            tcp_write_single(&sock, s.as_bytes().to_owned());
                             debug!("SERVER: after write.. die");
                             kill_ch.send(None);
                           }
@@ -1949,7 +1947,7 @@ mod test {
         }
         else {
             let sock = result::unwrap(connect_result);
-            let resp_bytes = str::to_bytes(resp);
+            let resp_bytes = resp.as_bytes().to_owned();
             tcp_write_single(&sock, resp_bytes);
             let read_result = sock.read(0u);
             if read_result.is_err() {
diff --git a/src/libextra/net_url.rs b/src/libextra/net_url.rs
index 83cda31c680..31d728f1813 100644
--- a/src/libextra/net_url.rs
+++ b/src/libextra/net_url.rs
@@ -1060,7 +1060,7 @@ mod tests {
         /*
         assert_eq!(decode_form_urlencoded([]).len(), 0);
 
-        let s = str::to_bytes("a=1&foo+bar=abc&foo+bar=12+%3D+34");
+        let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes();
         let form = decode_form_urlencoded(s);
         assert_eq!(form.len(), 2);
         assert_eq!(form.get_ref(&~"a"), &~[~"1"]);
diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs
index bd6425d779c..18ba4fb9c76 100644
--- a/src/libextra/num/bigint.rs
+++ b/src/libextra/num/bigint.rs
@@ -534,7 +534,7 @@ impl FromStrRadix for BigUint {
 
     pub fn from_str_radix(s: &str, radix: uint)
         -> Option<BigUint> {
-        BigUint::parse_bytes(str::to_bytes(s), radix)
+        BigUint::parse_bytes(s.as_bytes(), radix)
     }
 }
 
@@ -1090,7 +1090,7 @@ impl FromStrRadix for BigInt {
 
     fn from_str_radix(s: &str, radix: uint)
         -> Option<BigInt> {
-        BigInt::parse_bytes(str::to_bytes(s), radix)
+        BigInt::parse_bytes(s.as_bytes(), radix)
     }
 }
 
diff --git a/src/libextra/sha1.rs b/src/libextra/sha1.rs
index 658621e25bd..03ceded0073 100644
--- a/src/libextra/sha1.rs
+++ b/src/libextra/sha1.rs
@@ -25,7 +25,6 @@
 use core::prelude::*;
 
 use core::iterator::IteratorUtil;
-use core::str;
 use core::uint;
 use core::vec;
 
@@ -246,8 +245,7 @@ pub fn sha1() -> @Sha1 {
         }
         fn input(&mut self, msg: &const [u8]) { add_input(self, msg); }
         fn input_str(&mut self, msg: &str) {
-            let bs = str::to_bytes(msg);
-            add_input(self, bs);
+            add_input(self, msg.as_bytes());
         }
         fn result(&mut self) -> ~[u8] { return mk_result(self); }
         fn result_str(&mut self) -> ~str {
diff --git a/src/libextra/stats.rs b/src/libextra/stats.rs
index a9034074d93..17bdf6c3a1d 100644
--- a/src/libextra/stats.rs
+++ b/src/libextra/stats.rs
@@ -13,7 +13,6 @@
 use core::prelude::*;
 
 use core::iterator::*;
-use core::vec;
 use core::f64;
 use core::cmp;
 use core::num;
diff --git a/src/libextra/terminfo/parm.rs b/src/libextra/terminfo/parm.rs
index 4eb48f60a99..40191c89925 100644
--- a/src/libextra/terminfo/parm.rs
+++ b/src/libextra/terminfo/parm.rs
@@ -85,11 +85,14 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
                         _       => return Err(~"a non-char was used with %c")
                     },
                     's' => match stack.pop() {
-                        String(s) => output.push_all(s.to_bytes()),
+                        String(s) => output.push_all(s.as_bytes()),
                         _         => return Err(~"a non-str was used with %s")
                     },
                     'd' => match stack.pop() {
-                        Number(x) => output.push_all(x.to_str().to_bytes()),
+                        Number(x) => {
+                            let s = x.to_str();
+                            output.push_all(s.as_bytes())
+                        }
                         _         => return Err(~"a non-number was used with %d")
                     },
                     'p' => state = PushParam,
diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs
index 3f74b905f2a..0918ab8ddad 100644
--- a/src/libextra/treemap.rs
+++ b/src/libextra/treemap.rs
@@ -769,10 +769,10 @@ mod test_treemap {
     fn u8_map() {
         let mut m = TreeMap::new();
 
-        let k1 = str::to_bytes("foo");
-        let k2 = str::to_bytes("bar");
-        let v1 = str::to_bytes("baz");
-        let v2 = str::to_bytes("foobar");
+        let k1 = "foo".as_bytes();
+        let k2 = "bar".as_bytes();
+        let v1 = "baz".as_bytes();
+        let v2 = "foobar".as_bytes();
 
         m.insert(copy k1, copy v1);
         m.insert(copy k2, copy v2);
diff --git a/src/libextra/uv_ll.rs b/src/libextra/uv_ll.rs
index 187960b9101..744f4555d5c 100644
--- a/src/libextra/uv_ll.rs
+++ b/src/libextra/uv_ll.rs
@@ -1368,7 +1368,7 @@ mod test {
             // In C, this would be a malloc'd or stack-allocated
             // struct that we'd cast to a void* and store as the
             // data field in our uv_connect_t struct
-            let req_str_bytes = str::to_bytes(req_str);
+            let req_str_bytes = req_str.as_bytes();
             let req_msg_ptr: *u8 = vec::raw::to_ptr(req_str_bytes);
             debug!("req_msg ptr: %u", req_msg_ptr as uint);
             let req_msg = ~[
@@ -1626,7 +1626,7 @@ mod test {
             let server_write_req = write_t();
             let server_write_req_ptr: *uv_write_t = &server_write_req;
 
-            let resp_str_bytes = str::to_bytes(server_resp_msg);
+            let resp_str_bytes = server_resp_msg.as_bytes();
             let resp_msg_ptr: *u8 = vec::raw::to_ptr(resp_str_bytes);
             debug!("resp_msg ptr: %u", resp_msg_ptr as uint);
             let resp_msg = ~[