about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2014-10-08 17:40:31 -0700
committerBrian Anderson <banderson@mozilla.com>2014-11-21 13:17:09 -0800
commitf6607a20c4abbd03a806c1320d059e0911dd0cdb (patch)
tree610597e4174dbdbfe528da61f02e56f5c212b959 /src/libcore
parent0150fa4b1b3e30b1f763905bd1af2d2ccd73c47e (diff)
core: Add Char::len_utf16
Missing method to pair with len_utf8.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/char.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs
index 82dc2becf28..93fa614e597 100644
--- a/src/libcore/char.rs
+++ b/src/libcore/char.rs
@@ -323,6 +323,10 @@ pub trait Char {
     /// UTF-8.
     fn len_utf8(&self) -> uint;
 
+    /// Returns the amount of bytes this character would need if encoded in
+    /// UTF-16.
+    fn len_utf16(&self) -> uint;
+
     /// Encodes this character as UTF-8 into the provided byte buffer,
     /// and then returns the number of bytes written.
     ///
@@ -364,6 +368,12 @@ impl Char for char {
     fn len_utf8(&self) -> uint { len_utf8_bytes(*self) }
 
     #[inline]
+    fn len_utf16(&self) -> uint {
+        let ch = *self as u32;
+        if (ch & 0xFFFF_u32) == ch { 1 } else { 2 }
+    }
+
+    #[inline]
     fn encode_utf8<'a>(&self, dst: &'a mut [u8]) -> Option<uint> {
         // Marked #[inline] to allow llvm optimizing it away
         let code = *self as u32;