3 # Copyright 2011-2016 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
13 # This script uses the following Unicode tables:
17 from collections import namedtuple
22 NUM_CODEPOINTS=0x110000
27 if current is None or i != current[1] or i in (0x10000, 0x20000):
28 if current is not None:
33 if current is not None:
36 def get_escaped(codepoints):
38 if (c.class_ or "Cn") in "Cc Cf Cs Co Cn Zl Zp Zs".split() and c.value != ord(' '):
43 return open(os.path.basename(f))
44 except FileNotFoundError:
45 subprocess.run(["curl", "-O", f], check=True)
46 return open(os.path.basename(f))
48 Codepoint = namedtuple('Codepoint', 'value class_')
50 def get_codepoints(f):
51 r = csv.reader(f, delimiter=";")
55 codepoint = int(row[0], 16)
59 if class_first is not None:
60 if not name.endswith("Last>"):
61 raise ValueError("Missing Last after First")
63 for c in range(prev_codepoint + 1, codepoint):
64 yield Codepoint(c, class_first)
67 if name.endswith("First>"):
70 yield Codepoint(codepoint, class_)
71 prev_codepoint = codepoint
73 if class_first != None:
74 raise ValueError("Missing Last after First")
76 for c in range(prev_codepoint + 1, NUM_CODEPOINTS):
77 yield Codepoint(c, None)
80 file = get_file("http://www.unicode.org/Public/UNIDATA/UnicodeData.txt")
82 codepoints = get_codepoints(file)
91 for a, b in to_ranges(get_escaped(codepoints)):
93 extra.append((a, b - a))
96 singletons1.append(a & ~CUTOFF)
101 singletons1.append(a & ~CUTOFF)
102 singletons1.append((a + 1) & ~CUTOFF)
104 singletons0.append(a)
105 singletons0.append(a + 1)
108 extra.append((a, b - a))
110 normal1.append((a & ~CUTOFF, b - a))
112 normal0.append((a, b - a))
115 // Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
116 // file at the top-level directory of this distribution and at
117 // http://rust-lang.org/COPYRIGHT.
119 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
120 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
121 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
122 // option. This file may not be copied, modified, or distributed
123 // except according to those terms.
125 // NOTE: The following code was generated by "src/etc/char_private.py",
126 // do not edit directly!
130 fn check(x: u16, singletons: &[u16], normal: &[u16]) -> bool {
131 for &s in singletons {
138 for w in normal.chunks(2) {
141 let difference = (x as i32) - (start as i32);
143 if difference < len as i32 {
153 pub fn is_printable(x: char) -> bool {
155 let lower = x as u16;
157 check(lower, SINGLETONS0, NORMAL0)
158 } else if x < 0x20000 {
159 check(lower, SINGLETONS1, NORMAL1)
163 print(" if 0x{:x} <= x && x < 0x{:x} {{".format(a, a + b))
164 print(" return false;")
172 print("const SINGLETONS0: &'static [u16] = &[")
173 for s in singletons0:
174 print(" 0x{:x},".format(s))
176 print("const SINGLETONS1: &'static [u16] = &[")
177 for s in singletons1:
178 print(" 0x{:x},".format(s))
180 print("const NORMAL0: &'static [u16] = &[")
182 print(" 0x{:x}, 0x{:x},".format(a, b))
184 print("const NORMAL1: &'static [u16] = &[")
186 print(" 0x{:x}, 0x{:x},".format(a, b))
189 if __name__ == '__main__':