/// 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.
///
#[inline]
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
check('\U0001f4a9', &[0xd83d, 0xdca9]);
}
+#[test]
+fn test_len_utf16() {
+ assert!('x'.len_utf16() == 1);
+ assert!('\u00e9'.len_utf16() == 1);
+ assert!('\ua66e'.len_utf16() == 1);
+ assert!('\U0001f4a9'.len_utf16() == 2);
+}
+
#[test]
fn test_width() {
assert_eq!('\x00'.width(false),Some(0));