Rename str::bytes to str::to_bytes

Closes #3245
This commit is contained in:
Tim Chevalier 2012-08-23 15:44:57 -07:00
parent 0698fc6650
commit 9f591319dd
24 changed files with 87 additions and 86 deletions

View File

@ -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));
@ -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));
@ -2856,7 +2856,7 @@ 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));
@ -2864,8 +2864,8 @@ 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

View File

@ -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 {
@ -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);
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(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(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(str::bytes(~"Z"), 35u) == none;
assert parse_buf(str::bytes(~"-9"), 2u) == none;
assert parse_buf(to_bytes(~"Z"), 35u) == none;
assert parse_buf(to_bytes(~"-9"), 2u) == none;
}
#[test]

View File

@ -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 {

View File

@ -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)

View File

@ -38,7 +38,7 @@ export
trim,
// Transforming strings
bytes,
to_bytes,
byte_slice,
chars,
substr,
@ -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);
@ -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);

View File

@ -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) }
}

View File

@ -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> {
@ -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);
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(str::bytes(~"Z"), 10u) == none;
assert parse_buf(str::bytes(~"_"), 2u) == none;
assert parse_buf(to_bytes(~"Z"), 10u) == none;
assert parse_buf(to_bytes(~"_"), 2u) == none;
}
#[test]

View File

@ -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()
}
}
@ -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()
}
}
@ -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");
}
}

View File

@ -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));
}
}

View File

@ -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() {

View File

@ -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);
}
@ -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() {

View File

@ -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"]))

View File

@ -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);

View File

@ -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");

View File

@ -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 = ~[
@ -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 = ~[

View File

@ -18,6 +18,7 @@ import syntax::ast_map;
import syntax::attr;
import std::serialization::serializer;
import std::ebml::serializer;
import str::to_bytes;
import syntax::ast;
import syntax::diagnostic::span_handler;
@ -152,7 +153,7 @@ fn encode_type_param_bounds(ebml_w: ebml::writer, ecx: @encode_ctxt,
fn encode_variant_id(ebml_w: ebml::writer, vid: def_id) {
ebml_w.start_tag(tag_items_data_item_variant);
ebml_w.writer.write(str::bytes(def_to_str(vid)));
ebml_w.writer.write(str::to_bytes(def_to_str(vid)));
ebml_w.end_tag();
}
@ -181,25 +182,25 @@ fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml::writer, id: node_id) {
fmt!("encode_symbol: id not found %d", id));
}
};
ebml_w.writer.write(str::bytes(sym));
ebml_w.writer.write(str::to_bytes(sym));
ebml_w.end_tag();
}
fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml::writer, id: node_id) {
ebml_w.start_tag(tag_items_data_item_symbol);
ebml_w.writer.write(str::bytes(ecx.discrim_symbols.get(id)));
ebml_w.writer.write(str::to_bytes(ecx.discrim_symbols.get(id)));
ebml_w.end_tag();
}
fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: ebml::writer, disr_val: int) {
ebml_w.start_tag(tag_disr_val);
ebml_w.writer.write(str::bytes(int::to_str(disr_val,10u)));
ebml_w.writer.write(str::to_bytes(int::to_str(disr_val,10u)));
ebml_w.end_tag();
}
fn encode_parent_item(ebml_w: ebml::writer, id: def_id) {
ebml_w.start_tag(tag_items_data_parent_item);
ebml_w.writer.write(str::bytes(def_to_str(id)));
ebml_w.writer.write(str::to_bytes(def_to_str(id)));
ebml_w.end_tag();
}
@ -656,7 +657,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
/* Write the info that's needed when viewing this class
as an impl (just the method def_id and self type) */
ebml_w.start_tag(tag_item_impl_method);
ebml_w.writer.write(str::bytes(def_to_str(local_def(m.id))));
ebml_w.writer.write(to_bytes(def_to_str(local_def(m.id))));
ebml_w.end_tag();
}
}
@ -693,7 +694,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
encode_attributes(ebml_w, item.attrs);
for methods.each |m| {
ebml_w.start_tag(tag_item_impl_method);
ebml_w.writer.write(str::bytes(def_to_str(local_def(m.id))));
ebml_w.writer.write(str::to_bytes(def_to_str(local_def(m.id))));
ebml_w.end_tag();
}
if traits.len() > 1 {
@ -900,7 +901,7 @@ fn encode_meta_item(ebml_w: ebml::writer, mi: meta_item) {
meta_word(name) => {
ebml_w.start_tag(tag_meta_item_word);
ebml_w.start_tag(tag_meta_item_name);
ebml_w.writer.write(str::bytes(name));
ebml_w.writer.write(str::to_bytes(name));
ebml_w.end_tag();
ebml_w.end_tag();
}
@ -909,10 +910,10 @@ fn encode_meta_item(ebml_w: ebml::writer, mi: meta_item) {
lit_str(value) => {
ebml_w.start_tag(tag_meta_item_name_value);
ebml_w.start_tag(tag_meta_item_name);
ebml_w.writer.write(str::bytes(name));
ebml_w.writer.write(str::to_bytes(name));
ebml_w.end_tag();
ebml_w.start_tag(tag_meta_item_value);
ebml_w.writer.write(str::bytes(*value));
ebml_w.writer.write(str::to_bytes(*value));
ebml_w.end_tag();
ebml_w.end_tag();
}
@ -922,7 +923,7 @@ fn encode_meta_item(ebml_w: ebml::writer, mi: meta_item) {
meta_list(name, items) => {
ebml_w.start_tag(tag_meta_item_list);
ebml_w.start_tag(tag_meta_item_name);
ebml_w.writer.write(str::bytes(name));
ebml_w.writer.write(str::to_bytes(name));
ebml_w.end_tag();
for items.each |inner_item| {
encode_meta_item(ebml_w, *inner_item);
@ -1044,20 +1045,20 @@ fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml::writer,
dep: decoder::crate_dep) {
ebml_w.start_tag(tag_crate_dep);
ebml_w.start_tag(tag_crate_dep_name);
ebml_w.writer.write(str::bytes(ecx.tcx.sess.str_of(dep.name)));
ebml_w.writer.write(str::to_bytes(ecx.tcx.sess.str_of(dep.name)));
ebml_w.end_tag();
ebml_w.start_tag(tag_crate_dep_vers);
ebml_w.writer.write(str::bytes(dep.vers));
ebml_w.writer.write(str::to_bytes(dep.vers));
ebml_w.end_tag();
ebml_w.start_tag(tag_crate_dep_hash);
ebml_w.writer.write(str::bytes(dep.hash));
ebml_w.writer.write(str::to_bytes(dep.hash));
ebml_w.end_tag();
ebml_w.end_tag();
}
fn encode_hash(ebml_w: ebml::writer, hash: ~str) {
ebml_w.start_tag(tag_crate_hash);
ebml_w.writer.write(str::bytes(hash));
ebml_w.writer.write(str::to_bytes(hash));
ebml_w.end_tag();
}

View File

@ -391,7 +391,7 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
let variant_shape = shape_of_variant(ccx, v);
add_substr(data, variant_shape);
let zname = str::bytes(ccx.sess.str_of(v.name)) + ~[0u8];
let zname = str::to_bytes(ccx.sess.str_of(v.name)) + ~[0u8];
add_substr(data, zname);
}
enum_variants += ~[variants];

View File

@ -57,7 +57,7 @@ fn sort_and_fmt(mm: hashmap<~[u8], uint>, total: uint) -> ~str {
// given a map, search for the frequency of a pattern
fn find(mm: hashmap<~[u8], uint>, key: ~str) -> uint {
match mm.find(str::bytes(str::to_lower(key))) {
match mm.find(str::to_bytes(str::to_lower(key))) {
option::none => { return 0u; }
option::some(num) => { return num; }
}
@ -185,7 +185,7 @@ fn main(args: ~[~str]) {
// process the sequence for k-mers
(_, true) => {
let line_bytes = str::bytes(line);
let line_bytes = str::to_bytes(line);
for sizes.eachi |ii, _sz| {
let mut lb = line_bytes;

View File

@ -55,7 +55,7 @@ fn sort_and_fmt(mm: hashmap<~[u8], uint>, total: uint) -> ~str {
// given a map, search for the frequency of a pattern
fn find(mm: hashmap<~[u8], uint>, key: ~str) -> uint {
match mm.find(str::bytes(str::to_lower(key))) {
match mm.find(str::to_bytes(str::to_lower(key))) {
option::none => { return 0u; }
option::some(num) => { return num; }
}
@ -172,7 +172,7 @@ fn main(args: ~[~str]) {
// process the sequence for k-mers
(_, true) => {
let line_bytes = str::bytes(line);
let line_bytes = str::to_bytes(line);
for sizes.eachi |ii, _sz| {
let mut lb = line_bytes;

View File

@ -93,7 +93,7 @@ fn main(args: ~[~str]) {
if opts.stress {
stress(2);
} else {
let max = option::get(uint::parse_buf(str::bytes(args[1]),
let max = option::get(uint::parse_buf(str::to_bytes(args[1]),
10u)) as int;
let num_trials = 10;

View File

@ -12,7 +12,7 @@ extern mod libc {
fn strlen(str: ~str) -> uint unsafe {
// C string is terminated with a zero
let bytes = str::bytes(str) + ~[0u8];
let bytes = str::to_bytes(str) + ~[0u8];
return libc::my_strlen(vec::unsafe::to_ptr(bytes));
}

View File

@ -51,7 +51,7 @@ mod map_reduce {
none => {
let p = port();
error!("sending find_reducer");
send(ctrl, find_reducer(str::bytes(key), chan(p)));
send(ctrl, find_reducer(str::to_bytes(key), chan(p)));
error!("receiving");
c = recv(p);
log(error, c);

View File

@ -4,6 +4,6 @@ import std::map::hashmap;
fn main() {
let m = map::bytes_hash();
m.insert(str::bytes(~"foo"), str::bytes(~"bar"));
m.insert(str::to_bytes(~"foo"), str::to_bytes(~"bar"));
log(error, m);
}

View File

@ -14,7 +14,7 @@ fn main() {
assert (str::char_at(s, 0u) == 'e');
assert (str::char_at(s, 1u) == 'é');
assert (str::is_utf8(str::bytes(s)));
assert (str::is_utf8(str::to_bytes(s)));
assert (!str::is_utf8(~[0x80_u8]));
assert (!str::is_utf8(~[0xc0_u8]));
assert (!str::is_utf8(~[0xc0_u8, 0x10_u8]));