Skip to content

Commit efc71a8

Browse files
committed
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).
1 parent ba4a477 commit efc71a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+258
-221
lines changed

src/libextra/base64.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use core::prelude::*;
1414

15-
use core::str;
1615
use core::vec;
1716

1817
/// A trait for converting a value to base64 encoding.
@@ -111,7 +110,7 @@ impl<'self> ToBase64 for &'self str {
111110
*
112111
*/
113112
fn to_base64(&self) -> ~str {
114-
str::to_bytes(*self).to_base64()
113+
self.as_bytes().to_base64()
115114
}
116115
}
117116

@@ -224,7 +223,7 @@ impl<'self> FromBase64 for &'self str {
224223
* ~~~
225224
*/
226225
fn from_base64(&self) -> ~[u8] {
227-
str::to_bytes(*self).from_base64()
226+
self.as_bytes().from_base64()
228227
}
229228
}
230229

@@ -245,12 +244,12 @@ mod tests {
245244

246245
#[test]
247246
fn test_from_base64() {
248-
assert_eq!("".from_base64(), str::to_bytes(""));
249-
assert_eq!("Zg==".from_base64(), str::to_bytes("f"));
250-
assert_eq!("Zm8=".from_base64(), str::to_bytes("fo"));
251-
assert_eq!("Zm9v".from_base64(), str::to_bytes("foo"));
252-
assert_eq!("Zm9vYg==".from_base64(), str::to_bytes("foob"));
253-
assert_eq!("Zm9vYmE=".from_base64(), str::to_bytes("fooba"))
254-
assert_eq!("Zm9vYmFy".from_base64(), str::to_bytes("foobar"));
247+
assert_eq!("".from_base64(), "".as_bytes().to_owned());
248+
assert_eq!("Zg==".from_base64(), "f".as_bytes().to_owned());
249+
assert_eq!("Zm8=".from_base64(), "fo".as_bytes().to_owned());
250+
assert_eq!("Zm9v".from_base64(), "foo".as_bytes().to_owned());
251+
assert_eq!("Zm9vYg==".from_base64(), "foob".as_bytes().to_owned());
252+
assert_eq!("Zm9vYmE=".from_base64(), "fooba".as_bytes().to_owned());
253+
assert_eq!("Zm9vYmFy".from_base64(), "foobar".as_bytes().to_owned());
255254
}
256255
}

src/libextra/ebml.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ pub mod writer {
607607

608608
use core::cast;
609609
use core::io;
610-
use core::str;
611610

612611
// ebml writing
613612
pub struct Encoder {
@@ -725,7 +724,7 @@ pub mod writer {
725724
}
726725

727726
pub fn wr_tagged_str(&mut self, tag_id: uint, v: &str) {
728-
str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b));
727+
self.wr_tagged_bytes(tag_id, v.as_bytes());
729728
}
730729

731730
pub fn wr_bytes(&mut self, b: &[u8]) {
@@ -735,7 +734,7 @@ pub mod writer {
735734

736735
pub fn wr_str(&mut self, s: &str) {
737736
debug!("Write str: %?", s);
738-
self.writer.write(str::to_bytes(s));
737+
self.writer.write(s.as_bytes());
739738
}
740739
}
741740

src/libextra/fileinput.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ mod test {
487487
let mut buf : ~[u8] = vec::from_elem(6, 0u8);
488488
let count = fi.read(buf, 10);
489489
assert_eq!(count, 6);
490-
assert_eq!(buf, "0\n1\n2\n".to_bytes());
490+
assert_eq!(buf, "0\n1\n2\n".as_bytes().to_owned());
491491
assert!(fi.eof())
492492
assert_eq!(fi.state().line_num, 3);
493493
}

src/libextra/md4.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use core::prelude::*;
1212

13-
use core::str;
1413
use core::uint;
1514
use core::vec;
1615

@@ -129,7 +128,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
129128

130129
/// Calculates the md4 hash of a string, returning the hex-encoded version of
131130
/// the hash
132-
pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
131+
pub fn md4_text(msg: &str) -> ~str { md4_str(msg.as_bytes()) }
133132

134133
#[test]
135134
fn test_md4() {

src/libextra/net_tcp.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ mod test {
16361636
assert_eq!(net::ip::get_port(&sock.get_peer_addr()), 8887);
16371637
16381638
// Fulfill the protocol the test server expects
1639-
let resp_bytes = str::to_bytes("ping");
1639+
let resp_bytes = "ping".as_bytes().to_owned();
16401640
tcp_write_single(&sock, resp_bytes);
16411641
debug!("message sent");
16421642
sock.read(0u);
@@ -1756,9 +1756,7 @@ mod test {
17561756
buf_write(sock_buf, expected_req);
17571757
17581758
// so contrived!
1759-
let actual_resp = do str::as_bytes(&expected_resp.to_str()) |resp_buf| {
1760-
buf_read(sock_buf, resp_buf.len())
1761-
};
1759+
let actual_resp = buf_read(sock_buf, expected_resp.as_bytes().len());
17621760
17631761
let actual_req = server_result_po.recv();
17641762
debug!("REQ: expected: '%s' actual: '%s'",
@@ -1810,11 +1808,10 @@ mod test {
18101808

18111809
fn buf_write<W:io::Writer>(w: &W, val: &str) {
18121810
debug!("BUF_WRITE: val len %?", val.len());
1813-
do str::byte_slice(val) |b_slice| {
1814-
debug!("BUF_WRITE: b_slice len %?",
1815-
b_slice.len());
1816-
w.write(b_slice)
1817-
}
1811+
let b_slice = val.as_bytes();
1812+
debug!("BUF_WRITE: b_slice len %?",
1813+
b_slice.len());
1814+
w.write(b_slice)
18181815
}
18191816

18201817
fn buf_read<R:io::Reader>(r: &R, len: uint) -> ~str {
@@ -1877,7 +1874,8 @@ mod test {
18771874
server_ch.send(
18781875
str::from_bytes(data));
18791876
debug!("SERVER: before write");
1880-
tcp_write_single(&sock, str::to_bytes(resp_cell2.take()));
1877+
let s = resp_cell2.take();
1878+
tcp_write_single(&sock, s.as_bytes().to_owned());
18811879
debug!("SERVER: after write.. die");
18821880
kill_ch.send(None);
18831881
}
@@ -1949,7 +1947,7 @@ mod test {
19491947
}
19501948
else {
19511949
let sock = result::unwrap(connect_result);
1952-
let resp_bytes = str::to_bytes(resp);
1950+
let resp_bytes = resp.as_bytes().to_owned();
19531951
tcp_write_single(&sock, resp_bytes);
19541952
let read_result = sock.read(0u);
19551953
if read_result.is_err() {

src/libextra/net_url.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ mod tests {
10601060
/*
10611061
assert_eq!(decode_form_urlencoded([]).len(), 0);
10621062
1063-
let s = str::to_bytes("a=1&foo+bar=abc&foo+bar=12+%3D+34");
1063+
let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes();
10641064
let form = decode_form_urlencoded(s);
10651065
assert_eq!(form.len(), 2);
10661066
assert_eq!(form.get_ref(&~"a"), &~[~"1"]);

src/libextra/num/bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl FromStrRadix for BigUint {
534534
535535
pub fn from_str_radix(s: &str, radix: uint)
536536
-> Option<BigUint> {
537-
BigUint::parse_bytes(str::to_bytes(s), radix)
537+
BigUint::parse_bytes(s.as_bytes(), radix)
538538
}
539539
}
540540

@@ -1090,7 +1090,7 @@ impl FromStrRadix for BigInt {
10901090
10911091
fn from_str_radix(s: &str, radix: uint)
10921092
-> Option<BigInt> {
1093-
BigInt::parse_bytes(str::to_bytes(s), radix)
1093+
BigInt::parse_bytes(s.as_bytes(), radix)
10941094
}
10951095
}
10961096

src/libextra/sha1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use core::prelude::*;
2626

2727
use core::iterator::IteratorUtil;
28-
use core::str;
2928
use core::uint;
3029
use core::vec;
3130

@@ -246,8 +245,7 @@ pub fn sha1() -> @Sha1 {
246245
}
247246
fn input(&mut self, msg: &const [u8]) { add_input(self, msg); }
248247
fn input_str(&mut self, msg: &str) {
249-
let bs = str::to_bytes(msg);
250-
add_input(self, bs);
248+
add_input(self, msg.as_bytes());
251249
}
252250
fn result(&mut self) -> ~[u8] { return mk_result(self); }
253251
fn result_str(&mut self) -> ~str {

src/libextra/stats.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use core::prelude::*;
1414

1515
use core::iterator::*;
16-
use core::vec;
1716
use core::f64;
1817
use core::cmp;
1918
use core::num;

src/libextra/terminfo/parm.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
8585
_ => return Err(~"a non-char was used with %c")
8686
},
8787
's' => match stack.pop() {
88-
String(s) => output.push_all(s.to_bytes()),
88+
String(s) => output.push_all(s.as_bytes()),
8989
_ => return Err(~"a non-str was used with %s")
9090
},
9191
'd' => match stack.pop() {
92-
Number(x) => output.push_all(x.to_str().to_bytes()),
92+
Number(x) => {
93+
let s = x.to_str();
94+
output.push_all(s.as_bytes())
95+
}
9396
_ => return Err(~"a non-number was used with %d")
9497
},
9598
'p' => state = PushParam,

0 commit comments

Comments
 (0)