1  // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2  // file at http://rust-lang.org/COPYRIGHT.
3  //
4  // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5  // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6  // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7  // option. This file may not be copied, modified, or distributed
8  // except according to those terms.
9  
10  // (This used to be in the Rust unicode crate, which is now gone, so we'll
11  // just include it inline.)
12  
13  // https://tools.ietf.org/html/rfc3629
14  static UTF8_CHAR_WIDTH: [u8; 256] = [
15  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
16  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
17  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
18  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
19  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
20  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
21  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
22  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
23  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
24  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
25  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
26  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
27  0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
28  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
29  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
30  4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
31  ];
32  
33  /// Given a first byte, determine how many bytes are in this UTF-8 character
34  #[inline]
utf8_char_width(b: u8) -> usize35  pub fn utf8_char_width(b: u8) -> usize {
36      return UTF8_CHAR_WIDTH[b as usize] as usize;
37  }
38