From 48d5fe9ec560b53b1f5069219b0d62015e1de5ba Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 11 Mar 2016 11:01:46 -0800 Subject: std: Change `encode_utf{8,16}` to return iterators Currently these have non-traditional APIs which take a buffer and report how much was filled in, but they're not necessarily ergonomic to use. Returning an iterator which *also* exposes an underlying slice shouldn't result in any performance loss as it's just a lazy version of the same implementation, and it's also much more ergonomic! cc #27784 --- src/libstd/sys/common/wtf8.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/common/wtf8.rs b/src/libstd/sys/common/wtf8.rs index 48e9adb9296..db3bc2ed751 100644 --- a/src/libstd/sys/common/wtf8.rs +++ b/src/libstd/sys/common/wtf8.rs @@ -25,7 +25,6 @@ // unix (it's mostly used on windows), so don't worry about dead code here. #![allow(dead_code)] -use core::char::{encode_utf8_raw, encode_utf16_raw}; use core::str::next_code_point; use ascii::*; @@ -206,19 +205,10 @@ impl Wtf8Buf { /// Copied from String::push /// This does **not** include the WTF-8 concatenation check. fn push_code_point_unchecked(&mut self, code_point: CodePoint) { - let cur_len = self.len(); - // This may use up to 4 bytes. - self.reserve(4); - - unsafe { - // Attempt to not use an intermediate buffer by just pushing bytes - // directly onto this string. - let slice = slice::from_raw_parts_mut( - self.bytes.as_mut_ptr().offset(cur_len as isize), 4 - ); - let used = encode_utf8_raw(code_point.value, slice).unwrap(); - self.bytes.set_len(cur_len + used); - } + let bytes = unsafe { + char::from_u32_unchecked(code_point.value).encode_utf8() + }; + self.bytes.extend_from_slice(bytes.as_slice()); } #[inline] @@ -747,12 +737,15 @@ impl<'a> Iterator for EncodeWide<'a> { return Some(tmp); } - let mut buf = [0; 2]; self.code_points.next().map(|code_point| { - let n = encode_utf16_raw(code_point.value, &mut buf) - .unwrap_or(0); - if n == 2 { self.extra = buf[1]; } - buf[0] + let n = unsafe { + char::from_u32_unchecked(code_point.value).encode_utf16() + }; + let n = n.as_slice(); + if n.len() == 2 { + self.extra = n[1]; + } + n[0] }) } -- cgit 1.4.1-3-g733a5