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).
This commit is contained in:
Huon Wilson 2013-06-11 13:10:37 +10:00
parent ba4a4778cc
commit efc71a8bdb
44 changed files with 255 additions and 218 deletions

View file

@ -93,7 +93,7 @@ impl RepeatFasta {
let stdout = self.stdout;
let alu_len = self.alu.len();
let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8);
let alu: &[u8] = str::byte_slice_no_callback(self.alu);
let alu: &[u8] = self.alu.as_bytes_with_null();
copy_memory(buf, alu, alu_len);
copy_memory(vec::mut_slice(buf, alu_len, buf.len()),

View file

@ -81,7 +81,8 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint {
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
match mm.find(&str::to_bytes(key.to_ascii().to_lower().to_str_ascii())) {
let key = key.to_ascii().to_lower().to_str_ascii();
match mm.find_equiv(&key.as_bytes()) {
option::None => { return 0u; }
option::Some(&num) => { return num; }
}
@ -208,10 +209,10 @@ fn main() {
// process the sequence for k-mers
(_, true) => {
let line_bytes = str::to_bytes(line);
let line_bytes = line.as_bytes();
for sizes.eachi |ii, _sz| {
let mut lb = copy line_bytes;
let mut lb = line_bytes.to_owned();
to_child[ii].send(lb);
}
}

View file

@ -218,8 +218,7 @@ fn read_stdin() -> ~[u8] {
fstat(fileno(stdin), &mut st);
let mut buf = vec::from_elem(st.st_size as uint, 0);
let header = str::byte_slice_no_callback(">THREE");
let header = vec::slice(header, 0, 6);
let header = ">THREE".as_bytes();
{
let mut window: &mut [u8] = buf;

View file

@ -111,8 +111,7 @@ fn main() {
if opts.stress {
stress(2);
} else {
let max = uint::parse_bytes(str::to_bytes(args[1]),
10u).get() as int;
let max = uint::parse_bytes(args[1].as_bytes(), 10u).get() as int;
let num_trials = 10;

View file

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

View file

@ -48,7 +48,7 @@ mod map_reduce {
}
let (pp, cc) = stream();
error!("sending find_reducer");
ctrl.send(find_reducer(str::to_bytes(key), cc));
ctrl.send(find_reducer(key.as_bytes().to_owned(), cc));
error!("receiving");
let c = pp.recv();
error!(c);

View file

@ -15,6 +15,6 @@ use std::str;
pub fn main() {
let mut m = HashMap::new();
m.insert(str::to_bytes(~"foo"), str::to_bytes(~"bar"));
m.insert("foo".as_bytes().to_owned(), "bar".as_bytes().to_owned());
error!(m);
}

View file

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