Skip to content

Commit

Permalink
auto merge of #10701 : huonw/rust/rm-from_utf8, r=brson
Browse files Browse the repository at this point in the history
This function had type &[u8] -> ~str, i.e. it allocates a string
internally, even though the non-allocating version that take &[u8] ->
&str and ~[u8] -> ~str are all that is necessary in most circumstances.
  • Loading branch information
bors committed Dec 4, 2013
2 parents 5fa6bd5 + b0426ed commit 9b9cf98
Show file tree
Hide file tree
Showing 36 changed files with 122 additions and 226 deletions.
9 changes: 4 additions & 5 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub fn run(lib_path: &str,
for input in input.iter() {
process.input().write(input.as_bytes());
}
let output = process.finish_with_output();
let run::ProcessOutput { status, output, error } = process.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_owned(output),
err: str::from_utf8_owned(error)
}
}

Expand All @@ -90,4 +90,3 @@ pub fn run_background(lib_path: &str,

return process;
}

3 changes: 1 addition & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {

let adb_arg = format!("export LD_LIBRARY_PATH={}; gdbserver :5039 {}/{}",
config.adb_test_dir.clone(), config.adb_test_dir.clone(),
str::from_utf8(exe_file.filename().unwrap())).clone();
str::from_utf8(exe_file.filename().unwrap()));

let mut process = procsrv::run_background("", config.adb_path.clone(),
[~"shell",adb_arg.clone()],~[(~"",~"")], Some(~""));
Expand Down Expand Up @@ -1151,4 +1151,3 @@ fn run_codegen_test(config: &config, props: &TestProps,
(base_lines as f64) / (clang_lines as f64),
0.001);
}

4 changes: 2 additions & 2 deletions src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<'self> FromBase64 for &'self str {
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
* to the byte values it encodes.
*
* You can use the `from_utf8` function in `std::str`
* You can use the `from_utf8_owned` function in `std::str`
* to turn a `[u8]` into a string with characters corresponding to those
* values.
*
Expand All @@ -180,7 +180,7 @@ impl<'self> FromBase64 for &'self str {
* println!("base64 output: {}", hello_str);
* let res = hello_str.from_base64();
* if res.is_ok() {
* let optBytes = str::from_utf8_opt(res.unwrap());
* let optBytes = str::from_utf8_owned_opt(res.unwrap());
* if optBytes.is_some() {
* println!("decoded from base64: {}", optBytes.unwrap());
* }
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Doc {
}

pub fn as_str_slice<'a>(&'a self) -> &'a str {
str::from_utf8_slice(self.data.slice(self.start, self.end))
str::from_utf8(self.data.slice(self.start, self.end))
}

pub fn as_str(&self) -> ~str {
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'self> FromHex for &'self str {
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
* to the byte values it encodes.
*
* You can use the `from_utf8` function in `std::str`
* You can use the `from_utf8_owned` function in `std::str`
* to turn a `[u8]` into a string with characters corresponding to those
* values.
*
Expand All @@ -80,7 +80,7 @@ impl<'self> FromHex for &'self str {
* println!("{}", hello_str);
* let bytes = hello_str.from_hex().unwrap();
* println!("{:?}", bytes);
* let result_str = str::from_utf8(bytes);
* let result_str = str::from_utf8_owned(bytes);
* println!("{}", result_str);
* }
* ```
Expand Down
4 changes: 3 additions & 1 deletion src/libextra/terminfo/parser/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ pub fn parse(file: &mut io::Reader,
return Err(~"incompatible file: more string offsets than expected");
}

let names_str = str::from_utf8(file.read_bytes(names_bytes as uint - 1)); // don't read NUL
// don't read NUL
let names_str = str::from_utf8_owned(file.read_bytes(names_bytes as uint - 1));

let term_names: ~[~str] = names_str.split('|').map(|s| s.to_owned()).collect();

file.read_byte(); // consume NUL
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl Uuid {
s[i*2+0] = digit[0];
s[i*2+1] = digit[1];
}
str::from_utf8(s)
str::from_utf8_owned(s)
}

/// Returns a string of hexadecimal digits, separated into groups with a hypen
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn json_encode<'self, T:Encodable<json::Encoder<'self>>>(t: &T) -> ~str {
let mut writer = MemWriter::new();
let mut encoder = json::Encoder::init(&mut writer as &mut io::Writer);
t.encode(&mut encoder);
str::from_utf8(writer.inner_ref().as_slice())
str::from_utf8_owned(writer.inner())
}

// FIXME(#5121)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ pub mod write {
if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc, prog.status));
sess.note(format!("{} arguments: '{}'", cc, args.connect("' '")));
sess.note(str::from_utf8(prog.error + prog.output));
sess.note(str::from_utf8_owned(prog.error + prog.output));
sess.abort_if_errors();
}
}
Expand Down Expand Up @@ -1079,7 +1079,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,
if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc_prog, prog.status));
sess.note(format!("{} arguments: '{}'", cc_prog, cc_args.connect("' '")));
sess.note(str::from_utf8(prog.error + prog.output));
sess.note(str::from_utf8_owned(prog.error + prog.output));
sess.abort_if_errors();
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Available lint options:
max_key = num::max(name.len(), max_key);
}
fn padded(max: uint, s: &str) -> ~str {
str::from_utf8(vec::from_elem(max - s.len(), ' ' as u8)) + s
" ".repeat(max - s.len()) + s
}
println("\nAvailable lint checks:\n");
println!(" {} {:7.7s} {}",
Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
1u => {
let ifile = matches.free[0].as_slice();
if "-" == ifile {
let src = str::from_utf8(io::stdin().read_to_end());
let src = str::from_utf8_owned(io::stdin().read_to_end());
str_input(src.to_managed())
} else {
file_input(Path::init(ifile))
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,8 +1274,8 @@ fn family_names_type(fam: Family) -> bool {
fn read_path(d: ebml::Doc) -> (~str, uint) {
reader::with_doc_data(d, |desc| {
let pos = u64_from_be_bytes(desc, 0u, 4u) as uint;
let pathbytes = desc.slice(4u, desc.len());
let path = str::from_utf8(pathbytes);
let pathbytes = desc.slice_from(4u).to_owned();
let path = str::from_utf8_owned(pathbytes);

(path, pos)
})
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,5 +1902,5 @@ pub fn encoded_ty(tcx: ty::ctxt, t: ty::t) -> ~str {
abbrevs: tyencode::ac_no_abbrevs};
let wr = @mut MemWriter::new();
tyencode::enc_ty(wr, cx, t);
str::from_utf8(*wr.inner_ref())
str::from_utf8_owned(wr.inner_ref().to_owned())
}
14 changes: 8 additions & 6 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident {
}

fn parse_ident_(st: &mut PState, is_last: |char| -> bool) -> ast::Ident {
let rslt = scan(st, is_last, str::from_utf8);
return st.tcx.sess.ident_of(rslt);
scan(st, is_last, |bytes| {
st.tcx.sess.ident_of(str::from_utf8(bytes))
})
}

pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
Expand Down Expand Up @@ -492,10 +493,11 @@ 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);
abis.add(abi);
scan(st, |c| c == ',', |bytes| {
let abi_str = str::from_utf8(bytes).to_owned();
let abi = abi::lookup(abi_str).expect(abi_str);
abis.add(abi);
});
}
assert_eq!(next(st), ']');
return abis;
Expand Down
2 changes: 1 addition & 1 deletion src/librustpkg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'self> PkgScript<'self> {
[sysroot.as_str().unwrap().to_owned(), ~"configs"]);
debug!("run_custom: second pkg command did {:?}", output.status);
// Run the configs() function to get the configs
let cfgs = str::from_utf8_slice(output.output).words()
let cfgs = str::from_utf8(output.output).words()
.map(|w| w.to_owned()).collect();
(cfgs, output.status)
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~s
});
let output = prog.finish_with_output();
debug!("Output from command {} with args {:?} was {} \\{{}\\}[{:?}]",
cmd, args, str::from_utf8(output.output),
str::from_utf8(output.error),
output.status);
cmd, args, str::from_utf8(output.output),
str::from_utf8(output.error),
output.status);
if !output.status.success() {
debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} ---",
debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} {} ---",
cmd, args, output.status,
str::from_utf8(output.output) + str::from_utf8(output.error));
str::from_utf8(output.output), str::from_utf8(output.error));
Fail(output)
}
else {
Expand Down Expand Up @@ -1204,7 +1204,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.path());
assert_eq!(str::from_utf8(output.output), expected_info);
assert_eq!(str::from_utf8_owned(output.output), expected_info);
}
#[test]
Expand Down
22 changes: 11 additions & 11 deletions src/librustpkg/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ pub fn try_getting_local_version(local_path: &Path) -> Option<Version> {
continue;
}

let mut output = None;
let output_text = str::from_utf8(outp.output);
for l in output_text.lines() {
if !l.is_whitespace() {
output = Some(l);
}
match output.and_then(try_parsing_version) {
Some(v) => return Some(v),
None => ()
let mut output = None;
let output_text = str::from_utf8(outp.output);
for l in output_text.lines() {
if !l.is_whitespace() {
output = Some(l);
}
match output.and_then(try_parsing_version) {
Some(v) => return Some(v),
None => ()
}
}
}
}
None
None
}

/// If `remote_path` refers to a git repo that can be downloaded,
Expand Down
4 changes: 2 additions & 2 deletions src/librustuv/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ mod test {

let nread = result.unwrap();
assert!(nread > 0);
let read_str = str::from_utf8(read_mem.slice(0, nread as uint));
assert_eq!(read_str, ~"hello");
let read_str = str::from_utf8(read_mem.slice_to(nread as uint));
assert_eq!(read_str, "hello");
}
// unlink
let result = FsRequest::unlink(l(), &path_str.to_c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/librustuv/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn sockaddr_to_socket_addr(addr: *sockaddr) -> SocketAddr {
};
port as u16
};
let ip_str = str::from_utf8_slice(ip_name).trim_right_chars(&'\x00');
let ip_str = str::from_utf8(ip_name).trim_right_chars(&'\x00');
let ip_addr = FromStr::from_str(ip_str).unwrap();

SocketAddr { ip: ip_addr, port: ip_port }
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl CString {
if self.buf.is_null() { return None; }
let buf = self.as_bytes();
let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
str::from_utf8_slice_opt(buf)
str::from_utf8_opt(buf)
}

/// Return a CString iterator.
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ impl<'self> Formatter<'self> {

fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) {
::uint::to_str_bytes(value, 10, |buf| {
let valuestr = str::from_utf8_slice(buf);
let valuestr = str::from_utf8(buf);
for piece in pieces.iter() {
self.run(piece, Some(valuestr));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/io/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod test {
fn smoke_test() {
let mem_writer = MemWriter::new();
let mut deflate_writer = DeflateWriter::new(mem_writer);
let in_msg = "test";
let in_msg: &str = "test";
let in_bytes = in_msg.as_bytes();
deflate_writer.write(in_bytes);
deflate_writer.flush();
Expand All @@ -118,6 +118,6 @@ mod test {
let bytes_read = inflate_reader.read(out_bytes).unwrap();
assert_eq!(bytes_read, in_bytes.len());
let out_msg = str::from_utf8(out_bytes);
assert!(in_msg == out_msg);
assert_eq!(in_msg, out_msg);
}
}
Loading

0 comments on commit 9b9cf98

Please sign in to comment.