Skip to content

Commit 9b9cf98

Browse files
committed
auto merge of #10701 : huonw/rust/rm-from_utf8, r=brson
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.
2 parents 5fa6bd5 + b0426ed commit 9b9cf98

36 files changed

+122
-226
lines changed

Diff for: src/compiletest/procsrv.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ pub fn run(lib_path: &str,
6060
for input in input.iter() {
6161
process.input().write(input.as_bytes());
6262
}
63-
let output = process.finish_with_output();
63+
let run::ProcessOutput { status, output, error } = process.finish_with_output();
6464

6565
Result {
66-
status: output.status,
67-
out: str::from_utf8(output.output),
68-
err: str::from_utf8(output.error)
66+
status: status,
67+
out: str::from_utf8_owned(output),
68+
err: str::from_utf8_owned(error)
6969
}
7070
}
7171

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

9191
return process;
9292
}
93-

Diff for: src/compiletest/runtest.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
298298

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

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

Diff for: src/libextra/base64.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'self> FromBase64 for &'self str {
162162
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
163163
* to the byte values it encodes.
164164
*
165-
* You can use the `from_utf8` function in `std::str`
165+
* You can use the `from_utf8_owned` function in `std::str`
166166
* to turn a `[u8]` into a string with characters corresponding to those
167167
* values.
168168
*
@@ -180,7 +180,7 @@ impl<'self> FromBase64 for &'self str {
180180
* println!("base64 output: {}", hello_str);
181181
* let res = hello_str.from_base64();
182182
* if res.is_ok() {
183-
* let optBytes = str::from_utf8_opt(res.unwrap());
183+
* let optBytes = str::from_utf8_owned_opt(res.unwrap());
184184
* if optBytes.is_some() {
185185
* println!("decoded from base64: {}", optBytes.unwrap());
186186
* }

Diff for: src/libextra/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Doc {
4141
}
4242

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

4747
pub fn as_str(&self) -> ~str {

Diff for: src/libextra/hex.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'self> FromHex for &'self str {
6262
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
6363
* to the byte values it encodes.
6464
*
65-
* You can use the `from_utf8` function in `std::str`
65+
* You can use the `from_utf8_owned` function in `std::str`
6666
* to turn a `[u8]` into a string with characters corresponding to those
6767
* values.
6868
*
@@ -80,7 +80,7 @@ impl<'self> FromHex for &'self str {
8080
* println!("{}", hello_str);
8181
* let bytes = hello_str.from_hex().unwrap();
8282
* println!("{:?}", bytes);
83-
* let result_str = str::from_utf8(bytes);
83+
* let result_str = str::from_utf8_owned(bytes);
8484
* println!("{}", result_str);
8585
* }
8686
* ```

Diff for: src/libextra/terminfo/parser/compiled.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ pub fn parse(file: &mut io::Reader,
215215
return Err(~"incompatible file: more string offsets than expected");
216216
}
217217

218-
let names_str = str::from_utf8(file.read_bytes(names_bytes as uint - 1)); // don't read NUL
218+
// don't read NUL
219+
let names_str = str::from_utf8_owned(file.read_bytes(names_bytes as uint - 1));
220+
219221
let term_names: ~[~str] = names_str.split('|').map(|s| s.to_owned()).collect();
220222

221223
file.read_byte(); // consume NUL

Diff for: src/libextra/uuid.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl Uuid {
310310
s[i*2+0] = digit[0];
311311
s[i*2+1] = digit[1];
312312
}
313-
str::from_utf8(s)
313+
str::from_utf8_owned(s)
314314
}
315315

316316
/// Returns a string of hexadecimal digits, separated into groups with a hypen

Diff for: src/libextra/workcache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn json_encode<'self, T:Encodable<json::Encoder<'self>>>(t: &T) -> ~str {
260260
let mut writer = MemWriter::new();
261261
let mut encoder = json::Encoder::init(&mut writer as &mut io::Writer);
262262
t.encode(&mut encoder);
263-
str::from_utf8(writer.inner_ref().as_slice())
263+
str::from_utf8_owned(writer.inner())
264264
}
265265

266266
// FIXME(#5121)

Diff for: src/librustc/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ pub mod write {
368368
if !prog.status.success() {
369369
sess.err(format!("linking with `{}` failed: {}", cc, prog.status));
370370
sess.note(format!("{} arguments: '{}'", cc, args.connect("' '")));
371-
sess.note(str::from_utf8(prog.error + prog.output));
371+
sess.note(str::from_utf8_owned(prog.error + prog.output));
372372
sess.abort_if_errors();
373373
}
374374
}
@@ -1079,7 +1079,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,
10791079
if !prog.status.success() {
10801080
sess.err(format!("linking with `{}` failed: {}", cc_prog, prog.status));
10811081
sess.note(format!("{} arguments: '{}'", cc_prog, cc_args.connect("' '")));
1082-
sess.note(str::from_utf8(prog.error + prog.output));
1082+
sess.note(str::from_utf8_owned(prog.error + prog.output));
10831083
sess.abort_if_errors();
10841084
}
10851085

Diff for: src/librustc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Available lint options:
166166
max_key = num::max(name.len(), max_key);
167167
}
168168
fn padded(max: uint, s: &str) -> ~str {
169-
str::from_utf8(vec::from_elem(max - s.len(), ' ' as u8)) + s
169+
" ".repeat(max - s.len()) + s
170170
}
171171
println("\nAvailable lint checks:\n");
172172
println!(" {} {:7.7s} {}",
@@ -246,7 +246,7 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
246246
1u => {
247247
let ifile = matches.free[0].as_slice();
248248
if "-" == ifile {
249-
let src = str::from_utf8(io::stdin().read_to_end());
249+
let src = str::from_utf8_owned(io::stdin().read_to_end());
250250
str_input(src.to_managed())
251251
} else {
252252
file_input(Path::init(ifile))

Diff for: src/librustc/metadata/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,8 @@ fn family_names_type(fam: Family) -> bool {
12741274
fn read_path(d: ebml::Doc) -> (~str, uint) {
12751275
reader::with_doc_data(d, |desc| {
12761276
let pos = u64_from_be_bytes(desc, 0u, 4u) as uint;
1277-
let pathbytes = desc.slice(4u, desc.len());
1278-
let path = str::from_utf8(pathbytes);
1277+
let pathbytes = desc.slice_from(4u).to_owned();
1278+
let path = str::from_utf8_owned(pathbytes);
12791279

12801280
(path, pos)
12811281
})

Diff for: src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1902,5 +1902,5 @@ pub fn encoded_ty(tcx: ty::ctxt, t: ty::t) -> ~str {
19021902
abbrevs: tyencode::ac_no_abbrevs};
19031903
let wr = @mut MemWriter::new();
19041904
tyencode::enc_ty(wr, cx, t);
1905-
str::from_utf8(*wr.inner_ref())
1905+
str::from_utf8_owned(wr.inner_ref().to_owned())
19061906
}

Diff for: src/librustc/metadata/tydecode.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident {
9797
}
9898

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

104105
pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
@@ -492,10 +493,11 @@ fn parse_abi_set(st: &mut PState) -> AbiSet {
492493
assert_eq!(next(st), '[');
493494
let mut abis = AbiSet::empty();
494495
while peek(st) != ']' {
495-
// FIXME(#5422) str API should not force this copy
496-
let abi_str = scan(st, |c| c == ',', str::from_utf8);
497-
let abi = abi::lookup(abi_str).expect(abi_str);
498-
abis.add(abi);
496+
scan(st, |c| c == ',', |bytes| {
497+
let abi_str = str::from_utf8(bytes).to_owned();
498+
let abi = abi::lookup(abi_str).expect(abi_str);
499+
abis.add(abi);
500+
});
499501
}
500502
assert_eq!(next(st), ']');
501503
return abis;

Diff for: src/librustpkg/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'self> PkgScript<'self> {
184184
[sysroot.as_str().unwrap().to_owned(), ~"configs"]);
185185
debug!("run_custom: second pkg command did {:?}", output.status);
186186
// Run the configs() function to get the configs
187-
let cfgs = str::from_utf8_slice(output.output).words()
187+
let cfgs = str::from_utf8(output.output).words()
188188
.map(|w| w.to_owned()).collect();
189189
(cfgs, output.status)
190190
}

Diff for: src/librustpkg/tests.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~s
290290
});
291291
let output = prog.finish_with_output();
292292
debug!("Output from command {} with args {:?} was {} \\{{}\\}[{:?}]",
293-
cmd, args, str::from_utf8(output.output),
294-
str::from_utf8(output.error),
295-
output.status);
293+
cmd, args, str::from_utf8(output.output),
294+
str::from_utf8(output.error),
295+
output.status);
296296
if !output.status.success() {
297-
debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} ---",
297+
debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} {} ---",
298298
cmd, args, output.status,
299-
str::from_utf8(output.output) + str::from_utf8(output.error));
299+
str::from_utf8(output.output), str::from_utf8(output.error));
300300
Fail(output)
301301
}
302302
else {
@@ -1204,7 +1204,7 @@ fn test_info() {
12041204
let expected_info = ~"package foo"; // fill in
12051205
let workspace = create_local_package(&PkgId::new("foo"));
12061206
let output = command_line_test([~"info", ~"foo"], workspace.path());
1207-
assert_eq!(str::from_utf8(output.output), expected_info);
1207+
assert_eq!(str::from_utf8_owned(output.output), expected_info);
12081208
}
12091209
12101210
#[test]

Diff for: src/librustpkg/version.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,19 @@ pub fn try_getting_local_version(local_path: &Path) -> Option<Version> {
113113
continue;
114114
}
115115

116-
let mut output = None;
117-
let output_text = str::from_utf8(outp.output);
118-
for l in output_text.lines() {
119-
if !l.is_whitespace() {
120-
output = Some(l);
121-
}
122-
match output.and_then(try_parsing_version) {
123-
Some(v) => return Some(v),
124-
None => ()
116+
let mut output = None;
117+
let output_text = str::from_utf8(outp.output);
118+
for l in output_text.lines() {
119+
if !l.is_whitespace() {
120+
output = Some(l);
121+
}
122+
match output.and_then(try_parsing_version) {
123+
Some(v) => return Some(v),
124+
None => ()
125+
}
125126
}
126127
}
127-
}
128-
None
128+
None
129129
}
130130

131131
/// If `remote_path` refers to a git repo that can be downloaded,

Diff for: src/librustuv/file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ mod test {
487487

488488
let nread = result.unwrap();
489489
assert!(nread > 0);
490-
let read_str = str::from_utf8(read_mem.slice(0, nread as uint));
491-
assert_eq!(read_str, ~"hello");
490+
let read_str = str::from_utf8(read_mem.slice_to(nread as uint));
491+
assert_eq!(read_str, "hello");
492492
}
493493
// unlink
494494
let result = FsRequest::unlink(l(), &path_str.to_c_str());

Diff for: src/librustuv/net.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn sockaddr_to_socket_addr(addr: *sockaddr) -> SocketAddr {
8181
};
8282
port as u16
8383
};
84-
let ip_str = str::from_utf8_slice(ip_name).trim_right_chars(&'\x00');
84+
let ip_str = str::from_utf8(ip_name).trim_right_chars(&'\x00');
8585
let ip_addr = FromStr::from_str(ip_str).unwrap();
8686

8787
SocketAddr { ip: ip_addr, port: ip_port }

Diff for: src/libstd/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl CString {
167167
if self.buf.is_null() { return None; }
168168
let buf = self.as_bytes();
169169
let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
170-
str::from_utf8_slice_opt(buf)
170+
str::from_utf8_opt(buf)
171171
}
172172

173173
/// Return a CString iterator.

Diff for: src/libstd/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ impl<'self> Formatter<'self> {
802802

803803
fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) {
804804
::uint::to_str_bytes(value, 10, |buf| {
805-
let valuestr = str::from_utf8_slice(buf);
805+
let valuestr = str::from_utf8(buf);
806806
for piece in pieces.iter() {
807807
self.run(piece, Some(valuestr));
808808
}

Diff for: src/libstd/io/flate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod test {
107107
fn smoke_test() {
108108
let mem_writer = MemWriter::new();
109109
let mut deflate_writer = DeflateWriter::new(mem_writer);
110-
let in_msg = "test";
110+
let in_msg: &str = "test";
111111
let in_bytes = in_msg.as_bytes();
112112
deflate_writer.write(in_bytes);
113113
deflate_writer.flush();
@@ -118,6 +118,6 @@ mod test {
118118
let bytes_read = inflate_reader.read(out_bytes).unwrap();
119119
assert_eq!(bytes_read, in_bytes.len());
120120
let out_msg = str::from_utf8(out_bytes);
121-
assert!(in_msg == out_msg);
121+
assert_eq!(in_msg, out_msg);
122122
}
123123
}

0 commit comments

Comments
 (0)