blob: a236d310d247939111a5df2f8a8e83f002a4b9bc [file] [log] [blame]
Ivan Lozanoa7e4bc02021-08-20 09:59:16 -04001// 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
14static UTF8_CHAR_WIDTH: [u8; 256] = [
151,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
161,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
171,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
181,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
191,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
201,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
211,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
221,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
230,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
250,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
260,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
270,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
282,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
293,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
304,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]
35pub fn utf8_char_width(b: u8) -> usize {
36 return UTF8_CHAR_WIDTH[b as usize] as usize;
37}