From ad799f200cec632dc57edb0bd39e35c7f69204c0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 8 Jun 2013 09:41:05 -0700 Subject: [PATCH 1/6] std: allow vec::bytes::memcmp to work on slices --- src/libstd/vec.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 0af69815cf23a..595e484f6409d 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -2413,13 +2413,13 @@ pub mod bytes { use vec; /// Bytewise string comparison - pub fn memcmp(a: &~[u8], b: &~[u8]) -> int { + pub fn memcmp(a: &[u8], b: &[u8]) -> int { let a_len = a.len(); let b_len = b.len(); let n = uint::min(a_len, b_len) as libc::size_t; let r = unsafe { - libc::memcmp(raw::to_ptr(*a) as *libc::c_void, - raw::to_ptr(*b) as *libc::c_void, n) as int + libc::memcmp(raw::to_ptr(a) as *libc::c_void, + raw::to_ptr(b) as *libc::c_void, n) as int }; if r != 0 { r } else { @@ -2434,22 +2434,22 @@ pub mod bytes { } /// Bytewise less than or equal - pub fn lt(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) < 0 } + pub fn lt(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) < 0 } /// Bytewise less than or equal - pub fn le(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) <= 0 } + pub fn le(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) <= 0 } /// Bytewise equality - pub fn eq(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) == 0 } + pub fn eq(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) == 0 } /// Bytewise inequality - pub fn ne(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) != 0 } + pub fn ne(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) != 0 } /// Bytewise greater than or equal - pub fn ge(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) >= 0 } + pub fn ge(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) >= 0 } /// Bytewise greater than - pub fn gt(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) > 0 } + pub fn gt(a: &[u8], b: &[u8]) -> bool { memcmp(a, b) > 0 } /** * Copies data from one vector to another. From 1f4eddbd1c9dc03d5621a7913d59688287bce681 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 8 Jun 2013 11:29:16 -0700 Subject: [PATCH 2/6] std: group the option tests inside a module --- src/libstd/option.rs | 151 ++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 72 deletions(-) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 7dc6b7fe4b167..7075e849b29af 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -377,90 +377,97 @@ impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> { } } -#[test] -fn test_unwrap_ptr() { - unsafe { - let x = ~0; - let addr_x: *int = ::cast::transmute(&*x); +#[cfg(test)] +mod tests { + use super::*; + use std::str; + use std::util; + + #[test] + fn test_unwrap_ptr() { + unsafe { + let x = ~0; + let addr_x: *int = ::cast::transmute(&*x); + let opt = Some(x); + let y = opt.unwrap(); + let addr_y: *int = ::cast::transmute(&*y); + assert_eq!(addr_x, addr_y); + } + } + + #[test] + fn test_unwrap_str() { + let x = ~"test"; + let addr_x = str::as_buf(x, |buf, _len| buf); let opt = Some(x); let y = opt.unwrap(); - let addr_y: *int = ::cast::transmute(&*y); + let addr_y = str::as_buf(y, |buf, _len| buf); assert_eq!(addr_x, addr_y); } -} - -#[test] -fn test_unwrap_str() { - let x = ~"test"; - let addr_x = str::as_buf(x, |buf, _len| buf); - let opt = Some(x); - let y = opt.unwrap(); - let addr_y = str::as_buf(y, |buf, _len| buf); - assert_eq!(addr_x, addr_y); -} -#[test] -fn test_unwrap_resource() { - struct R { - i: @mut int, - } - - #[unsafe_destructor] - impl ::ops::Drop for R { - fn finalize(&self) { *(self.i) += 1; } - } + #[test] + fn test_unwrap_resource() { + struct R { + i: @mut int, + } - fn R(i: @mut int) -> R { - R { - i: i + #[unsafe_destructor] + impl ::ops::Drop for R { + fn finalize(&self) { *(self.i) += 1; } } - } - let i = @mut 0; - { - let x = R(i); - let opt = Some(x); - let _y = opt.unwrap(); - } - assert_eq!(*i, 1); -} + fn R(i: @mut int) -> R { + R { + i: i + } + } -#[test] -fn test_option_dance() { - let x = Some(()); - let mut y = Some(5); - let mut y2 = 0; - for x.iter().advance |_x| { - y2 = y.swap_unwrap(); + let i = @mut 0; + { + let x = R(i); + let opt = Some(x); + let _y = opt.unwrap(); + } + assert_eq!(*i, 1); } - assert_eq!(y2, 5); - assert!(y.is_none()); -} -#[test] #[should_fail] #[ignore(cfg(windows))] -fn test_option_too_much_dance() { - let mut y = Some(util::NonCopyable::new()); - let _y2 = y.swap_unwrap(); - let _y3 = y.swap_unwrap(); -} -#[test] -fn test_option_while_some() { - let mut i = 0; - do Some(10).while_some |j| { - i += 1; - if (j > 0) { - Some(j-1) - } else { - None + #[test] + fn test_option_dance() { + let x = Some(()); + let mut y = Some(5); + let mut y2 = 0; + for x.iter().advance |_x| { + y2 = y.swap_unwrap(); + } + assert_eq!(y2, 5); + assert!(y.is_none()); + } + #[test] #[should_fail] #[ignore(cfg(windows))] + fn test_option_too_much_dance() { + let mut y = Some(util::NonCopyable::new()); + let _y2 = y.swap_unwrap(); + let _y3 = y.swap_unwrap(); + } + + #[test] + fn test_option_while_some() { + let mut i = 0; + do Some(10).while_some |j| { + i += 1; + if (j > 0) { + Some(j-1) + } else { + None + } } + assert_eq!(i, 11); } - assert_eq!(i, 11); -} -#[test] -fn test_get_or_zero() { - let some_stuff = Some(42); - assert_eq!(some_stuff.get_or_zero(), 42); - let no_stuff: Option = None; - assert_eq!(no_stuff.get_or_zero(), 0); + #[test] + fn test_get_or_zero() { + let some_stuff = Some(42); + assert_eq!(some_stuff.get_or_zero(), 42); + let no_stuff: Option = None; + assert_eq!(no_stuff.get_or_zero(), 0); + } } From 48312a5dfc3700b6bd1a7d2f52feb4cd290909e9 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 12 Jun 2013 08:45:26 -0700 Subject: [PATCH 3/6] minor cleanup --- src/libextra/rl.rs | 3 +-- src/libextra/terminfo/parser/compiled.rs | 2 +- src/libextra/uv_ll.rs | 2 +- src/librustc/metadata/tydecode.rs | 6 ++++-- src/librustpkg/package_source.rs | 4 ++-- src/librustpkg/version.rs | 2 +- src/libstd/os.rs | 8 +++----- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/libextra/rl.rs b/src/libextra/rl.rs index 040adcc443d92..c2aee887c5948 100644 --- a/src/libextra/rl.rs +++ b/src/libextra/rl.rs @@ -77,8 +77,7 @@ pub unsafe fn complete(cb: CompletionCb) { extern fn callback(line: *c_char, completions: *()) { unsafe { - let cb = *local_data::local_data_get(complete_key) - .get(); + let cb = *local_data::local_data_get(complete_key).get(); do cb(str::raw::from_c_str(line)) |suggestion| { do str::as_c_str(suggestion) |buf| { diff --git a/src/libextra/terminfo/parser/compiled.rs b/src/libextra/terminfo/parser/compiled.rs index 66649c62fcaf3..6d1617a38cf49 100644 --- a/src/libextra/terminfo/parser/compiled.rs +++ b/src/libextra/terminfo/parser/compiled.rs @@ -213,7 +213,7 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> { } let names_str = str::from_bytes(file.read_bytes(names_bytes as uint - 1)); // don't read NUL - let term_names: ~[~str] = names_str.split_iter('|').transform(|s| s.to_owned()).collect(); + let term_names = names_str.split_iter('|').transform(|s| s.to_owned()).collect(); file.read_byte(); // consume NUL diff --git a/src/libextra/uv_ll.rs b/src/libextra/uv_ll.rs index 744f4555d5cbd..b4d5573c7ce43 100644 --- a/src/libextra/uv_ll.rs +++ b/src/libextra/uv_ll.rs @@ -1046,7 +1046,7 @@ pub unsafe fn ip6_addr(ip: &str, port: int) -> sockaddr_in6 { pub unsafe fn ip4_name(src: &sockaddr_in) -> ~str { // ipv4 addr max size: 15 + 1 trailing null byte let dst: ~[u8] = ~[0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8, - 0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8]; + 0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8]; do vec::as_imm_buf(dst) |dst_buf, size| { rust_uv_ip4_name(to_unsafe_ptr(src), dst_buf, size as libc::size_t); diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index cf2a92b291f28..7b60e3a5d193d 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -89,7 +89,7 @@ fn scan(st: &mut PState, is_last: &fn(char) -> bool, } let end_pos = st.pos; st.pos += 1; - return op(st.data.slice(start_pos, end_pos)); + op(st.data.slice(start_pos, end_pos)) } pub fn parse_ident(st: &mut PState, last: char) -> ast::ident { @@ -409,7 +409,9 @@ fn parse_mt(st: &mut PState, conv: conv_did) -> ty::mt { fn parse_def(st: &mut PState, source: DefIdSource, conv: conv_did) -> ast::def_id { - return conv(source, scan(st, |c| { c == '|' }, parse_def_id)); + do scan(st, |c| { c == '|' }) |v| { + conv(source, parse_def_id(v)) + } } fn parse_uint(st: &mut PState) -> uint { diff --git a/src/librustpkg/package_source.rs b/src/librustpkg/package_source.rs index 01cc48fc037b9..9fc7c9ec11dda 100644 --- a/src/librustpkg/package_source.rs +++ b/src/librustpkg/package_source.rs @@ -185,8 +185,8 @@ impl PkgSrc { && self.tests.is_empty() && self.benchs.is_empty() { note("Couldn't infer any crates to build.\n\ - Try naming a crate `main.rs`, `lib.rs`, \ - `test.rs`, or `bench.rs`."); + Try naming a crate `main.rs`, `lib.rs`, \ + `test.rs`, or `bench.rs`."); cond.raise(copy self.id); } diff --git a/src/librustpkg/version.rs b/src/librustpkg/version.rs index 7431b5e4c011c..41338f88735e9 100644 --- a/src/librustpkg/version.rs +++ b/src/librustpkg/version.rs @@ -208,4 +208,4 @@ fn test_split_version() { let s = "a#1.2"; assert!(split_version(s) == Some((s.slice(0, 1), ExactRevision(~"1.2")))); assert!(split_version("a#a#3.4") == None); -} \ No newline at end of file +} diff --git a/src/libstd/os.rs b/src/libstd/os.rs index e48dc723c47a4..7adee1915213b 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -712,7 +712,7 @@ pub fn list_dir(p: &Path) -> ~[~str] { debug!("os::list_dir -- BEFORE OPENDIR"); let dir_ptr = opendir(input_ptr); if (dir_ptr as uint != 0) { - debug!("os::list_dir -- opendir() SUCCESS"); + debug!("os::list_dir -- opendir() SUCCESS"); let mut entry_ptr = readdir(dir_ptr); while (entry_ptr as uint != 0) { strings.push(str::raw::from_c_str(rust_list_dir_val( @@ -722,11 +722,9 @@ pub fn list_dir(p: &Path) -> ~[~str] { closedir(dir_ptr); } else { - debug!("os::list_dir -- opendir() FAILURE"); + debug!("os::list_dir -- opendir() FAILURE"); } - debug!( - "os::list_dir -- AFTER -- #: %?", - strings.len()); + debug!("os::list_dir -- AFTER -- #: %?", strings.len()); strings } #[cfg(windows)] From 5795bb1b17dd2db525af0d85fc8407eec405e4c4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 15 Jun 2013 15:44:35 -0700 Subject: [PATCH 4/6] std: rename str::from_bytes{,_with_null,_slice} to from_utf8* --- src/compiletest/procsrv.rs | 4 +- src/libextra/base64.rs | 2 +- src/libextra/ebml.rs | 2 +- src/libextra/net_tcp.rs | 10 ++-- src/libextra/terminfo/parser/compiled.rs | 2 +- src/libextra/uv_ll.rs | 4 +- src/librustc/back/link.rs | 4 +- src/librustc/metadata/decoder.rs | 2 +- src/librustc/metadata/tydecode.rs | 4 +- src/librustc/rustc.rc | 4 +- src/librustc/util/ppaux.rs | 4 +- src/librustpkg/tests.rs | 6 +-- src/libstd/io.rs | 8 +-- src/libstd/logging.rs | 2 +- src/libstd/num/strconv.rs | 2 +- src/libstd/rt/io/flate.rs | 2 +- src/libstd/run.rs | 16 +++--- src/libstd/str.rs | 52 +++++++++---------- src/libstd/str/ascii.rs | 2 +- src/libsyntax/parse/comments.rs | 2 +- src/test/bench/shootout-k-nucleotide-pipes.rs | 2 +- src/test/bench/shootout-k-nucleotide.rs | 2 +- src/test/run-pass/core-run-destroy.rs | 2 +- src/test/run-pass/hashmap-memory.rs | 2 +- 24 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/compiletest/procsrv.rs b/src/compiletest/procsrv.rs index 93fe258d167ed..c319069779ae4 100644 --- a/src/compiletest/procsrv.rs +++ b/src/compiletest/procsrv.rs @@ -65,7 +65,7 @@ pub fn run(lib_path: &str, Result { status: output.status, - out: str::from_bytes(output.output), - err: str::from_bytes(output.error) + out: str::from_utf8(output.output), + err: str::from_utf8(output.error) } } diff --git a/src/libextra/base64.rs b/src/libextra/base64.rs index 5bf4dd517a5b2..516e332753910 100644 --- a/src/libextra/base64.rs +++ b/src/libextra/base64.rs @@ -217,7 +217,7 @@ impl<'self> FromBase64 for &'self str { * println(fmt!("%s",hello_str)); * let bytes = hello_str.from_base64(); * println(fmt!("%?",bytes)); - * let result_str = str::from_bytes(bytes); + * let result_str = str::from_utf8(bytes); * println(fmt!("%s",result_str)); * } * ~~~ diff --git a/src/libextra/ebml.rs b/src/libextra/ebml.rs index dd08f23a7a10f..7461a3b826c66 100644 --- a/src/libextra/ebml.rs +++ b/src/libextra/ebml.rs @@ -95,7 +95,7 @@ pub mod reader { } pub fn as_str_slice<'a>(&'a self) -> &'a str { - str::from_bytes_slice(self.data.slice(self.start, self.end)) + str::from_utf8_slice(self.data.slice(self.start, self.end)) } pub fn as_str(&self) -> ~str { diff --git a/src/libextra/net_tcp.rs b/src/libextra/net_tcp.rs index 28b3700399a6e..8a97a9cfa61d6 100644 --- a/src/libextra/net_tcp.rs +++ b/src/libextra/net_tcp.rs @@ -1800,7 +1800,7 @@ mod test { buf_write(sock_buf, expected_req); let buf_reader = sock_buf as @Reader; - let actual_response = str::from_bytes(buf_reader.read_whole_stream()); + let actual_response = str::from_utf8(buf_reader.read_whole_stream()); debug!("Actual response: %s", actual_response); assert!(expected_resp == actual_response); } @@ -1817,7 +1817,7 @@ mod test { let new_bytes = (*r).read_bytes(len); debug!("in buf_read.. new_bytes len: %?", new_bytes.len()); - str::from_bytes(new_bytes) + str::from_utf8(new_bytes) } fn run_tcp_test_server(server_ip: &str, server_port: uint, resp: ~str, @@ -1867,11 +1867,11 @@ mod test { let received_req_bytes = read(&sock, 0u); match received_req_bytes { result::Ok(data) => { - debug!("SERVER: got REQ str::from_bytes.."); + debug!("SERVER: got REQ str::from_utf8.."); debug!("SERVER: REQ data len: %?", data.len()); server_ch.send( - str::from_bytes(data)); + str::from_utf8(data)); debug!("SERVER: before write"); let s = resp_cell2.take(); tcp_write_single(&sock, s.as_bytes().to_owned()); @@ -1954,7 +1954,7 @@ mod test { Ok(~"") } else { - let ret_val = str::from_bytes(read_result.get()); + let ret_val = str::from_utf8(read_result.get()); debug!("CLIENT: after client_ch recv ret: '%s'", ret_val); Ok(ret_val) diff --git a/src/libextra/terminfo/parser/compiled.rs b/src/libextra/terminfo/parser/compiled.rs index 6d1617a38cf49..db308e3695f4f 100644 --- a/src/libextra/terminfo/parser/compiled.rs +++ b/src/libextra/terminfo/parser/compiled.rs @@ -212,7 +212,7 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> { return Err(~"incompatible file: more string offsets than expected"); } - let names_str = str::from_bytes(file.read_bytes(names_bytes as uint - 1)); // don't read NUL + let names_str = str::from_utf8(file.read_bytes(names_bytes as uint - 1)); // don't read NUL let term_names = names_str.split_iter('|').transform(|s| s.to_owned()).collect(); file.read_byte(); // consume NUL diff --git a/src/libextra/uv_ll.rs b/src/libextra/uv_ll.rs index b4d5573c7ce43..9cc1cd35eb506 100644 --- a/src/libextra/uv_ll.rs +++ b/src/libextra/uv_ll.rs @@ -1287,7 +1287,7 @@ mod test { let buf_base = get_base_from_buf(buf); let bytes = vec::from_buf(buf_base, nread as uint); let read_chan = (*client_data).read_chan.clone(); - let msg_from_server = str::from_bytes(bytes); + let msg_from_server = str::from_utf8(bytes); read_chan.send(msg_from_server); close(stream as *libc::c_void, after_close_cb) } @@ -1473,7 +1473,7 @@ mod test { buf_len as uint, nread); let bytes = vec::from_buf(buf_base, nread as uint); - let request_str = str::from_bytes(bytes); + let request_str = str::from_utf8(bytes); let client_data = get_data_for_uv_handle( client_stream_ptr as *libc::c_void) as *tcp_server_data; diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 6aa97e393eab7..c918ec243a264 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -397,7 +397,7 @@ pub mod write { cc_prog, prog.status)); sess.note(fmt!("%s arguments: %s", cc_prog, cc_args.connect(" "))); - sess.note(str::from_bytes(prog.error + prog.output)); + sess.note(str::from_utf8(prog.error + prog.output)); sess.abort_if_errors(); } } @@ -819,7 +819,7 @@ pub fn link_binary(sess: Session, cc_prog, prog.status)); sess.note(fmt!("%s arguments: %s", cc_prog, cc_args.connect(" "))); - sess.note(str::from_bytes(prog.error + prog.output)); + sess.note(str::from_utf8(prog.error + prog.output)); sess.abort_if_errors(); } diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index fdef25b5e711a..725b95eb9853f 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -941,7 +941,7 @@ fn read_path(d: ebml::Doc) -> (~str, uint) { do reader::with_doc_data(d) |desc| { let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint; let pathbytes = desc.slice(4u, desc.len()); - let path = str::from_bytes(pathbytes); + let path = str::from_utf8(pathbytes); (path, pos) } diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 7b60e3a5d193d..0ae94782dba27 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -99,7 +99,7 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::ident { fn parse_ident_(st: &mut PState, is_last: @fn(char) -> bool) -> ast::ident { - let rslt = scan(st, is_last, str::from_bytes); + let rslt = scan(st, is_last, str::from_utf8); return st.tcx.sess.ident_of(rslt); } @@ -453,7 +453,7 @@ fn parse_abi_set(st: &mut PState) -> AbiSet { let mut abis = AbiSet::empty(); while peek(st) != ']' { // FIXME(#5422) str API should not force this copy - let abi_str = scan(st, |c| c == ',', str::from_bytes); + let abi_str = scan(st, |c| c == ',', str::from_utf8); let abi = abi::lookup(abi_str).expect(abi_str); abis.add(abi); } diff --git a/src/librustc/rustc.rc b/src/librustc/rustc.rc index 1197693d1b6df..c02fc01a3db8e 100644 --- a/src/librustc/rustc.rc +++ b/src/librustc/rustc.rc @@ -168,7 +168,7 @@ Available lint options: let mut max_key = 0; for lint_dict.each_key |k| { max_key = uint::max(k.len(), max_key); } fn padded(max: uint, s: &str) -> ~str { - str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s + str::from_utf8(vec::from_elem(max - s.len(), ' ' as u8)) + s } io::println(fmt!("\nAvailable lint checks:\n")); io::println(fmt!(" %s %7.7s %s", @@ -251,7 +251,7 @@ pub fn run_compiler(args: &~[~str], demitter: diagnostic::Emitter) { 1u => { let ifile = matches.free[0].as_slice(); if "-" == ifile { - let src = str::from_bytes(io::stdin().read_whole_stream()); + let src = str::from_utf8(io::stdin().read_whole_stream()); str_input(src.to_managed()) } else { file_input(Path(ifile)) diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 64af555bb37a1..e8d533e0f63b7 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -433,11 +433,11 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str { ty_param(param_ty {idx: id, def_id: did}) => { if cx.sess.verbose() { fmt!("'%s:%?", - str::from_bytes([('a' as u8) + (id as u8)]), + str::from_utf8([('a' as u8) + (id as u8)]), did) } else { fmt!("'%s", - str::from_bytes([('a' as u8) + (id as u8)])) + str::from_utf8([('a' as u8) + (id as u8)])) } } ty_self(*) => ~"Self", diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs index 6ec14e2aecf62..2d4ee051503e6 100644 --- a/src/librustpkg/tests.rs +++ b/src/librustpkg/tests.rs @@ -660,7 +660,7 @@ fn test_info() { let expected_info = ~"package foo"; // fill in let workspace = create_local_package(&PkgId::new("foo")); let output = command_line_test([~"info", ~"foo"], &workspace); - assert_eq!(str::from_bytes(output.output), expected_info); + assert_eq!(str::from_utf8(output.output), expected_info); } #[test] @@ -669,7 +669,7 @@ fn test_rustpkg_test() { let expected_results = ~"1 out of 1 tests passed"; // fill in let workspace = create_local_package_with_test(&PkgId::new("foo")); let output = command_line_test([~"test", ~"foo"], &workspace); - assert_eq!(str::from_bytes(output.output), expected_results); + assert_eq!(str::from_utf8(output.output), expected_results); } #[test] @@ -679,5 +679,5 @@ fn test_uninstall() { let _output = command_line_test([~"info", ~"foo"], &workspace); command_line_test([~"uninstall", ~"foo"], &workspace); let output = command_line_test([~"list"], &workspace); - assert!(!str::from_bytes(output.output).contains("foo")); + assert!(!str::from_utf8(output.output).contains("foo")); } diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 3e771c4dddebb..8fbf77737de69 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -636,7 +636,7 @@ impl ReaderUtil for T { } bytes.push(ch as u8); } - str::from_bytes(bytes) + str::from_utf8(bytes) } fn read_line(&self) -> ~str { @@ -645,7 +645,7 @@ impl ReaderUtil for T { fn read_chars(&self, n: uint) -> ~[char] { // returns the (consumed offset, n_req), appends characters to &chars - fn chars_from_bytes(bytes: &~[u8], chars: &mut ~[char]) + fn chars_from_utf8(bytes: &~[u8], chars: &mut ~[char]) -> (uint, uint) { let mut i = 0; let bytes_len = bytes.len(); @@ -691,7 +691,7 @@ impl ReaderUtil for T { break; } bytes.push_all(data); - let (offset, nbreq) = chars_from_bytes::(&bytes, &mut chars); + let (offset, nbreq) = chars_from_utf8::(&bytes, &mut chars); let ncreq = n - chars.len(); // again we either know we need a certain number of bytes // to complete a character, or we make sure we don't @@ -1721,7 +1721,7 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> { result::chain(read_whole_file(file), |bytes| { if str::is_utf8(bytes) { - result::Ok(str::from_bytes(bytes)) + result::Ok(str::from_utf8(bytes)) } else { result::Err(file.to_str() + " is not UTF-8") } diff --git a/src/libstd/logging.rs b/src/libstd/logging.rs index c2f854179b8dd..2ebb8f4b3ea06 100644 --- a/src/libstd/logging.rs +++ b/src/libstd/logging.rs @@ -60,7 +60,7 @@ pub fn log_type(level: u32, object: &T) { } _ => { // XXX: Bad allocation - let msg = str::from_bytes(bytes); + let msg = str::from_utf8(bytes); newsched_log_str(msg); } } diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index a062838aacf04..b557685e83334 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -388,7 +388,7 @@ pub fn to_str_common (~str, bool) { let (bytes, special) = to_str_bytes_common(num, radix, negative_zero, sign, digits); - (str::from_bytes(bytes), special) + (str::from_utf8(bytes), special) } // Some constants for from_str_bytes_common's input validation, diff --git a/src/libstd/rt/io/flate.rs b/src/libstd/rt/io/flate.rs index e57b80658eef2..9d4839b9ac3bd 100644 --- a/src/libstd/rt/io/flate.rs +++ b/src/libstd/rt/io/flate.rs @@ -117,7 +117,7 @@ mod test { let mut out_bytes = [0, .. 100]; let bytes_read = inflate_reader.read(out_bytes).get(); assert_eq!(bytes_read, in_bytes.len()); - let out_msg = str::from_bytes(out_bytes); + let out_msg = str::from_utf8(out_bytes); assert!(in_msg == out_msg); } } diff --git a/src/libstd/run.rs b/src/libstd/run.rs index b204cf6cfb04f..f38d6f280676b 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -954,7 +954,7 @@ mod tests { let run::ProcessOutput {status, output, error} = run::process_output("echo", [~"hello"]); - let output_str = str::from_bytes(output); + let output_str = str::from_utf8(output); assert_eq!(status, 0); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -1016,7 +1016,7 @@ mod tests { let reader = io::FILE_reader(file, false); let buf = reader.read_whole_stream(); os::fclose(file); - str::from_bytes(buf) + str::from_utf8(buf) } } @@ -1039,7 +1039,7 @@ mod tests { let mut prog = run::Process::new("echo", [~"hello"], run::ProcessOptions::new()); let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_bytes(output); + let output_str = str::from_utf8(output); assert_eq!(status, 0); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -1053,7 +1053,7 @@ mod tests { let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_bytes(output); + let output_str = str::from_utf8(output); assert_eq!(status, 0); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -1102,7 +1102,7 @@ mod tests { fn test_keep_current_working_dir() { let mut prog = run_pwd(None); - let output = str::from_bytes(prog.finish_with_output().output); + let output = str::from_utf8(prog.finish_with_output().output); let parent_dir = os::getcwd().normalize(); let child_dir = Path(output.trim()).normalize(); @@ -1120,7 +1120,7 @@ mod tests { let parent_dir = os::getcwd().dir_path().normalize(); let mut prog = run_pwd(Some(&parent_dir)); - let output = str::from_bytes(prog.finish_with_output().output); + let output = str::from_utf8(prog.finish_with_output().output); let child_dir = Path(output.trim()).normalize(); let parent_stat = parent_dir.stat().unwrap(); @@ -1150,7 +1150,7 @@ mod tests { fn test_inherit_env() { let mut prog = run_env(None); - let output = str::from_bytes(prog.finish_with_output().output); + let output = str::from_utf8(prog.finish_with_output().output); for os::env().each |&(k, v)| { // don't check windows magical empty-named variables @@ -1165,7 +1165,7 @@ mod tests { new_env.push((~"RUN_TEST_NEW_ENV", ~"123")); let mut prog = run_env(Some(new_env.slice(0, new_env.len()))); - let output = str::from_bytes(prog.finish_with_output().output); + let output = str::from_utf8(prog.finish_with_output().output); assert!(output.contains("RUN_TEST_NEW_ENV=123")); } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 04f5247782b0e..2203a532a6cdc 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -56,16 +56,16 @@ Section: Creating a string * Raises the `not_utf8` condition if invalid UTF-8 */ -pub fn from_bytes(vv: &[u8]) -> ~str { +pub fn from_utf8(vv: &[u8]) -> ~str { use str::not_utf8::cond; if !is_utf8(vv) { let first_bad_byte = vec::find(vv, |b| !is_utf8([*b])).get(); - cond.raise(fmt!("from_bytes: input is not UTF-8; first bad byte is %u", + cond.raise(fmt!("from_utf8: input is not UTF-8; first bad byte is %u", first_bad_byte as uint)) } else { - return unsafe { raw::from_bytes(vv) } + return unsafe { raw::from_utf8(vv) } } } @@ -73,17 +73,17 @@ pub fn from_bytes(vv: &[u8]) -> ~str { * Convert a vector of bytes to a UTF-8 string. * The vector needs to be one byte longer than the string, and end with a 0 byte. * - * Compared to `from_bytes()`, this fn doesn't need to allocate a new owned str. + * Compared to `from_utf8()`, this fn doesn't need to allocate a new owned str. * * # Failure * * Fails if invalid UTF-8 * Fails if not null terminated */ -pub fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str { +pub fn from_utf8_with_null<'a>(vv: &'a [u8]) -> &'a str { assert_eq!(vv[vv.len() - 1], 0); assert!(is_utf8(vv)); - return unsafe { raw::from_bytes_with_null(vv) }; + return unsafe { raw::from_utf8_with_null(vv) }; } /** @@ -96,7 +96,7 @@ pub fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str { * * Fails if invalid UTF-8 */ -pub fn from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str { +pub fn from_utf8_slice<'a>(vector: &'a [u8]) -> &'a str { unsafe { assert!(is_utf8(vector)); let (ptr, len): (*u8, uint) = ::cast::transmute(vector); @@ -770,7 +770,7 @@ pub mod raw { } /// Converts a vector of bytes to a new owned string. - pub unsafe fn from_bytes(v: &const [u8]) -> ~str { + pub unsafe fn from_utf8(v: &const [u8]) -> ~str { do vec::as_const_buf(v) |buf, len| { from_buf_len(buf, len) } @@ -779,12 +779,12 @@ pub mod raw { /// Converts a vector of bytes to a string. /// The byte slice needs to contain valid utf8 and needs to be one byte longer than /// the string, if possible ending in a 0 byte. - pub unsafe fn from_bytes_with_null<'a>(v: &'a [u8]) -> &'a str { + pub unsafe fn from_utf8_with_null<'a>(v: &'a [u8]) -> &'a str { cast::transmute(v) } /// Converts a byte to a string. - pub unsafe fn from_byte(u: u8) -> ~str { raw::from_bytes([u]) } + pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8([u]) } /// Form a slice from a C string. Unsafe because the caller must ensure the /// C string has the static lifetime, or else the return value may be @@ -2740,14 +2740,14 @@ mod tests { } #[test] - fn test_unsafe_from_bytes() { + fn test_unsafe_from_utf8() { let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8]; - let b = unsafe { raw::from_bytes(a) }; + let b = unsafe { raw::from_utf8(a) }; assert_eq!(b, ~"AAAAAAA"); } #[test] - fn test_from_bytes() { + fn test_from_utf8() { let ss = ~"ศไทย中华Việt Nam"; let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8, 0xe0_u8, 0xb9_u8, 0x84_u8, @@ -2760,12 +2760,12 @@ mod tests { 0x20_u8, 0x4e_u8, 0x61_u8, 0x6d_u8]; - assert_eq!(ss, from_bytes(bb)); + assert_eq!(ss, from_utf8(bb)); } #[test] #[ignore(cfg(windows))] - fn test_from_bytes_fail() { + fn test_from_utf8_fail() { use str::not_utf8::cond; let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, @@ -2781,24 +2781,24 @@ mod tests { let mut error_happened = false; let _x = do cond.trap(|err| { - assert_eq!(err, ~"from_bytes: input is not UTF-8; first bad byte is 255"); + assert_eq!(err, ~"from_utf8: input is not UTF-8; first bad byte is 255"); error_happened = true; ~"" }).in { - from_bytes(bb) + from_utf8(bb) }; assert!(error_happened); } #[test] - fn test_unsafe_from_bytes_with_null() { + fn test_unsafe_from_utf8_with_null() { let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; - let b = unsafe { raw::from_bytes_with_null(a) }; + let b = unsafe { raw::from_utf8_with_null(a) }; assert_eq!(b, "AAAAAAA"); } #[test] - fn test_from_bytes_with_null() { + fn test_from_utf8_with_null() { let ss = "ศไทย中华Việt Nam"; let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8, 0xe0_u8, 0xb9_u8, 0x84_u8, @@ -2811,13 +2811,13 @@ mod tests { 0x20_u8, 0x4e_u8, 0x61_u8, 0x6d_u8, 0x0_u8]; - assert_eq!(ss, from_bytes_with_null(bb)); + assert_eq!(ss, from_utf8_with_null(bb)); } #[test] #[should_fail] #[ignore(cfg(windows))] - fn test_from_bytes_with_null_fail() { + fn test_from_utf8_with_null_fail() { let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, 0xe0_u8, 0xb9_u8, 0x84_u8, 0xe0_u8, 0xb8_u8, 0x97_u8, @@ -2829,13 +2829,13 @@ mod tests { 0x20_u8, 0x4e_u8, 0x61_u8, 0x6d_u8, 0x0_u8]; - let _x = from_bytes_with_null(bb); + let _x = from_utf8_with_null(bb); } #[test] #[should_fail] #[ignore(cfg(windows))] - fn test_from_bytes_with_null_fail_2() { + fn test_from_utf8_with_null_fail_2() { let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, 0xe0_u8, 0xb9_u8, 0x84_u8, 0xe0_u8, 0xb8_u8, 0x97_u8, @@ -2847,7 +2847,7 @@ mod tests { 0x20_u8, 0x4e_u8, 0x61_u8, 0x6d_u8, 0x60_u8]; - let _x = from_bytes_with_null(bb); + let _x = from_utf8_with_null(bb); } #[test] @@ -2995,7 +2995,7 @@ mod tests { let s1: ~str = ~"All mimsy were the borogoves"; let v: ~[u8] = s1.as_bytes().to_owned(); - let s2: ~str = from_bytes(v); + let s2: ~str = from_utf8(v); let mut i: uint = 0u; let n1: uint = s1.len(); let n2: uint = v.len(); diff --git a/src/libstd/str/ascii.rs b/src/libstd/str/ascii.rs index c71765f911afa..f30a902ed125c 100644 --- a/src/libstd/str/ascii.rs +++ b/src/libstd/str/ascii.rs @@ -65,7 +65,7 @@ impl Ascii { impl ToStr for Ascii { #[inline] - fn to_str(&self) -> ~str { str::from_bytes(['\'' as u8, self.chr, '\'' as u8]) } + fn to_str(&self) -> ~str { str::from_utf8(['\'' as u8, self.chr, '\'' as u8]) } } /// Trait for converting into an ascii type. diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 472f807cd8b68..606b521230bea 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -338,7 +338,7 @@ pub fn gather_comments_and_literals(span_diagnostic: path: @str, srdr: @io::Reader) -> (~[cmnt], ~[lit]) { - let src = str::from_bytes(srdr.read_whole_stream()).to_managed(); + let src = str::from_utf8(srdr.read_whole_stream()).to_managed(); let cm = CodeMap::new(); let filemap = cm.new_filemap(path, src); let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap); diff --git a/src/test/bench/shootout-k-nucleotide-pipes.rs b/src/test/bench/shootout-k-nucleotide-pipes.rs index e12df5811eeea..8c512a141525d 100644 --- a/src/test/bench/shootout-k-nucleotide-pipes.rs +++ b/src/test/bench/shootout-k-nucleotide-pipes.rs @@ -67,7 +67,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str { for pairs_sorted.each |kv| { let (k,v) = copy *kv; unsafe { - let b = str::raw::from_bytes(k); + let b = str::raw::from_utf8(k); // 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. buffer += (fmt!("%s %0.3f\n", b.to_ascii().to_upper().to_str_ascii(), v)); diff --git a/src/test/bench/shootout-k-nucleotide.rs b/src/test/bench/shootout-k-nucleotide.rs index 646b9788f706a..c5de5b177d144 100644 --- a/src/test/bench/shootout-k-nucleotide.rs +++ b/src/test/bench/shootout-k-nucleotide.rs @@ -60,7 +60,7 @@ impl Code { } reverse(result); - str::from_bytes(result) + str::from_utf8(result) } } diff --git a/src/test/run-pass/core-run-destroy.rs b/src/test/run-pass/core-run-destroy.rs index 81cdb926e5f96..83513b0d5daf9 100644 --- a/src/test/run-pass/core-run-destroy.rs +++ b/src/test/run-pass/core-run-destroy.rs @@ -45,7 +45,7 @@ fn test_destroy_actually_kills(force: bool) { #[cfg(unix)] fn process_exists(pid: libc::pid_t) -> bool { let run::ProcessOutput {output, _} = run::process_output("ps", [~"-p", pid.to_str()]); - str::from_bytes(output).contains(pid.to_str()) + str::from_utf8(output).contains(pid.to_str()) } #[cfg(windows)] diff --git a/src/test/run-pass/hashmap-memory.rs b/src/test/run-pass/hashmap-memory.rs index eae2f507c5197..b660f6fafff36 100644 --- a/src/test/run-pass/hashmap-memory.rs +++ b/src/test/run-pass/hashmap-memory.rs @@ -80,7 +80,7 @@ mod map_reduce { mapper_done => { num_mappers -= 1; } find_reducer(k, cc) => { let mut c; - match reducers.find(&str::from_bytes(k)) { + match reducers.find(&str::from_utf8(k)) { Some(&_c) => { c = _c; } None => { c = 0; } } From 73b7604a71836013bb45cc9de0d155d774824293 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 15 Jun 2013 16:02:30 -0700 Subject: [PATCH 5/6] std: add str::from_utf8_slice{,with_null} --- src/librustc/metadata/decoder.rs | 2 +- src/librustc/metadata/tydecode.rs | 15 +-- src/librustdoc/markdown_writer.rs | 4 +- src/librustpkg/rustpkg.rc | 2 +- src/librustpkg/tests.rs | 6 +- src/librustpkg/version.rs | 6 +- src/libstd/rt/io/flate.rs | 2 +- src/libstd/str.rs | 143 +++++++++++++++++++++++++++-- src/test/run-pass/const-str-ptr.rs | 2 +- 9 files changed, 156 insertions(+), 26 deletions(-) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 725b95eb9853f..7091b68c42ae4 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -941,7 +941,7 @@ fn read_path(d: ebml::Doc) -> (~str, uint) { do reader::with_doc_data(d) |desc| { let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint; let pathbytes = desc.slice(4u, desc.len()); - let path = str::from_utf8(pathbytes); + let path = str::from_utf8_slice(pathbytes).to_owned(); (path, pos) } diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 0ae94782dba27..f3dedd58ce24b 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -97,10 +97,10 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::ident { return parse_ident_(st, |a| is_last(last, a) ); } -fn parse_ident_(st: &mut PState, is_last: @fn(char) -> bool) -> - ast::ident { - let rslt = scan(st, is_last, str::from_utf8); - return st.tcx.sess.ident_of(rslt); +fn parse_ident_(st: &mut PState, is_last: @fn(char) -> bool) -> ast::ident { + do scan(st, is_last) |v| { + st.tcx.sess.ident_of(str::from_utf8_slice(v)) + } } pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: int, @@ -452,9 +452,10 @@ fn parse_abi_set(st: &mut PState) -> AbiSet { assert_eq!(next(st), '['); let mut abis = AbiSet::empty(); while peek(st) != ']' { - // FIXME(#5422) str API should not force this copy - let abi_str = scan(st, |c| c == ',', str::from_utf8); - let abi = abi::lookup(abi_str).expect(abi_str); + let abi = do scan(st, |c| c == ',') |v| { + let abi_str = str::from_utf8_slice(v); + abi::lookup(abi_str).expect(abi_str) + }; abis.add(abi); } assert_eq!(next(st), ']'); diff --git a/src/librustdoc/markdown_writer.rs b/src/librustdoc/markdown_writer.rs index 353152763267c..8da7ea431f900 100644 --- a/src/librustdoc/markdown_writer.rs +++ b/src/librustdoc/markdown_writer.rs @@ -116,8 +116,8 @@ fn pandoc_writer( debug!("pandoc result: %i", output.status); if output.status != 0 { - error!("pandoc-out: %s", str::from_bytes(output.output)); - error!("pandoc-err: %s", str::from_bytes(output.error)); + error!("pandoc-out: %s", str::from_utf8_slice(output.output)); + error!("pandoc-err: %s", str::from_utf8_slice(output.error)); fail!("pandoc failed"); } } diff --git a/src/librustpkg/rustpkg.rc b/src/librustpkg/rustpkg.rc index 9242e450e2499..79de7038d3345 100644 --- a/src/librustpkg/rustpkg.rc +++ b/src/librustpkg/rustpkg.rc @@ -163,7 +163,7 @@ impl<'self> PkgScript<'self> { exe.to_str(), root.to_str(), "configs"); let output = run::process_output(exe.to_str(), [root.to_str(), ~"configs"]); // Run the configs() function to get the configs - let cfgs = str::from_bytes_slice(output.output).word_iter() + let cfgs = str::from_utf8_slice(output.output).word_iter() .transform(|w| w.to_owned()).collect(); (cfgs, output.status) } diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs index 2d4ee051503e6..1f3cfa35d006a 100644 --- a/src/librustpkg/tests.rs +++ b/src/librustpkg/tests.rs @@ -145,8 +145,8 @@ fn command_line_test(args: &[~str], cwd: &Path) -> ProcessOutput { }); let output = prog.finish_with_output(); io::println(fmt!("Output from command %s with args %? was %s {%s}[%?]", - cmd, args, str::from_bytes(output.output), - str::from_bytes(output.error), + cmd, args, str::from_utf8_slice(output.output), + str::from_utf8_slice(output.error), output.status)); /* By the way, rustpkg *won't* return a nonzero exit code if it fails -- @@ -246,7 +246,7 @@ fn assert_executable_exists(repo: &Path, short_name: &str) { fn command_line_test_output(args: &[~str]) -> ~[~str] { let mut result = ~[]; let p_output = command_line_test(args, &os::getcwd()); - let test_output = str::from_bytes(p_output.output); + let test_output = str::from_utf8_slice(p_output.output); for test_output.split_iter('\n').advance |s| { result += [s.to_owned()]; } diff --git a/src/librustpkg/version.rs b/src/librustpkg/version.rs index 41338f88735e9..415ba49d62a82 100644 --- a/src/librustpkg/version.rs +++ b/src/librustpkg/version.rs @@ -95,15 +95,15 @@ pub fn try_getting_version(remote_path: &RemotePath) -> Option { tmp_dir.to_str()]); if outp.status == 0 { debug!("Cloned it... ( %s, %s )", - str::from_bytes(outp.output), - str::from_bytes(outp.error)); + str::from_utf8_slice(outp.output), + str::from_utf8_slice(outp.error)); let mut output = None; debug!("(getting version, now getting tags) executing {git --git-dir=%s tag -l}", tmp_dir.push(".git").to_str()); let outp = run::process_output("git", [fmt!("--git-dir=%s", tmp_dir.push(".git").to_str()), ~"tag", ~"-l"]); - let output_text = str::from_bytes(outp.output); + let output_text = str::from_utf8_slice(outp.output); debug!("Full output: ( %s ) [%?]", output_text, outp.status); for output_text.line_iter().advance |l| { debug!("A line of output: %s", l); diff --git a/src/libstd/rt/io/flate.rs b/src/libstd/rt/io/flate.rs index 9d4839b9ac3bd..6a6a39ad6b990 100644 --- a/src/libstd/rt/io/flate.rs +++ b/src/libstd/rt/io/flate.rs @@ -117,7 +117,7 @@ mod test { let mut out_bytes = [0, .. 100]; let bytes_read = inflate_reader.read(out_bytes).get(); assert_eq!(bytes_read, in_bytes.len()); - let out_msg = str::from_utf8(out_bytes); + let out_msg = str::from_utf8_slice(out_bytes); assert!(in_msg == out_msg); } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 2203a532a6cdc..a112266faa59e 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -96,13 +96,26 @@ pub fn from_utf8_with_null<'a>(vv: &'a [u8]) -> &'a str { * * Fails if invalid UTF-8 */ -pub fn from_utf8_slice<'a>(vector: &'a [u8]) -> &'a str { - unsafe { - assert!(is_utf8(vector)); - let (ptr, len): (*u8, uint) = ::cast::transmute(vector); - let string: &'a str = ::cast::transmute((ptr, len + 1)); - string - } +pub fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { + assert!(is_utf8(v)); + unsafe { raw::from_utf8_slice(v) } +} + +/** + * Convert a vector of bytes to a UTF-8 string. + * The vector needs to be one byte longer than the string, and end with a 0 byte. + * + * Compared to `from_utf8()`, this fn doesn't need to allocate a new owned str. + * + * # Failure + * + * Fails if not NULL terminated + * Fails if invalid UTF-8 + */ +pub fn from_utf8_slice_with_null<'a>(v: &'a [u8]) -> &'a str { + assert_eq!(v[v.len() - 1], 0); + assert!(is_utf8(v)); + unsafe { raw::from_utf8_slice_with_null(v) } } /// Copy a slice into a new unique str @@ -783,6 +796,20 @@ pub mod raw { cast::transmute(v) } + /// Converts a vector of bytes to a string slice. + /// The byte slice needs to contain valid utf8. + pub unsafe fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { + let (ptr, len): (*u8, uint) = ::cast::transmute(v); + cast::transmute((ptr, len + 1)) + } + + /// Converts a vector of bytes to a string. + /// The byte slice needs to contain valid utf8 and needs to be one byte longer than + /// the string, if possible ending in a 0 byte. + pub unsafe fn from_utf8_slice_with_null<'a>(v: &'a [u8]) -> &'a str { + cast::transmute(v) + } + /// Converts a byte to a string. pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8([u]) } @@ -2850,6 +2877,108 @@ mod tests { let _x = from_utf8_with_null(bb); } + #[test] + fn test_unsafe_from_utf8_slice() { + let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8]; + let b = unsafe { raw::from_utf8_slice(a) }; + assert_eq!(b, "AAAAAAA"); + } + + #[test] + fn test_from_utf8_slice() { + let ss = "ศไทย中华Việt Nam"; + let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8]; + + assert_eq!(ss, from_utf8_slice(bb)); + } + + #[test] + #[should_fail] + #[ignore(cfg(windows))] + fn test_from_utf8_slice_fail() { + let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8]; + + let _x = from_utf8_slice(bb); + } + + #[test] + fn test_unsafe_from_utf8_slice_with_null() { + let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; + let b = unsafe { raw::from_utf8_slice_with_null(a) }; + assert_eq!(b, "AAAAAAA"); + } + + #[test] + fn test_from_utf8_slice_with_null() { + let ss = "ศไทย中华Việt Nam"; + let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x0_u8]; + + assert_eq!(ss, from_utf8_slice_with_null(bb)); + } + + #[test] + #[should_fail] + #[ignore(cfg(windows))] + fn test_from_utf8_slice_with_null_fail() { + let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x0_u8]; + + let _x = from_utf8_slice_with_null(bb); + } + + #[test] + #[should_fail] + #[ignore(cfg(windows))] + fn test_from_utf8_slice_with_null_fail_2() { + let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x60_u8]; + + let _x = from_utf8_slice_with_null(bb); + } + #[test] fn test_from_buf() { unsafe { diff --git a/src/test/run-pass/const-str-ptr.rs b/src/test/run-pass/const-str-ptr.rs index 2f0cd3c611f62..a2eeef453b905 100644 --- a/src/test/run-pass/const-str-ptr.rs +++ b/src/test/run-pass/const-str-ptr.rs @@ -16,7 +16,7 @@ static b: *u8 = c as *u8; pub fn main() { let foo = &a as *u8; - assert_eq!(unsafe { str::raw::from_bytes(a) }, ~"hi\x00"); + assert_eq!(unsafe { str::raw::from_utf8_slice(a) }, "hi\x00"); assert_eq!(unsafe { str::raw::from_buf(foo) }, ~"hi"); assert_eq!(unsafe { str::raw::from_buf(b) }, ~"hi"); assert!(unsafe { *b == a[0] }); From 486509bcf93730bcbd81ef5a632b95caa9fedcea Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 12 Jun 2013 11:18:06 -0700 Subject: [PATCH 6/6] std: change std::{from_bytes,from_bytes_with_null} to consume vec --- src/compiletest/procsrv.rs | 8 +- src/librustc/util/ppaux.rs | 7 +- src/libstd/str.rs | 173 ++++++++++++++++++------------------- src/libstd/str/ascii.rs | 4 +- 4 files changed, 94 insertions(+), 98 deletions(-) diff --git a/src/compiletest/procsrv.rs b/src/compiletest/procsrv.rs index c319069779ae4..9cb15a1784ff6 100644 --- a/src/compiletest/procsrv.rs +++ b/src/compiletest/procsrv.rs @@ -61,11 +61,11 @@ pub fn run(lib_path: &str, for input.iter().advance |input| { proc.input().write_str(*input); } - let output = proc.finish_with_output(); + let run::ProcessOutput { status, output, error, _ } = proc.finish_with_output(); Result { - status: output.status, - out: str::from_utf8(output.output), - err: str::from_utf8(output.error) + status: status, + out: str::from_utf8(output), + err: str::from_utf8(error) } } diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index e8d533e0f63b7..b5304c14ca5f5 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -432,12 +432,9 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str { ty_err => ~"[type error]", ty_param(param_ty {idx: id, def_id: did}) => { if cx.sess.verbose() { - fmt!("'%s:%?", - str::from_utf8([('a' as u8) + (id as u8)]), - did) + fmt!("'%s:%?", str::from_byte(('a' as u8) + (id as u8)), did) } else { - fmt!("'%s", - str::from_utf8([('a' as u8) + (id as u8)])) + fmt!("'%s", str::from_byte(('a' as u8) + (id as u8))) } } ty_self(*) => ~"Self", diff --git a/src/libstd/str.rs b/src/libstd/str.rs index a112266faa59e..f3c4261d4216a 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -55,17 +55,16 @@ Section: Creating a string * * Raises the `not_utf8` condition if invalid UTF-8 */ - -pub fn from_utf8(vv: &[u8]) -> ~str { +pub fn from_utf8(v: ~[u8]) -> ~str { use str::not_utf8::cond; - if !is_utf8(vv) { - let first_bad_byte = vec::find(vv, |b| !is_utf8([*b])).get(); + if !is_utf8(v) { + let first_bad_byte = vec::find(v, |b| !is_utf8([*b])).get(); cond.raise(fmt!("from_utf8: input is not UTF-8; first bad byte is %u", first_bad_byte as uint)) } else { - return unsafe { raw::from_utf8(vv) } + return unsafe { raw::from_utf8(v) } } } @@ -73,17 +72,15 @@ pub fn from_utf8(vv: &[u8]) -> ~str { * Convert a vector of bytes to a UTF-8 string. * The vector needs to be one byte longer than the string, and end with a 0 byte. * - * Compared to `from_utf8()`, this fn doesn't need to allocate a new owned str. - * * # Failure * * Fails if invalid UTF-8 * Fails if not null terminated */ -pub fn from_utf8_with_null<'a>(vv: &'a [u8]) -> &'a str { - assert_eq!(vv[vv.len() - 1], 0); - assert!(is_utf8(vv)); - return unsafe { raw::from_utf8_with_null(vv) }; +pub fn from_utf8_with_null(v: ~[u8]) -> ~str { + assert_eq!(v[v.len() - 1], 0); + assert!(is_utf8(v)); + unsafe { raw::from_utf8_with_null(v) } } /** @@ -783,16 +780,14 @@ pub mod raw { } /// Converts a vector of bytes to a new owned string. - pub unsafe fn from_utf8(v: &const [u8]) -> ~str { - do vec::as_const_buf(v) |buf, len| { - from_buf_len(buf, len) - } + pub unsafe fn from_utf8(mut v: ~[u8]) -> ~str { + // Make sure the string is NULL terminated. + v.push(0); + from_utf8_with_null(v) } - /// Converts a vector of bytes to a string. - /// The byte slice needs to contain valid utf8 and needs to be one byte longer than - /// the string, if possible ending in a 0 byte. - pub unsafe fn from_utf8_with_null<'a>(v: &'a [u8]) -> &'a str { + /// Converts a vector of bytes with a trailing null to a new owned string. + pub unsafe fn from_utf8_with_null(v: ~[u8]) -> ~str { cast::transmute(v) } @@ -811,7 +806,7 @@ pub mod raw { } /// Converts a byte to a string. - pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8([u]) } + pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8_with_null(~[u, 0]) } /// Form a slice from a C string. Unsafe because the caller must ensure the /// C string has the static lifetime, or else the return value may be @@ -2247,17 +2242,19 @@ impl Zero for @str { #[cfg(test)] mod tests { - use iterator::IteratorUtil; + use super::*; + use char; + use cmp::{TotalOrd, Less, Equal, Greater}; use container::Container; - use option::Some; + use iterator::IteratorUtil; use libc::c_char; use libc; use old_iter::BaseIter; + use option::Some; use ptr; - use str::*; - use vec; + use uint; use vec::{ImmutableVector, CopyableVector}; - use cmp::{TotalOrd, Less, Equal, Greater}; + use vec; #[test] fn test_eq() { @@ -2777,15 +2774,15 @@ mod tests { fn test_from_utf8() { let ss = ~"ศไทย中华Việt Nam"; let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8]; + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8]; assert_eq!(ss, from_utf8(bb)); } @@ -2795,23 +2792,23 @@ mod tests { fn test_from_utf8_fail() { use str::not_utf8::cond; - let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8]; - let mut error_happened = false; let _x = do cond.trap(|err| { assert_eq!(err, ~"from_utf8: input is not UTF-8; first bad byte is 255"); error_happened = true; ~"" }).in { + let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8]; + from_utf8(bb) }; assert!(error_happened); @@ -2819,24 +2816,24 @@ mod tests { #[test] fn test_unsafe_from_utf8_with_null() { - let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; + let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; let b = unsafe { raw::from_utf8_with_null(a) }; - assert_eq!(b, "AAAAAAA"); + assert_eq!(b, ~"AAAAAAA"); } #[test] fn test_from_utf8_with_null() { - let ss = "ศไทย中华Việt Nam"; - let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8, 0x0_u8]; + let ss = ~"ศไทย中华Việt Nam"; + let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x0_u8]; assert_eq!(ss, from_utf8_with_null(bb)); } @@ -2845,16 +2842,16 @@ mod tests { #[should_fail] #[ignore(cfg(windows))] fn test_from_utf8_with_null_fail() { - let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8, 0x0_u8]; + let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x0_u8]; let _x = from_utf8_with_null(bb); } @@ -2863,16 +2860,16 @@ mod tests { #[should_fail] #[ignore(cfg(windows))] fn test_from_utf8_with_null_fail_2() { - let bb = [0xff_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8, 0x60_u8]; + let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, + 0xe0_u8, 0xb9_u8, 0x84_u8, + 0xe0_u8, 0xb8_u8, 0x97_u8, + 0xe0_u8, 0xb8_u8, 0xa2_u8, + 0xe4_u8, 0xb8_u8, 0xad_u8, + 0xe5_u8, 0x8d_u8, 0x8e_u8, + 0x56_u8, 0x69_u8, 0xe1_u8, + 0xbb_u8, 0x87_u8, 0x74_u8, + 0x20_u8, 0x4e_u8, 0x61_u8, + 0x6d_u8, 0x60_u8]; let _x = from_utf8_with_null(bb); } @@ -3121,21 +3118,21 @@ mod tests { #[test] fn vec_str_conversions() { - let s1: ~str = ~"All mimsy were the borogoves"; + let s1 = ~"All mimsy were the borogoves"; + let n1 = s1.len(); + let v = s1.as_bytes().to_owned(); + let n2 = v.len(); - let v: ~[u8] = s1.as_bytes().to_owned(); - let s2: ~str = from_utf8(v); - let mut i: uint = 0u; - let n1: uint = s1.len(); - let n2: uint = v.len(); assert_eq!(n1, n2); - while i < n1 { - let a: u8 = s1[i]; - let b: u8 = s2[i]; + + let s2 = from_utf8(v); + + for uint::range(0, n1) |i| { + let a = s1[i]; + let b = s2[i]; debug!(a); debug!(b); assert_eq!(a, b); - i += 1u; } } diff --git a/src/libstd/str/ascii.rs b/src/libstd/str/ascii.rs index f30a902ed125c..28739f7b54e08 100644 --- a/src/libstd/str/ascii.rs +++ b/src/libstd/str/ascii.rs @@ -65,7 +65,9 @@ impl Ascii { impl ToStr for Ascii { #[inline] - fn to_str(&self) -> ~str { str::from_utf8(['\'' as u8, self.chr, '\'' as u8]) } + fn to_str(&self) -> ~str { + str::from_utf8_with_null(~['\'' as u8, self.chr, '\'' as u8, 0]) + } } /// Trait for converting into an ascii type.