Skip to content

Commit 6ac4fc7

Browse files
committed
Deprecate str::from_utf16
Use `String::from_utf16` instead [breaking-change]
1 parent 173baac commit 6ac4fc7

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

src/libcollections/str.rs

+9-31
Original file line numberDiff line numberDiff line change
@@ -378,32 +378,10 @@ pub fn replace(s: &str, from: &str, to: &str) -> String {
378378
Section: Misc
379379
*/
380380

381-
/// Decode a UTF-16 encoded vector `v` into a string, returning `None`
382-
/// if `v` contains any invalid data.
383-
///
384-
/// # Example
385-
///
386-
/// ```rust
387-
/// use std::str;
388-
///
389-
/// // 𝄞music
390-
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
391-
/// 0x0073, 0x0069, 0x0063];
392-
/// assert_eq!(str::from_utf16(v), Some("𝄞music".to_string()));
393-
///
394-
/// // 𝄞mu<invalid>ic
395-
/// v[4] = 0xD800;
396-
/// assert_eq!(str::from_utf16(v), None);
397-
/// ```
381+
/// Deprecated. Use `String::from_utf16`.
382+
#[deprecated = "Replaced by String::from_utf16"]
398383
pub fn from_utf16(v: &[u16]) -> Option<String> {
399-
let mut s = String::with_capacity(v.len() / 2);
400-
for c in utf16_items(v) {
401-
match c {
402-
ScalarValue(c) => s.push_char(c),
403-
LoneSurrogate(_) => return None
404-
}
405-
}
406-
Some(s)
384+
String::from_utf16(v)
407385
}
408386

409387
/// Decode a UTF-16 encoded vector `v` into a string, replacing
@@ -1722,15 +1700,15 @@ mod tests {
17221700
for p in pairs.iter() {
17231701
let (s, u) = (*p).clone();
17241702
let s_as_utf16 = s.as_slice().utf16_units().collect::<Vec<u16>>();
1725-
let u_as_string = from_utf16(u.as_slice()).unwrap();
1703+
let u_as_string = String::from_utf16(u.as_slice()).unwrap();
17261704

17271705
assert!(is_utf16(u.as_slice()));
17281706
assert_eq!(s_as_utf16, u);
17291707

17301708
assert_eq!(u_as_string, s);
17311709
assert_eq!(from_utf16_lossy(u.as_slice()), s);
17321710

1733-
assert_eq!(from_utf16(s_as_utf16.as_slice()).unwrap(), s);
1711+
assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s);
17341712
assert_eq!(u_as_string.as_slice().utf16_units().collect::<Vec<u16>>(), u);
17351713
}
17361714
}
@@ -1739,15 +1717,15 @@ mod tests {
17391717
fn test_utf16_invalid() {
17401718
// completely positive cases tested above.
17411719
// lead + eof
1742-
assert_eq!(from_utf16([0xD800]), None);
1720+
assert_eq!(String::from_utf16([0xD800]), None);
17431721
// lead + lead
1744-
assert_eq!(from_utf16([0xD800, 0xD800]), None);
1722+
assert_eq!(String::from_utf16([0xD800, 0xD800]), None);
17451723

17461724
// isolated trail
1747-
assert_eq!(from_utf16([0x0061, 0xDC00]), None);
1725+
assert_eq!(String::from_utf16([0x0061, 0xDC00]), None);
17481726

17491727
// general
1750-
assert_eq!(from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
1728+
assert_eq!(String::from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
17511729
}
17521730

17531731
#[test]

src/libcollections/string.rs

+26
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ impl String {
9191
Err(vec)
9292
}
9393
}
94+
95+
/// Decode a UTF-16 encoded vector `v` into a string, returning `None`
96+
/// if `v` contains any invalid data.
97+
///
98+
/// # Example
99+
///
100+
/// ```rust
101+
/// // 𝄞music
102+
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
103+
/// 0x0073, 0x0069, 0x0063];
104+
/// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
105+
///
106+
/// // 𝄞mu<invalid>ic
107+
/// v[4] = 0xD800;
108+
/// assert_eq!(String::from_utf16(v), None);
109+
/// ```
110+
pub fn from_utf16(v: &[u16]) -> Option<String> {
111+
let mut s = String::with_capacity(v.len() / 2);
112+
for c in str::utf16_items(v) {
113+
match c {
114+
str::ScalarValue(c) => s.push_char(c),
115+
str::LoneSurrogate(_) => return None
116+
}
117+
}
118+
Some(s)
119+
}
94120

95121
/// Convert a vector of chars to a string
96122
///

src/libnative/io/file_win32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
378378
} else {
379379
let fp_vec = vec::raw::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
380380
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec.as_slice());
381-
let fp_str = str::from_utf16(fp_trimmed)
381+
let fp_str = String::from_utf16(fp_trimmed)
382382
.expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16");
383383
paths.push(Path::new(fp_str));
384384
}

src/libstd/os.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn getcwd() -> Path {
137137
fail!();
138138
}
139139
}
140-
Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
140+
Path::new(String::from_utf16(str::truncate_utf16_at_nul(buf))
141141
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
142142
}
143143

@@ -180,7 +180,7 @@ pub mod win32 {
180180
// We want to explicitly catch the case when the
181181
// closure returned invalid UTF-16, rather than
182182
// set `res` to None and continue.
183-
let s = str::from_utf16(sub)
183+
let s = String::from_utf16(sub)
184184
.expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
185185
res = option::Some(s)
186186
}
@@ -1050,7 +1050,7 @@ pub fn error_string(errnum: uint) -> String {
10501050
return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
10511051
}
10521052

1053-
let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
1053+
let msg = String::from_utf16(str::truncate_utf16_at_nul(buf));
10541054
match msg {
10551055
Some(msg) => format!("OS Error {}: {}", errnum, msg),
10561056
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),
@@ -1207,7 +1207,7 @@ fn real_args() -> Vec<String> {
12071207

12081208
// Push it onto the list.
12091209
let opt_s = slice::raw::buf_as_slice(ptr as *const _, len, |buf| {
1210-
str::from_utf16(str::truncate_utf16_at_nul(buf))
1210+
String::from_utf16(str::truncate_utf16_at_nul(buf))
12111211
});
12121212
opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
12131213
});

0 commit comments

Comments
 (0)