提交 f6607a20 编写于 作者: B Brian Anderson

core: Add Char::len_utf16

Missing method to pair with len_utf8.
上级 0150fa4b
......@@ -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.
///
......@@ -363,6 +367,12 @@ fn len_utf8_bytes(&self) -> uint { len_utf8_bytes(*self) }
#[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
......
......@@ -197,6 +197,14 @@ fn check(input: char, expect: &[u16]) {
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));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册