about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-09-29 11:20:02 -0700
committerGitHub <noreply@github.com>2016-09-29 11:20:02 -0700
commit289f3a4ca79916d6445b452fc19a18a1e42a879a (patch)
treeff6191e3e2f803fa54875567235f301ed5a762bf /src/libstd/sys
parentff67da63ea4ca9b19e1e8ee97de002a64a2a0473 (diff)
parent13a2dd96fe824cc5d61e94ed380db0114efdd014 (diff)
downloadrust-289f3a4ca79916d6445b452fc19a18a1e42a879a.tar.gz
rust-289f3a4ca79916d6445b452fc19a18a1e42a879a.zip
Auto merge of #36377 - tormol:encode_utf, r=alexcrichton
Change encode_utf{8,16}() to write to a buffer and panic if it's too small

cc #27784

Should the "A buffer that's too small" examples be removed and replaced by tests?
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/common/wtf8.rs21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/libstd/sys/common/wtf8.rs b/src/libstd/sys/common/wtf8.rs
index 8d357aa78c9..0a94ff1e958 100644
--- a/src/libstd/sys/common/wtf8.rs
+++ b/src/libstd/sys/common/wtf8.rs
@@ -206,10 +206,12 @@ 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 bytes = unsafe {
-            char::from_u32_unchecked(code_point.value).encode_utf8()
+        let c = unsafe {
+            char::from_u32_unchecked(code_point.value)
         };
-        self.bytes.extend_from_slice(bytes.as_slice());
+        let mut bytes = [0; 4];
+        let bytes = c.encode_utf8(&mut bytes).as_bytes();
+        self.bytes.extend_from_slice(bytes)
     }
 
     #[inline]
@@ -738,15 +740,16 @@ impl<'a> Iterator for EncodeWide<'a> {
             return Some(tmp);
         }
 
+        let mut buf = [0; 2];
         self.code_points.next().map(|code_point| {
-            let n = unsafe {
-                char::from_u32_unchecked(code_point.value).encode_utf16()
+            let c = unsafe {
+                char::from_u32_unchecked(code_point.value)
             };
-            let n = n.as_slice();
-            if n.len() == 2 {
-                self.extra = n[1];
+            let n = c.encode_utf16(&mut buf).len();
+            if n == 2 {
+                self.extra = buf[1];
             }
-            n[0]
+            buf[0]
         })
     }