diff options
| author | Brian Anderson <banderson@mozilla.com> | 2014-10-08 17:40:31 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2014-11-21 13:17:09 -0800 |
| commit | f6607a20c4abbd03a806c1320d059e0911dd0cdb (patch) | |
| tree | 610597e4174dbdbfe528da61f02e56f5c212b959 | |
| parent | 0150fa4b1b3e30b1f763905bd1af2d2ccd73c47e (diff) | |
| download | rust-f6607a20c4abbd03a806c1320d059e0911dd0cdb.tar.gz rust-f6607a20c4abbd03a806c1320d059e0911dd0cdb.zip | |
core: Add Char::len_utf16
Missing method to pair with len_utf8.
| -rw-r--r-- | src/libcore/char.rs | 10 | ||||
| -rw-r--r-- | src/libcoretest/char.rs | 8 |
2 files changed, 18 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; diff --git a/src/libcoretest/char.rs b/src/libcoretest/char.rs index 2d5ca983fec..507ddf65e55 100644 --- a/src/libcoretest/char.rs +++ b/src/libcoretest/char.rs @@ -198,6 +198,14 @@ fn test_encode_utf16() { } #[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)); assert_eq!('\x00'.width(true),Some(0)); |
