Skip to content

Commit

Permalink
auto merge of rust-lang#12317 : huonw/rust/utf16, r=alexcrichton
Browse files Browse the repository at this point in the history
Iterators! Use them (in `is_utf16`), create them (in `utf16_items`).

Handle errors gracefully (`from_utf16_lossy`) and `from_utf16` returning `Option<~str>` instead of failing.

Add a pile of tests.
  • Loading branch information
bors committed Feb 19, 2014
2 parents a25f447 + c9b4538 commit cae5999
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 51 deletions.
4 changes: 3 additions & 1 deletion src/libnative/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
else {
let fp_vec = vec::from_buf(
fp_buf, wcslen(fp_buf) as uint);
let fp_str = str::from_utf16(fp_vec);
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec);
let fp_str = str::from_utf16(fp_trimmed)
.expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16");
paths.push(Path::new(fp_str));
}
more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE);
Expand Down
19 changes: 14 additions & 5 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub fn getcwd() -> Path {
fail!();
}
}
Path::new(str::from_utf16(buf))
Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
}

#[cfg(windows)]
Expand Down Expand Up @@ -124,7 +125,12 @@ pub mod win32 {
}
if k != 0 && done {
let sub = buf.slice(0, k as uint);
res = option::Some(str::from_utf16(sub));
// We want to explicitly catch the case when the
// closure returned invalid UTF-16, rather than
// set `res` to None and continue.
let s = str::from_utf16(sub)
.expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
res = option::Some(s)
}
}
return res;
Expand Down Expand Up @@ -739,7 +745,8 @@ pub fn last_os_error() -> ~str {
fail!("[{}] FormatMessage failure", errno());
}

str::from_utf16(buf)
str::from_utf16(str::truncate_utf16_at_nul(buf))
.expect("FormatMessageW returned invalid UTF-16")
}
}

Expand Down Expand Up @@ -828,8 +835,10 @@ fn real_args() -> ~[~str] {
while *ptr.offset(len as int) != 0 { len += 1; }

// Push it onto the list.
args.push(vec::raw::buf_as_slice(ptr, len,
str::from_utf16));
let opt_s = vec::raw::buf_as_slice(ptr, len, |buf| {
str::from_utf16(str::truncate_utf16_at_nul(buf))
});
args.push(opt_s.expect("CommandLineToArgvW returned invalid UTF-16"));
}
}

Expand Down
Loading

0 comments on commit cae5999

Please sign in to comment.