Skip to content

Commit f6607a2

Browse files
committed
core: Add Char::len_utf16
Missing method to pair with len_utf8.
1 parent 0150fa4 commit f6607a2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/libcore/char.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ pub trait Char {
323323
/// UTF-8.
324324
fn len_utf8(&self) -> uint;
325325

326+
/// Returns the amount of bytes this character would need if encoded in
327+
/// UTF-16.
328+
fn len_utf16(&self) -> uint;
329+
326330
/// Encodes this character as UTF-8 into the provided byte buffer,
327331
/// and then returns the number of bytes written.
328332
///
@@ -363,6 +367,12 @@ impl Char for char {
363367
#[inline]
364368
fn len_utf8(&self) -> uint { len_utf8_bytes(*self) }
365369

370+
#[inline]
371+
fn len_utf16(&self) -> uint {
372+
let ch = *self as u32;
373+
if (ch & 0xFFFF_u32) == ch { 1 } else { 2 }
374+
}
375+
366376
#[inline]
367377
fn encode_utf8<'a>(&self, dst: &'a mut [u8]) -> Option<uint> {
368378
// Marked #[inline] to allow llvm optimizing it away

src/libcoretest/char.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ fn test_encode_utf16() {
197197
check('\U0001f4a9', &[0xd83d, 0xdca9]);
198198
}
199199

200+
#[test]
201+
fn test_len_utf16() {
202+
assert!('x'.len_utf16() == 1);
203+
assert!('\u00e9'.len_utf16() == 1);
204+
assert!('\ua66e'.len_utf16() == 1);
205+
assert!('\U0001f4a9'.len_utf16() == 2);
206+
}
207+
200208
#[test]
201209
fn test_width() {
202210
assert_eq!('\x00'.width(false),Some(0));

0 commit comments

Comments
 (0)