about summary refs log tree commit diff
path: root/src/libstd/str.rs
diff options
context:
space:
mode:
authorKeegan McAllister <kmcallister@mozilla.com>2014-04-11 12:49:31 -0700
committerKeegan McAllister <kmcallister@mozilla.com>2014-04-11 15:20:15 -0700
commite011939b1af554d2a29947feb66f01e27a2a1524 (patch)
tree8aafae4be0c043f239c04f247ae65473dd348d89 /src/libstd/str.rs
parent8b6091e8f1f5531fe907f84b6a2b27af04a95e8f (diff)
Implement Char::encode_utf16
And clean up encode_utf8 a bit.
Diffstat (limited to 'src/libstd/str.rs')
-rw-r--r--src/libstd/str.rs19
1 files changed, 3 insertions, 16 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 904c19b40ed..449329ce63a 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -2555,22 +2555,9 @@ impl<'a> StrSlice<'a> for &'a str {
     fn to_utf16(&self) -> ~[u16] {
         let mut u = ~[];
         for ch in self.chars() {
-            // Arithmetic with u32 literals is easier on the eyes than chars.
-            let mut ch = ch as u32;
-
-            if (ch & 0xFFFF_u32) == ch {
-                // The BMP falls through (assuming non-surrogate, as it
-                // should)
-                assert!(ch <= 0xD7FF_u32 || ch >= 0xE000_u32);
-                u.push(ch as u16)
-            } else {
-                // Supplementary planes break into surrogates.
-                assert!(ch >= 0x1_0000_u32 && ch <= 0x10_FFFF_u32);
-                ch -= 0x1_0000_u32;
-                let w1 = 0xD800_u16 | ((ch >> 10) as u16);
-                let w2 = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
-                u.push_all([w1, w2])
-            }
+            let mut buf = [0u16, ..2];
+            let n = ch.encode_utf16(buf /* as mut slice! */);
+            u.push_all(buf.slice_to(n));
         }
         u
     }