File tree Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: 0150fa4b1b3e30b1f763905bd1af2d2ccd73c47e
2
+ refs/heads/master: f6607a20c4abbd03a806c1320d059e0911dd0cdb
3
3
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4
4
refs/heads/snap-stage3: c9f6d696420107f82304b992cf623b806995fe18
5
5
refs/heads/try: 225de0d60f8ca8dcc62ab2fd8818ebbda4b58cfe
Original file line number Diff line number Diff line change @@ -323,6 +323,10 @@ pub trait Char {
323
323
/// UTF-8.
324
324
fn len_utf8 ( & self ) -> uint ;
325
325
326
+ /// Returns the amount of bytes this character would need if encoded in
327
+ /// UTF-16.
328
+ fn len_utf16 ( & self ) -> uint ;
329
+
326
330
/// Encodes this character as UTF-8 into the provided byte buffer,
327
331
/// and then returns the number of bytes written.
328
332
///
@@ -363,6 +367,12 @@ impl Char for char {
363
367
#[ inline]
364
368
fn len_utf8 ( & self ) -> uint { len_utf8_bytes ( * self ) }
365
369
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
+
366
376
#[ inline]
367
377
fn encode_utf8 < ' a > ( & self , dst : & ' a mut [ u8 ] ) -> Option < uint > {
368
378
// Marked #[inline] to allow llvm optimizing it away
Original file line number Diff line number Diff line change @@ -197,6 +197,14 @@ fn test_encode_utf16() {
197
197
check ( ' \U 0001 f4a9' , & [ 0xd83d , 0xdca9 ] ) ;
198
198
}
199
199
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 ! ( ' \U 0001 f4a9' . len_utf16( ) == 2 ) ;
206
+ }
207
+
200
208
#[ test]
201
209
fn test_width ( ) {
202
210
assert_eq ! ( '\x00' . width( false ) , Some ( 0 ) ) ;
You can’t perform that action at this time.
0 commit comments