Skip to content

Commit 9f59131

Browse files
committed
Rename str::bytes to str::to_bytes
Closes #3245
1 parent 0698fc6 commit 9f59131

24 files changed

+90
-89
lines changed

doc/tutorial.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ fn as_hex(data: ~[u8]) -> ~str {
27132713
}
27142714
27152715
fn sha1(data: ~str) -> ~str unsafe {
2716-
let bytes = str::bytes(data);
2716+
let bytes = str::to_bytes(data);
27172717
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
27182718
vec::len(bytes) as c_uint, ptr::null());
27192719
return as_hex(vec::unsafe::from_buf(hash, 20u));
@@ -2813,7 +2813,7 @@ The `sha1` function is the most obscure part of the program.
28132813
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
28142814
fn sha1(data: ~str) -> ~str {
28152815
unsafe {
2816-
let bytes = str::bytes(data);
2816+
let bytes = str::to_bytes(data);
28172817
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
28182818
vec::len(bytes), ptr::null());
28192819
return as_hex(vec::unsafe::from_buf(hash, 20u));
@@ -2856,16 +2856,16 @@ Let's look at our `sha1` function again.
28562856
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
28572857
# fn x(data: ~str) -> ~str {
28582858
# unsafe {
2859-
let bytes = str::bytes(data);
2859+
let bytes = str::to_bytes(data);
28602860
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
28612861
vec::len(bytes), ptr::null());
28622862
return as_hex(vec::unsafe::from_buf(hash, 20u));
28632863
# }
28642864
# }
28652865
~~~~
28662866

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

28712871
This pointer will become invalid as soon as the vector it points into

src/libcore/int-template.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn parse_buf(buf: ~[u8], radix: uint) -> option<T> {
156156
}
157157

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

161161
/// Convert to a string in a given base
162162
fn to_str(n: T, radix: uint) -> ~str {
@@ -202,27 +202,27 @@ fn test_from_str() {
202202
#[test]
203203
#[ignore]
204204
fn test_parse_buf() {
205-
import str::bytes;
206-
assert parse_buf(bytes(~"123"), 10u) == some(123 as T);
207-
assert parse_buf(bytes(~"1001"), 2u) == some(9 as T);
208-
assert parse_buf(bytes(~"123"), 8u) == some(83 as T);
209-
assert parse_buf(bytes(~"123"), 16u) == some(291 as T);
210-
assert parse_buf(bytes(~"ffff"), 16u) == some(65535 as T);
211-
assert parse_buf(bytes(~"FFFF"), 16u) == some(65535 as T);
212-
assert parse_buf(bytes(~"z"), 36u) == some(35 as T);
213-
assert parse_buf(bytes(~"Z"), 36u) == some(35 as T);
214-
215-
assert parse_buf(bytes(~"-123"), 10u) == some(-123 as T);
216-
assert parse_buf(bytes(~"-1001"), 2u) == some(-9 as T);
217-
assert parse_buf(bytes(~"-123"), 8u) == some(-83 as T);
218-
assert parse_buf(bytes(~"-123"), 16u) == some(-291 as T);
219-
assert parse_buf(bytes(~"-ffff"), 16u) == some(-65535 as T);
220-
assert parse_buf(bytes(~"-FFFF"), 16u) == some(-65535 as T);
221-
assert parse_buf(bytes(~"-z"), 36u) == some(-35 as T);
222-
assert parse_buf(bytes(~"-Z"), 36u) == some(-35 as T);
223-
224-
assert parse_buf(str::bytes(~"Z"), 35u) == none;
225-
assert parse_buf(str::bytes(~"-9"), 2u) == none;
205+
import str::to_bytes;
206+
assert parse_buf(to_bytes(~"123"), 10u) == some(123 as T);
207+
assert parse_buf(to_bytes(~"1001"), 2u) == some(9 as T);
208+
assert parse_buf(to_bytes(~"123"), 8u) == some(83 as T);
209+
assert parse_buf(to_bytes(~"123"), 16u) == some(291 as T);
210+
assert parse_buf(to_bytes(~"ffff"), 16u) == some(65535 as T);
211+
assert parse_buf(to_bytes(~"FFFF"), 16u) == some(65535 as T);
212+
assert parse_buf(to_bytes(~"z"), 36u) == some(35 as T);
213+
assert parse_buf(to_bytes(~"Z"), 36u) == some(35 as T);
214+
215+
assert parse_buf(to_bytes(~"-123"), 10u) == some(-123 as T);
216+
assert parse_buf(to_bytes(~"-1001"), 2u) == some(-9 as T);
217+
assert parse_buf(to_bytes(~"-123"), 8u) == some(-83 as T);
218+
assert parse_buf(to_bytes(~"-123"), 16u) == some(-291 as T);
219+
assert parse_buf(to_bytes(~"-ffff"), 16u) == some(-65535 as T);
220+
assert parse_buf(to_bytes(~"-FFFF"), 16u) == some(-65535 as T);
221+
assert parse_buf(to_bytes(~"-z"), 36u) == some(-35 as T);
222+
assert parse_buf(to_bytes(~"-Z"), 36u) == some(-35 as T);
223+
224+
assert parse_buf(to_bytes(~"Z"), 35u) == none;
225+
assert parse_buf(to_bytes(~"-9"), 2u) == none;
226226
}
227227

228228
#[test]

src/libcore/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ fn with_bytes_reader_between<t>(bytes: ~[u8], start: uint, end: uint,
319319
}
320320

321321
fn str_reader(s: ~str) -> Reader {
322-
bytes_reader(str::bytes(s))
322+
bytes_reader(str::to_bytes(s))
323323
}
324324

325325
fn with_str_reader<T>(s: ~str, f: fn(Reader) -> T) -> T {

src/libcore/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ mod tests {
10281028
};
10291029
assert (ostream as uint != 0u);
10301030
let s = ~"hello";
1031-
let mut buf = vec::to_mut(str::bytes(s) + ~[0 as u8]);
1031+
let mut buf = vec::to_mut(str::to_bytes(s) + ~[0 as u8]);
10321032
do vec::as_mut_buf(buf) |b, _len| {
10331033
assert (libc::fwrite(b as *c_void, 1u as size_t,
10341034
(str::len(s) + 1u) as size_t, ostream)

src/libcore/str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export
3838
trim,
3939

4040
// Transforming strings
41-
bytes,
41+
to_bytes,
4242
byte_slice,
4343
chars,
4444
substr,
@@ -372,7 +372,7 @@ Section: Transforming strings
372372
*
373373
* The result vector is not null-terminated.
374374
*/
375-
pure fn bytes(s: &str) -> ~[u8] {
375+
pure fn to_bytes(s: &str) -> ~[u8] {
376376
unsafe {
377377
let mut s_copy = from_slice(s);
378378
let mut v: ~[u8] = ::unsafe::transmute(s_copy);
@@ -2727,7 +2727,7 @@ mod tests {
27272727
fn vec_str_conversions() {
27282728
let s1: ~str = ~"All mimsy were the borogoves";
27292729

2730-
let v: ~[u8] = bytes(s1);
2730+
let v: ~[u8] = to_bytes(s1);
27312731
let s2: ~str = from_bytes(v);
27322732
let mut i: uint = 0u;
27332733
let n1: uint = len(s1);

src/libcore/to_bytes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ impl @~[u8]: ToBytes {
1515
}
1616

1717
impl ~str: ToBytes {
18-
fn to_bytes() -> ~[u8] { str::bytes(self) }
18+
fn to_bytes() -> ~[u8] { str::to_bytes(self) }
1919
}
2020

2121
impl @(~str): ToBytes {
22-
fn to_bytes() -> ~[u8] { str::bytes(*self) }
22+
fn to_bytes() -> ~[u8] { str::to_bytes(*self) }
2323
}

src/libcore/uint-template.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn parse_buf(buf: &[const u8], radix: uint) -> option<T> {
138138
}
139139

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

143143
/// Parse a string as an unsigned integer.
144144
fn from_str_radix(buf: ~str, radix: u64) -> option<u64> {
@@ -267,16 +267,16 @@ fn test_from_str() {
267267
#[test]
268268
#[ignore]
269269
fn test_parse_buf() {
270-
import str::bytes;
271-
assert parse_buf(bytes(~"123"), 10u) == some(123u as T);
272-
assert parse_buf(bytes(~"1001"), 2u) == some(9u as T);
273-
assert parse_buf(bytes(~"123"), 8u) == some(83u as T);
274-
assert parse_buf(bytes(~"123"), 16u) == some(291u as T);
275-
assert parse_buf(bytes(~"ffff"), 16u) == some(65535u as T);
276-
assert parse_buf(bytes(~"z"), 36u) == some(35u as T);
277-
278-
assert parse_buf(str::bytes(~"Z"), 10u) == none;
279-
assert parse_buf(str::bytes(~"_"), 2u) == none;
270+
import str::to_bytes;
271+
assert parse_buf(to_bytes(~"123"), 10u) == some(123u as T);
272+
assert parse_buf(to_bytes(~"1001"), 2u) == some(9u as T);
273+
assert parse_buf(to_bytes(~"123"), 8u) == some(83u as T);
274+
assert parse_buf(to_bytes(~"123"), 16u) == some(291u as T);
275+
assert parse_buf(to_bytes(~"ffff"), 16u) == some(65535u as T);
276+
assert parse_buf(to_bytes(~"z"), 36u) == some(35u as T);
277+
278+
assert parse_buf(to_bytes(~"Z"), 10u) == none;
279+
assert parse_buf(to_bytes(~"_"), 2u) == none;
280280
}
281281

282282
#[test]

src/libstd/base64.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl ~[u8]: to_base64 {
5757

5858
impl ~str: to_base64 {
5959
fn to_base64() -> ~str {
60-
str::bytes(self).to_base64()
60+
str::to_bytes(self).to_base64()
6161
}
6262
}
6363

@@ -129,7 +129,7 @@ impl ~[u8]: from_base64 {
129129

130130
impl ~str: from_base64 {
131131
fn from_base64() -> ~[u8] {
132-
str::bytes(self).from_base64()
132+
str::to_bytes(self).from_base64()
133133
}
134134
}
135135

@@ -148,12 +148,12 @@ mod tests {
148148

149149
#[test]
150150
fn test_from_base64() {
151-
assert (~"").from_base64() == str::bytes(~"");
152-
assert (~"Zg==").from_base64() == str::bytes(~"f");
153-
assert (~"Zm8=").from_base64() == str::bytes(~"fo");
154-
assert (~"Zm9v").from_base64() == str::bytes(~"foo");
155-
assert (~"Zm9vYg==").from_base64() == str::bytes(~"foob");
156-
assert (~"Zm9vYmE=").from_base64() == str::bytes(~"fooba");
157-
assert (~"Zm9vYmFy").from_base64() == str::bytes(~"foobar");
151+
assert (~"").from_base64() == str::to_bytes(~"");
152+
assert (~"Zg==").from_base64() == str::to_bytes(~"f");
153+
assert (~"Zm8=").from_base64() == str::to_bytes(~"fo");
154+
assert (~"Zm9v").from_base64() == str::to_bytes(~"foo");
155+
assert (~"Zm9vYg==").from_base64() == str::to_bytes(~"foob");
156+
assert (~"Zm9vYmE=").from_base64() == str::to_bytes(~"fooba");
157+
assert (~"Zm9vYmFy").from_base64() == str::to_bytes(~"foobar");
158158
}
159159
}

src/libstd/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl writer {
295295

296296
fn wr_str(s: ~str) {
297297
debug!("Write str: %?", s);
298-
self.writer.write(str::bytes(s));
298+
self.writer.write(str::to_bytes(s));
299299
}
300300
}
301301

src/libstd/md4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn md4_str(msg: ~[u8]) -> ~str {
100100
result
101101
}
102102

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

105105
#[test]
106106
fn test_md4() {

src/libstd/net_tcp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ mod test {
15231523
server_ch.send(
15241524
str::from_bytes(data));
15251525
log(debug, ~"SERVER: before write");
1526-
tcp_write_single(sock, str::bytes(resp));
1526+
tcp_write_single(sock, str::to_bytes(resp));
15271527
log(debug, ~"SERVER: after write.. die");
15281528
core::comm::send(kill_ch, none);
15291529
}
@@ -1599,7 +1599,7 @@ mod test {
15991599
}
16001600
else {
16011601
let sock = result::unwrap(connect_result);
1602-
let resp_bytes = str::bytes(resp);
1602+
let resp_bytes = str::to_bytes(resp);
16031603
tcp_write_single(sock, resp_bytes);
16041604
let read_result = sock.read(0u);
16051605
if read_result.is_err() {

src/libstd/net_url.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ mod tests {
10291029

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

1032-
let s = str::bytes(~"a=1&foo+bar=abc&foo+bar=12+%3D+34");
1032+
let s = str::to_bytes(~"a=1&foo+bar=abc&foo+bar=12+%3D+34");
10331033
assert decode_form_urlencoded(s) == hash_from_strs(~[
10341034
(~"a", @dvec::from_elem(@~"1")),
10351035
(~"foo bar", @dvec::from_vec(~[mut @~"abc", @~"12 = 34"]))

src/libstd/sha1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn sha1() -> sha1 {
228228
self.computed = false;
229229
}
230230
fn input(msg: ~[u8]) { add_input(self, msg); }
231-
fn input_str(msg: ~str) { add_input(self, str::bytes(msg)); }
231+
fn input_str(msg: ~str) { add_input(self, str::to_bytes(msg)); }
232232
fn result() -> ~[u8] { return mk_result(self); }
233233
fn result_str() -> ~str {
234234
let r = mk_result(self);

src/libstd/treemap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ mod tests {
132132
fn u8_map() {
133133
let m = treemap();
134134

135-
let k1 = str::bytes(~"foo");
136-
let k2 = str::bytes(~"bar");
135+
let k1 = str::to_bytes(~"foo");
136+
let k2 = str::to_bytes(~"bar");
137137

138138
insert(m, k1, ~"foo");
139139
insert(m, k2, ~"bar");

src/libstd/uv_ll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ mod test {
11191119
// In C, this would be a malloc'd or stack-allocated
11201120
// struct that we'd cast to a void* and store as the
11211121
// data field in our uv_connect_t struct
1122-
let req_str_bytes = str::bytes(req_str);
1122+
let req_str_bytes = str::to_bytes(req_str);
11231123
let req_msg_ptr: *u8 = vec::unsafe::to_ptr(req_str_bytes);
11241124
log(debug, fmt!("req_msg ptr: %u", req_msg_ptr as uint));
11251125
let req_msg = ~[
@@ -1367,7 +1367,7 @@ mod test {
13671367
let server_write_req = write_t();
13681368
let server_write_req_ptr = ptr::addr_of(server_write_req);
13691369

1370-
let resp_str_bytes = str::bytes(server_resp_msg);
1370+
let resp_str_bytes = str::to_bytes(server_resp_msg);
13711371
let resp_msg_ptr: *u8 = vec::unsafe::to_ptr(resp_str_bytes);
13721372
log(debug, fmt!("resp_msg ptr: %u", resp_msg_ptr as uint));
13731373
let resp_msg = ~[

0 commit comments

Comments
 (0)