Skip to content

Commit 9d64e46

Browse files
committed
std::str: remove from_utf8.
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.
1 parent 9635c76 commit 9d64e46

31 files changed

+119
-223
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_slice(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/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/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ fn should_sort_failures_before_printing_them() {
701701

702702
st.write_failures();
703703
let s = match st.out {
704-
Right(ref m) => str::from_utf8(*m.inner_ref()),
704+
Right(ref m) => str::from_utf8_slice(*m.inner_ref()),
705705
Left(_) => unreachable!()
706706
};
707707

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/archive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ fn run_ar(sess: Session, args: &str, cwd: Option<&Path>,
4141
let o = Process::new(ar, args.as_slice(), opts).finish_with_output();
4242
if !o.status.success() {
4343
sess.err(format!("{} failed with: {}", ar, o.status));
44-
sess.note(format!("stdout ---\n{}", str::from_utf8(o.output)));
45-
sess.note(format!("stderr ---\n{}", str::from_utf8(o.error)));
44+
sess.note(format!("stdout ---\n{}", str::from_utf8_slice(o.output)));
45+
sess.note(format!("stderr ---\n{}", str::from_utf8_slice(o.error)));
4646
sess.abort_if_errors();
4747
}
4848
o

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_slice(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_slice(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/librustc/metadata/tyencode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn enc_ty(w: @mut MemWriter, cx: @ctxt, t: ty::t) {
7272
None => {
7373
let wr = @mut MemWriter::new();
7474
enc_sty(wr, cx, &ty::get(t).sty);
75-
let s = str::from_utf8(*wr.inner_ref()).to_managed();
75+
let s = str::from_utf8_slice(*wr.inner_ref()).to_managed();
7676
cx.tcx.short_names_cache.insert(t, s);
7777
s
7878
}

Diff for: src/librustpkg/tests.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn run_git(args: &[~str], env: Option<~[(~str, ~str)]>, cwd: &Path, err_msg: &st
154154
let rslt = prog.finish_with_output();
155155
if !rslt.status.success() {
156156
fail!("{} [git returned {:?}, output = {}, error = {}]", err_msg,
157-
rslt.status, str::from_utf8(rslt.output), str::from_utf8(rslt.error));
157+
rslt.status, str::from_utf8_slice(rslt.output), str::from_utf8_slice(rslt.error));
158158
}
159159
}
160160

@@ -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_slice(output.output),
294+
str::from_utf8_slice(output.error),
295+
output.status);
296296
if !output.status.success() {
297297
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_slice(output.output) + str::from_utf8_slice(output.error));
300300
Fail(output)
301301
}
302302
else {
@@ -455,7 +455,7 @@ fn built_library_exists(repo: &Path, short_name: &str) -> bool {
455455
fn command_line_test_output(args: &[~str]) -> ~[~str] {
456456
let mut result = ~[];
457457
let p_output = command_line_test(args, &os::getcwd());
458-
let test_output = str::from_utf8(p_output.output);
458+
let test_output = str::from_utf8_slice(p_output.output);
459459
for s in test_output.split('\n') {
460460
result.push(s.to_owned());
461461
}
@@ -469,7 +469,7 @@ fn command_line_test_output_with_env(args: &[~str], env: ~[(~str, ~str)]) -> ~[~
469469
Fail(_) => fail!("Command-line test failed"),
470470
Success(r) => r
471471
};
472-
let test_output = str::from_utf8(p_output.output);
472+
let test_output = str::from_utf8_slice(p_output.output);
473473
for s in test_output.split('\n') {
474474
result.push(s.to_owned());
475475
}
@@ -1204,15 +1204,15 @@ 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]
12111211
fn test_uninstall() {
12121212
let workspace = create_local_package(&PkgId::new("foo"));
12131213
command_line_test([~"uninstall", ~"foo"], workspace.path());
12141214
let output = command_line_test([~"list"], workspace.path());
1215-
assert!(!str::from_utf8(output.output).contains("foo"));
1215+
assert!(!str::from_utf8_slice(output.output).contains("foo"));
12161216
}
12171217
12181218
#[test]
@@ -1282,8 +1282,8 @@ fn test_extern_mod() {
12821282
let outp = prog.finish_with_output();
12831283
if !outp.status.success() {
12841284
fail!("output was {}, error was {}",
1285-
str::from_utf8(outp.output),
1286-
str::from_utf8(outp.error));
1285+
str::from_utf8_slice(outp.output),
1286+
str::from_utf8_slice(outp.error));
12871287
}
12881288
assert!(exec_file.exists() && is_executable(&exec_file));
12891289
}
@@ -1337,8 +1337,8 @@ fn test_extern_mod_simpler() {
13371337
let outp = prog.finish_with_output();
13381338
if !outp.status.success() {
13391339
fail!("output was {}, error was {}",
1340-
str::from_utf8(outp.output),
1341-
str::from_utf8(outp.error));
1340+
str::from_utf8_slice(outp.output),
1341+
str::from_utf8_slice(outp.error));
13421342
}
13431343
assert!(exec_file.exists() && is_executable(&exec_file));
13441344
}
@@ -2101,7 +2101,7 @@ fn test_rustpkg_test_creates_exec() {
21012101
fn test_rustpkg_test_output() {
21022102
let workspace = create_local_package_with_test(&PkgId::new("foo"));
21032103
let output = command_line_test([~"test", ~"foo"], workspace.path());
2104-
let output_str = str::from_utf8(output.output);
2104+
let output_str = str::from_utf8_slice(output.output);
21052105
// The first two assertions are separate because test output may
21062106
// contain color codes, which could appear between "test f" and "ok".
21072107
assert!(output_str.contains("test f"));
@@ -2132,7 +2132,7 @@ fn test_rustpkg_test_cfg() {
21322132
"#[test] #[cfg(not(foobar))] fn f() { assert!('a' != 'a'); }");
21332133
let output = command_line_test([~"test", ~"--cfg", ~"foobar", ~"foo"],
21342134
foo_workspace);
2135-
let output_str = str::from_utf8(output.output);
2135+
let output_str = str::from_utf8_slice(output.output);
21362136
assert!(output_str.contains("0 passed; 0 failed; 0 ignored; 0 measured"));
21372137
}
21382138
@@ -2430,8 +2430,8 @@ fn correct_error_dependency() {
24302430
Fail(ProcessOutput{ error: error, output: output, .. }) => {
24312431
assert!(str::is_utf8(error));
24322432
assert!(str::is_utf8(output));
2433-
let error_str = str::from_utf8(error);
2434-
let out_str = str::from_utf8(output);
2433+
let error_str = str::from_utf8_slice(error);
2434+
let out_str = str::from_utf8_slice(output);
24352435
debug!("ss = {}", error_str);
24362436
debug!("out_str = {}", out_str);
24372437
if out_str.contains("Package badpkg depends on some_package_that_doesnt_exist") &&

Diff for: src/librustpkg/version.rs

+14-14
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_slice(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,
@@ -145,8 +145,8 @@ pub fn try_getting_version(remote_path: &Path) -> Option<Version> {
145145
tmp_dir.as_str().unwrap().to_owned()]);
146146
if outp.status.success() {
147147
debug!("Cloned it... ( {}, {} )",
148-
str::from_utf8(outp.output),
149-
str::from_utf8(outp.error));
148+
str::from_utf8_slice(outp.output),
149+
str::from_utf8_slice(outp.error));
150150
let mut output = None;
151151
let git_dir = tmp_dir.join(".git");
152152
debug!("(getting version, now getting tags) executing \\{git --git-dir={} tag -l\\}",
@@ -155,7 +155,7 @@ pub fn try_getting_version(remote_path: &Path) -> Option<Version> {
155155
let outp = run::process_output("git",
156156
["--git-dir=" + git_dir.as_str().unwrap(),
157157
~"tag", ~"-l"]);
158-
let output_text = str::from_utf8(outp.output);
158+
let output_text = str::from_utf8_slice(outp.output);
159159
debug!("Full output: ( {} ) [{:?}]", output_text, outp.status);
160160
for l in output_text.lines() {
161161
debug!("A line of output: {}", l);

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_slice(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());

0 commit comments

Comments
 (0)