Skip to content

Commit 558e706

Browse files
committed
---
yaml --- r: 160471 b: refs/heads/master c: f6607a2 h: refs/heads/master i: 160469: d2396fe 160467: d97dcbc 160463: 7ba3e70 v: v3
1 parent 7d26d54 commit 558e706

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 0150fa4b1b3e30b1f763905bd1af2d2ccd73c47e
2+
refs/heads/master: f6607a20c4abbd03a806c1320d059e0911dd0cdb
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: c9f6d696420107f82304b992cf623b806995fe18
55
refs/heads/try: 225de0d60f8ca8dcc62ab2fd8818ebbda4b58cfe

trunk/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

trunk/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)