Skip to content

Commit

Permalink
Rename str::bytes to str::to_bytes
Browse files Browse the repository at this point in the history
Closes #3245
  • Loading branch information
catamorphism committed Aug 23, 2012
1 parent 0698fc6 commit 9f59131
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 89 deletions.
10 changes: 5 additions & 5 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ fn as_hex(data: ~[u8]) -> ~str {
}
fn sha1(data: ~str) -> ~str unsafe {
let bytes = str::bytes(data);
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes) as c_uint, ptr::null());
return as_hex(vec::unsafe::from_buf(hash, 20u));
Expand Down Expand Up @@ -2813,7 +2813,7 @@ The `sha1` function is the most obscure part of the program.
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
fn sha1(data: ~str) -> ~str {
unsafe {
let bytes = str::bytes(data);
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes), ptr::null());
return as_hex(vec::unsafe::from_buf(hash, 20u));
Expand Down Expand Up @@ -2856,16 +2856,16 @@ Let's look at our `sha1` function again.
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
# fn x(data: ~str) -> ~str {
# unsafe {
let bytes = str::bytes(data);
let bytes = str::to_bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes), ptr::null());
return as_hex(vec::unsafe::from_buf(hash, 20u));
# }
# }
~~~~

The `str::bytes` function is perfectly safe, it converts a string to
an `[u8]`. This byte array is then fed to `vec::unsafe::to_ptr`, which
The `str::to_bytes` function is perfectly safe: it converts a string to
a `[u8]`. This byte array is then fed to `vec::unsafe::to_ptr`, which
returns an unsafe pointer to its contents.

This pointer will become invalid as soon as the vector it points into
Expand Down
44 changes: 22 additions & 22 deletions src/libcore/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
}

/// Parse a string to an int
fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) }
fn from_str(s: ~str) -> option<T> { parse_buf(str::to_bytes(s), 10u) }

/// Convert to a string in a given base
fn to_str(n: T, radix: uint) -> ~str {
Expand Down Expand Up @@ -202,27 +202,27 @@ fn test_from_str() {
#[test]
#[ignore]
fn test_parse_buf() {
import str::bytes;
assert parse_buf(bytes(~"123"), 10u) == some(123 as T);
assert parse_buf(bytes(~"1001"), 2u) == some(9 as T);
assert parse_buf(bytes(~"123"), 8u) == some(83 as T);
assert parse_buf(bytes(~"123"), 16u) == some(291 as T);
assert parse_buf(bytes(~"ffff"), 16u) == some(65535 as T);
assert parse_buf(bytes(~"FFFF"), 16u) == some(65535 as T);
assert parse_buf(bytes(~"z"), 36u) == some(35 as T);
assert parse_buf(bytes(~"Z"), 36u) == some(35 as T);

assert parse_buf(bytes(~"-123"), 10u) == some(-123 as T);
assert parse_buf(bytes(~"-1001"), 2u) == some(-9 as T);
assert parse_buf(bytes(~"-123"), 8u) == some(-83 as T);
assert parse_buf(bytes(~"-123"), 16u) == some(-291 as T);
assert parse_buf(bytes(~"-ffff"), 16u) == some(-65535 as T);
assert parse_buf(bytes(~"-FFFF"), 16u) == some(-65535 as T);
assert parse_buf(bytes(~"-z"), 36u) == some(-35 as T);
assert parse_buf(bytes(~"-Z"), 36u) == some(-35 as T);

assert parse_buf(str::bytes(~"Z"), 35u) == none;
assert parse_buf(str::bytes(~"-9"), 2u) == none;
import str::to_bytes;
assert parse_buf(to_bytes(~"123"), 10u) == some(123 as T);
assert parse_buf(to_bytes(~"1001"), 2u) == some(9 as T);
assert parse_buf(to_bytes(~"123"), 8u) == some(83 as T);
assert parse_buf(to_bytes(~"123"), 16u) == some(291 as T);
assert parse_buf(to_bytes(~"ffff"), 16u) == some(65535 as T);
assert parse_buf(to_bytes(~"FFFF"), 16u) == some(65535 as T);
assert parse_buf(to_bytes(~"z"), 36u) == some(35 as T);
assert parse_buf(to_bytes(~"Z"), 36u) == some(35 as T);

assert parse_buf(to_bytes(~"-123"), 10u) == some(-123 as T);
assert parse_buf(to_bytes(~"-1001"), 2u) == some(-9 as T);
assert parse_buf(to_bytes(~"-123"), 8u) == some(-83 as T);
assert parse_buf(to_bytes(~"-123"), 16u) == some(-291 as T);
assert parse_buf(to_bytes(~"-ffff"), 16u) == some(-65535 as T);
assert parse_buf(to_bytes(~"-FFFF"), 16u) == some(-65535 as T);
assert parse_buf(to_bytes(~"-z"), 36u) == some(-35 as T);
assert parse_buf(to_bytes(~"-Z"), 36u) == some(-35 as T);

assert parse_buf(to_bytes(~"Z"), 35u) == none;
assert parse_buf(to_bytes(~"-9"), 2u) == none;
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ fn with_bytes_reader_between<t>(bytes: ~[u8], start: uint, end: uint,
}

fn str_reader(s: ~str) -> Reader {
bytes_reader(str::bytes(s))
bytes_reader(str::to_bytes(s))
}

fn with_str_reader<T>(s: ~str, f: fn(Reader) -> T) -> T {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ mod tests {
};
assert (ostream as uint != 0u);
let s = ~"hello";
let mut buf = vec::to_mut(str::bytes(s) + ~[0 as u8]);
let mut buf = vec::to_mut(str::to_bytes(s) + ~[0 as u8]);
do vec::as_mut_buf(buf) |b, _len| {
assert (libc::fwrite(b as *c_void, 1u as size_t,
(str::len(s) + 1u) as size_t, ostream)
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export
trim,

// Transforming strings
bytes,
to_bytes,
byte_slice,
chars,
substr,
Expand Down Expand Up @@ -372,7 +372,7 @@ Section: Transforming strings
*
* The result vector is not null-terminated.
*/
pure fn bytes(s: &str) -> ~[u8] {
pure fn to_bytes(s: &str) -> ~[u8] {
unsafe {
let mut s_copy = from_slice(s);
let mut v: ~[u8] = ::unsafe::transmute(s_copy);
Expand Down Expand Up @@ -2727,7 +2727,7 @@ mod tests {
fn vec_str_conversions() {
let s1: ~str = ~"All mimsy were the borogoves";

let v: ~[u8] = bytes(s1);
let v: ~[u8] = to_bytes(s1);
let s2: ~str = from_bytes(v);
let mut i: uint = 0u;
let n1: uint = len(s1);
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/to_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl @~[u8]: ToBytes {
}

impl ~str: ToBytes {
fn to_bytes() -> ~[u8] { str::bytes(self) }
fn to_bytes() -> ~[u8] { str::to_bytes(self) }
}

impl @(~str): ToBytes {
fn to_bytes() -> ~[u8] { str::bytes(*self) }
fn to_bytes() -> ~[u8] { str::to_bytes(*self) }
}
22 changes: 11 additions & 11 deletions src/libcore/uint-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn parse_buf(buf: &[const u8], radix: uint) -> option<T> {
}

/// Parse a string to an int
fn from_str(s: ~str) -> option<T> { parse_buf(str::bytes(s), 10u) }
fn from_str(s: ~str) -> option<T> { parse_buf(str::to_bytes(s), 10u) }

/// Parse a string as an unsigned integer.
fn from_str_radix(buf: ~str, radix: u64) -> option<u64> {
Expand Down Expand Up @@ -267,16 +267,16 @@ fn test_from_str() {
#[test]
#[ignore]
fn test_parse_buf() {
import str::bytes;
assert parse_buf(bytes(~"123"), 10u) == some(123u as T);
assert parse_buf(bytes(~"1001"), 2u) == some(9u as T);
assert parse_buf(bytes(~"123"), 8u) == some(83u as T);
assert parse_buf(bytes(~"123"), 16u) == some(291u as T);
assert parse_buf(bytes(~"ffff"), 16u) == some(65535u as T);
assert parse_buf(bytes(~"z"), 36u) == some(35u as T);

assert parse_buf(str::bytes(~"Z"), 10u) == none;
assert parse_buf(str::bytes(~"_"), 2u) == none;
import str::to_bytes;
assert parse_buf(to_bytes(~"123"), 10u) == some(123u as T);
assert parse_buf(to_bytes(~"1001"), 2u) == some(9u as T);
assert parse_buf(to_bytes(~"123"), 8u) == some(83u as T);
assert parse_buf(to_bytes(~"123"), 16u) == some(291u as T);
assert parse_buf(to_bytes(~"ffff"), 16u) == some(65535u as T);
assert parse_buf(to_bytes(~"z"), 36u) == some(35u as T);

assert parse_buf(to_bytes(~"Z"), 10u) == none;
assert parse_buf(to_bytes(~"_"), 2u) == none;
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions src/libstd/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ~[u8]: to_base64 {

impl ~str: to_base64 {
fn to_base64() -> ~str {
str::bytes(self).to_base64()
str::to_bytes(self).to_base64()
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ impl ~[u8]: from_base64 {

impl ~str: from_base64 {
fn from_base64() -> ~[u8] {
str::bytes(self).from_base64()
str::to_bytes(self).from_base64()
}
}

Expand All @@ -148,12 +148,12 @@ mod tests {

#[test]
fn test_from_base64() {
assert (~"").from_base64() == str::bytes(~"");
assert (~"Zg==").from_base64() == str::bytes(~"f");
assert (~"Zm8=").from_base64() == str::bytes(~"fo");
assert (~"Zm9v").from_base64() == str::bytes(~"foo");
assert (~"Zm9vYg==").from_base64() == str::bytes(~"foob");
assert (~"Zm9vYmE=").from_base64() == str::bytes(~"fooba");
assert (~"Zm9vYmFy").from_base64() == str::bytes(~"foobar");
assert (~"").from_base64() == str::to_bytes(~"");
assert (~"Zg==").from_base64() == str::to_bytes(~"f");
assert (~"Zm8=").from_base64() == str::to_bytes(~"fo");
assert (~"Zm9v").from_base64() == str::to_bytes(~"foo");
assert (~"Zm9vYg==").from_base64() == str::to_bytes(~"foob");
assert (~"Zm9vYmE=").from_base64() == str::to_bytes(~"fooba");
assert (~"Zm9vYmFy").from_base64() == str::to_bytes(~"foobar");
}
}
2 changes: 1 addition & 1 deletion src/libstd/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl writer {

fn wr_str(s: ~str) {
debug!("Write str: %?", s);
self.writer.write(str::bytes(s));
self.writer.write(str::to_bytes(s));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/md4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn md4_str(msg: ~[u8]) -> ~str {
result
}

fn md4_text(msg: ~str) -> ~str { md4_str(str::bytes(msg)) }
fn md4_text(msg: ~str) -> ~str { md4_str(str::to_bytes(msg)) }

#[test]
fn test_md4() {
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ mod test {
server_ch.send(
str::from_bytes(data));
log(debug, ~"SERVER: before write");
tcp_write_single(sock, str::bytes(resp));
tcp_write_single(sock, str::to_bytes(resp));
log(debug, ~"SERVER: after write.. die");
core::comm::send(kill_ch, none);
}
Expand Down Expand Up @@ -1599,7 +1599,7 @@ mod test {
}
else {
let sock = result::unwrap(connect_result);
let resp_bytes = str::bytes(resp);
let resp_bytes = str::to_bytes(resp);
tcp_write_single(sock, resp_bytes);
let read_result = sock.read(0u);
if read_result.is_err() {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/net_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ mod tests {

assert decode_form_urlencoded(~[]) == str_hash();

let s = str::bytes(~"a=1&foo+bar=abc&foo+bar=12+%3D+34");
let s = str::to_bytes(~"a=1&foo+bar=abc&foo+bar=12+%3D+34");
assert decode_form_urlencoded(s) == hash_from_strs(~[
(~"a", @dvec::from_elem(@~"1")),
(~"foo bar", @dvec::from_vec(~[mut @~"abc", @~"12 = 34"]))
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn sha1() -> sha1 {
self.computed = false;
}
fn input(msg: ~[u8]) { add_input(self, msg); }
fn input_str(msg: ~str) { add_input(self, str::bytes(msg)); }
fn input_str(msg: ~str) { add_input(self, str::to_bytes(msg)); }
fn result() -> ~[u8] { return mk_result(self); }
fn result_str() -> ~str {
let r = mk_result(self);
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ mod tests {
fn u8_map() {
let m = treemap();

let k1 = str::bytes(~"foo");
let k2 = str::bytes(~"bar");
let k1 = str::to_bytes(~"foo");
let k2 = str::to_bytes(~"bar");

insert(m, k1, ~"foo");
insert(m, k2, ~"bar");
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/uv_ll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,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::bytes(req_str);
let req_str_bytes = str::to_bytes(req_str);
let req_msg_ptr: *u8 = vec::unsafe::to_ptr(req_str_bytes);
log(debug, fmt!("req_msg ptr: %u", req_msg_ptr as uint));
let req_msg = ~[
Expand Down Expand Up @@ -1367,7 +1367,7 @@ mod test {
let server_write_req = write_t();
let server_write_req_ptr = ptr::addr_of(server_write_req);

let resp_str_bytes = str::bytes(server_resp_msg);
let resp_str_bytes = str::to_bytes(server_resp_msg);
let resp_msg_ptr: *u8 = vec::unsafe::to_ptr(resp_str_bytes);
log(debug, fmt!("resp_msg ptr: %u", resp_msg_ptr as uint));
let resp_msg = ~[
Expand Down
Loading

0 comments on commit 9f59131

Please sign in to comment.