]> git.lizzy.rs Git - rust.git/blob - src/libstd_unicode/unicode.py
Move unicode Python script into libstd_unicode crate.
[rust.git] / src / libstd_unicode / unicode.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2011-2013 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.
6 #
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.
12
13 # This script uses the following Unicode tables:
14 # - DerivedCoreProperties.txt
15 # - DerivedNormalizationProps.txt
16 # - EastAsianWidth.txt
17 # - auxiliary/GraphemeBreakProperty.txt
18 # - PropList.txt
19 # - ReadMe.txt
20 # - Scripts.txt
21 # - UnicodeData.txt
22 #
23 # Since this should not require frequent updates, we just store this
24 # out-of-line and check the unicode.rs file into git.
25
26 import fileinput, re, os, sys, operator, math
27
28 preamble = '''// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
29 // file at the top-level directory of this distribution and at
30 // http://rust-lang.org/COPYRIGHT.
31 //
32 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
33 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
34 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
35 // option. This file may not be copied, modified, or distributed
36 // except according to those terms.
37
38 // NOTE: The following code was generated by "./unicode.py", do not edit directly
39
40 #![allow(missing_docs, non_upper_case_globals, non_snake_case)]
41 '''
42
43 # Mapping taken from Table 12 from:
44 # http://www.unicode.org/reports/tr44/#General_Category_Values
45 expanded_categories = {
46     'Lu': ['LC', 'L'], 'Ll': ['LC', 'L'], 'Lt': ['LC', 'L'],
47     'Lm': ['L'], 'Lo': ['L'],
48     'Mn': ['M'], 'Mc': ['M'], 'Me': ['M'],
49     'Nd': ['N'], 'Nl': ['N'], 'No': ['No'],
50     'Pc': ['P'], 'Pd': ['P'], 'Ps': ['P'], 'Pe': ['P'],
51     'Pi': ['P'], 'Pf': ['P'], 'Po': ['P'],
52     'Sm': ['S'], 'Sc': ['S'], 'Sk': ['S'], 'So': ['S'],
53     'Zs': ['Z'], 'Zl': ['Z'], 'Zp': ['Z'],
54     'Cc': ['C'], 'Cf': ['C'], 'Cs': ['C'], 'Co': ['C'], 'Cn': ['C'],
55 }
56
57 # these are the surrogate codepoints, which are not valid rust characters
58 surrogate_codepoints = (0xd800, 0xdfff)
59
60 def fetch(f):
61     if not os.path.exists(os.path.basename(f)):
62         os.system("curl -O http://www.unicode.org/Public/UNIDATA/%s"
63                   % f)
64
65     if not os.path.exists(os.path.basename(f)):
66         sys.stderr.write("cannot load %s" % f)
67         exit(1)
68
69 def is_surrogate(n):
70     return surrogate_codepoints[0] <= n <= surrogate_codepoints[1]
71
72 def load_unicode_data(f):
73     fetch(f)
74     gencats = {}
75     to_lower = {}
76     to_upper = {}
77     to_title = {}
78     combines = {}
79     canon_decomp = {}
80     compat_decomp = {}
81
82     udict = {}
83     range_start = -1
84     for line in fileinput.input(f):
85         data = line.split(';')
86         if len(data) != 15:
87             continue
88         cp = int(data[0], 16)
89         if is_surrogate(cp):
90             continue
91         if range_start >= 0:
92             for i in xrange(range_start, cp):
93                 udict[i] = data
94             range_start = -1
95         if data[1].endswith(", First>"):
96             range_start = cp
97             continue
98         udict[cp] = data
99
100     for code in udict:
101         (code_org, name, gencat, combine, bidi,
102          decomp, deci, digit, num, mirror,
103          old, iso, upcase, lowcase, titlecase) = udict[code]
104
105         # generate char to char direct common and simple conversions
106         # uppercase to lowercase
107         if lowcase != "" and code_org != lowcase:
108             to_lower[code] = (int(lowcase, 16), 0, 0)
109
110         # lowercase to uppercase
111         if upcase != "" and code_org != upcase:
112             to_upper[code] = (int(upcase, 16), 0, 0)
113
114         # title case
115         if titlecase.strip() != "" and code_org != titlecase:
116             to_title[code] = (int(titlecase, 16), 0, 0)
117
118         # store decomposition, if given
119         if decomp != "":
120             if decomp.startswith('<'):
121                 seq = []
122                 for i in decomp.split()[1:]:
123                     seq.append(int(i, 16))
124                 compat_decomp[code] = seq
125             else:
126                 seq = []
127                 for i in decomp.split():
128                     seq.append(int(i, 16))
129                 canon_decomp[code] = seq
130
131         # place letter in categories as appropriate
132         for cat in [gencat, "Assigned"] + expanded_categories.get(gencat, []):
133             if cat not in gencats:
134                 gencats[cat] = []
135             gencats[cat].append(code)
136
137         # record combining class, if any
138         if combine != "0":
139             if combine not in combines:
140                 combines[combine] = []
141             combines[combine].append(code)
142
143     # generate Not_Assigned from Assigned
144     gencats["Cn"] = gen_unassigned(gencats["Assigned"])
145     # Assigned is not a real category
146     del(gencats["Assigned"])
147     # Other contains Not_Assigned
148     gencats["C"].extend(gencats["Cn"])
149     gencats = group_cats(gencats)
150     combines = to_combines(group_cats(combines))
151
152     return (canon_decomp, compat_decomp, gencats, combines, to_upper, to_lower, to_title)
153
154 def load_special_casing(f, to_upper, to_lower, to_title):
155     fetch(f)
156     for line in fileinput.input(f):
157         data = line.split('#')[0].split(';')
158         if len(data) == 5:
159             code, lower, title, upper, _comment = data
160         elif len(data) == 6:
161             code, lower, title, upper, condition, _comment = data
162             if condition.strip():  # Only keep unconditional mappins
163                 continue
164         else:
165             continue
166         code = code.strip()
167         lower = lower.strip()
168         title = title.strip()
169         upper = upper.strip()
170         key = int(code, 16)
171         for (map_, values) in [(to_lower, lower), (to_upper, upper), (to_title, title)]:
172             if values != code:
173                 values = [int(i, 16) for i in values.split()]
174                 for _ in range(len(values), 3):
175                     values.append(0)
176                 assert len(values) == 3
177                 map_[key] = values
178
179 def group_cats(cats):
180     cats_out = {}
181     for cat in cats:
182         cats_out[cat] = group_cat(cats[cat])
183     return cats_out
184
185 def group_cat(cat):
186     cat_out = []
187     letters = sorted(set(cat))
188     cur_start = letters.pop(0)
189     cur_end = cur_start
190     for letter in letters:
191         assert letter > cur_end, \
192             "cur_end: %s, letter: %s" % (hex(cur_end), hex(letter))
193         if letter == cur_end + 1:
194             cur_end = letter
195         else:
196             cat_out.append((cur_start, cur_end))
197             cur_start = cur_end = letter
198     cat_out.append((cur_start, cur_end))
199     return cat_out
200
201 def ungroup_cat(cat):
202     cat_out = []
203     for (lo, hi) in cat:
204         while lo <= hi:
205             cat_out.append(lo)
206             lo += 1
207     return cat_out
208
209 def gen_unassigned(assigned):
210     assigned = set(assigned)
211     return ([i for i in range(0, 0xd800) if i not in assigned] +
212             [i for i in range(0xe000, 0x110000) if i not in assigned])
213
214 def to_combines(combs):
215     combs_out = []
216     for comb in combs:
217         for (lo, hi) in combs[comb]:
218             combs_out.append((lo, hi, comb))
219     combs_out.sort(key=lambda comb: comb[0])
220     return combs_out
221
222 def format_table_content(f, content, indent):
223     line = " "*indent
224     first = True
225     for chunk in content.split(","):
226         if len(line) + len(chunk) < 98:
227             if first:
228                 line += chunk
229             else:
230                 line += ", " + chunk
231             first = False
232         else:
233             f.write(line + ",\n")
234             line = " "*indent + chunk
235     f.write(line)
236
237 def load_properties(f, interestingprops):
238     fetch(f)
239     props = {}
240     re1 = re.compile("^ *([0-9A-F]+) *; *(\w+)")
241     re2 = re.compile("^ *([0-9A-F]+)\.\.([0-9A-F]+) *; *(\w+)")
242
243     for line in fileinput.input(os.path.basename(f)):
244         prop = None
245         d_lo = 0
246         d_hi = 0
247         m = re1.match(line)
248         if m:
249             d_lo = m.group(1)
250             d_hi = m.group(1)
251             prop = m.group(2)
252         else:
253             m = re2.match(line)
254             if m:
255                 d_lo = m.group(1)
256                 d_hi = m.group(2)
257                 prop = m.group(3)
258             else:
259                 continue
260         if interestingprops and prop not in interestingprops:
261             continue
262         d_lo = int(d_lo, 16)
263         d_hi = int(d_hi, 16)
264         if prop not in props:
265             props[prop] = []
266         props[prop].append((d_lo, d_hi))
267
268     # optimize if possible
269     for prop in props:
270         props[prop] = group_cat(ungroup_cat(props[prop]))
271
272     return props
273
274 def escape_char(c):
275     return "'\\u{%x}'" % c if c != 0 else "'\\0'"
276
277 def emit_bsearch_range_table(f):
278     f.write("""
279 fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
280     use core::cmp::Ordering::{Equal, Less, Greater};
281     r.binary_search_by(|&(lo, hi)| {
282          if c < lo {
283              Greater
284          } else if hi < c {
285              Less
286          } else {
287              Equal
288          }
289      })
290      .is_ok()
291 }\n
292 """)
293
294 def emit_table(f, name, t_data, t_type = "&'static [(char, char)]", is_pub=True,
295         pfun=lambda x: "(%s,%s)" % (escape_char(x[0]), escape_char(x[1]))):
296     pub_string = ""
297     if is_pub:
298         pub_string = "pub "
299     f.write("    %sconst %s: %s = &[\n" % (pub_string, name, t_type))
300     data = ""
301     first = True
302     for dat in t_data:
303         if not first:
304             data += ","
305         first = False
306         data += pfun(dat)
307     format_table_content(f, data, 8)
308     f.write("\n    ];\n\n")
309
310 def emit_trie_lookup_range_table(f):
311     f.write("""
312
313 // BoolTrie is a trie for representing a set of Unicode codepoints. It is
314 // implemented with postfix compression (sharing of identical child nodes),
315 // which gives both compact size and fast lookup.
316 //
317 // The space of Unicode codepoints is divided into 3 subareas, each
318 // represented by a trie with different depth. In the first (0..0x800), there
319 // is no trie structure at all; each u64 entry corresponds to a bitvector
320 // effectively holding 64 bool values.
321 //
322 // In the second (0x800..0x10000), each child of the root node represents a
323 // 64-wide subrange, but instead of storing the full 64-bit value of the leaf,
324 // the trie stores an 8-bit index into a shared table of leaf values. This
325 // exploits the fact that in reasonable sets, many such leaves can be shared.
326 //
327 // In the third (0x10000..0x110000), each child of the root node represents a
328 // 4096-wide subrange, and the trie stores an 8-bit index into a 64-byte slice
329 // of a child tree. Each of these 64 bytes represents an index into the table
330 // of shared 64-bit leaf values. This exploits the sparse structure in the
331 // non-BMP range of most Unicode sets.
332 pub struct BoolTrie {
333     // 0..0x800 (corresponding to 1 and 2 byte utf-8 sequences)
334     r1: [u64; 32],   // leaves
335
336     // 0x800..0x10000 (corresponding to 3 byte utf-8 sequences)
337     r2: [u8; 992],      // first level
338     r3: &'static [u64],  // leaves
339
340     // 0x10000..0x110000 (corresponding to 4 byte utf-8 sequences)
341     r4: [u8; 256],       // first level
342     r5: &'static [u8],   // second level
343     r6: &'static [u64],  // leaves
344 }
345
346 fn trie_range_leaf(c: usize, bitmap_chunk: u64) -> bool {
347     ((bitmap_chunk >> (c & 63)) & 1) != 0
348 }
349
350 fn trie_lookup_range_table(c: char, r: &'static BoolTrie) -> bool {
351     let c = c as usize;
352     if c < 0x800 {
353         trie_range_leaf(c, r.r1[c >> 6])
354     } else if c < 0x10000 {
355         let child = r.r2[(c >> 6) - 0x20];
356         trie_range_leaf(c, r.r3[child as usize])
357     } else {
358         let child = r.r4[(c >> 12) - 0x10];
359         let leaf = r.r5[((child as usize) << 6) + ((c >> 6) & 0x3f)];
360         trie_range_leaf(c, r.r6[leaf as usize])
361     }
362 }
363
364 pub struct SmallBoolTrie {
365     r1: &'static [u8],  // first level
366     r2: &'static [u64],  // leaves
367 }
368
369 impl SmallBoolTrie {
370     fn lookup(&self, c: char) -> bool {
371         let c = c as usize;
372         match self.r1.get(c >> 6) {
373             Some(&child) => trie_range_leaf(c, self.r2[child as usize]),
374             None => false,
375         }
376     }
377 }
378
379 """)
380
381 def compute_trie(rawdata, chunksize):
382     root = []
383     childmap = {}
384     child_data = []
385     for i in range(len(rawdata) / chunksize):
386         data = rawdata[i * chunksize: (i + 1) * chunksize]
387         child = '|'.join(map(str, data))
388         if child not in childmap:
389             childmap[child] = len(childmap)
390             child_data.extend(data)
391         root.append(childmap[child])
392     return (root, child_data)
393
394 def emit_bool_trie(f, name, t_data, is_pub=True):
395     CHUNK = 64
396     rawdata = [False] * 0x110000
397     for (lo, hi) in t_data:
398         for cp in range(lo, hi + 1):
399             rawdata[cp] = True
400
401     # convert to bitmap chunks of 64 bits each
402     chunks = []
403     for i in range(0x110000 / CHUNK):
404         chunk = 0
405         for j in range(64):
406             if rawdata[i * 64 + j]:
407                 chunk |= 1 << j
408         chunks.append(chunk)
409
410     pub_string = ""
411     if is_pub:
412         pub_string = "pub "
413     f.write("    %sconst %s: &'static super::BoolTrie = &super::BoolTrie {\n" % (pub_string, name))
414     f.write("        r1: [\n")
415     data = ','.join('0x%016x' % chunk for chunk in chunks[0:0x800 / CHUNK])
416     format_table_content(f, data, 12)
417     f.write("\n        ],\n")
418
419     # 0x800..0x10000 trie
420     (r2, r3) = compute_trie(chunks[0x800 / CHUNK : 0x10000 / CHUNK], 64 / CHUNK)
421     f.write("        r2: [\n")
422     data = ','.join(str(node) for node in r2)
423     format_table_content(f, data, 12)
424     f.write("\n        ],\n")
425     f.write("        r3: &[\n")
426     data = ','.join('0x%016x' % chunk for chunk in r3)
427     format_table_content(f, data, 12)
428     f.write("\n        ],\n")
429
430     # 0x10000..0x110000 trie
431     (mid, r6) = compute_trie(chunks[0x10000 / CHUNK : 0x110000 / CHUNK], 64 / CHUNK)
432     (r4, r5) = compute_trie(mid, 64)
433     f.write("        r4: [\n")
434     data = ','.join(str(node) for node in r4)
435     format_table_content(f, data, 12)
436     f.write("\n        ],\n")
437     f.write("        r5: &[\n")
438     data = ','.join(str(node) for node in r5)
439     format_table_content(f, data, 12)
440     f.write("\n        ],\n")
441     f.write("        r6: &[\n")
442     data = ','.join('0x%016x' % chunk for chunk in r6)
443     format_table_content(f, data, 12)
444     f.write("\n        ],\n")
445
446     f.write("    };\n\n")
447
448 def emit_small_bool_trie(f, name, t_data, is_pub=True):
449     last_chunk = max(int(hi / 64) for (lo, hi) in t_data)
450     n_chunks = last_chunk + 1
451     chunks = [0] * n_chunks
452     for (lo, hi) in t_data:
453         for cp in range(lo, hi + 1):
454             if int(cp / 64) >= len(chunks):
455                 print(cp, int(cp / 64), len(chunks), lo, hi)
456             chunks[int(cp / 64)] |= 1 << (cp & 63)
457
458     pub_string = ""
459     if is_pub:
460         pub_string = "pub "
461     f.write("    %sconst %s: &'static super::SmallBoolTrie = &super::SmallBoolTrie {\n"
462             % (pub_string, name))
463
464     (r1, r2) = compute_trie(chunks, 1)
465
466     f.write("        r1: &[\n")
467     data = ','.join(str(node) for node in r1)
468     format_table_content(f, data, 12)
469     f.write("\n        ],\n")
470
471     f.write("        r2: &[\n")
472     data = ','.join('0x%016x' % node for node in r2)
473     format_table_content(f, data, 12)
474     f.write("\n        ],\n")
475
476     f.write("    };\n\n")
477
478 def emit_property_module(f, mod, tbl, emit):
479     f.write("pub mod %s {\n" % mod)
480     for cat in sorted(emit):
481         if cat in ["Cc", "White_Space", "Pattern_White_Space"]:
482             emit_small_bool_trie(f, "%s_table" % cat, tbl[cat])
483             f.write("    pub fn %s(c: char) -> bool {\n" % cat)
484             f.write("        %s_table.lookup(c)\n" % cat)
485             f.write("    }\n\n")
486         else:
487             emit_bool_trie(f, "%s_table" % cat, tbl[cat])
488             f.write("    pub fn %s(c: char) -> bool {\n" % cat)
489             f.write("        super::trie_lookup_range_table(c, %s_table)\n" % cat)
490             f.write("    }\n\n")
491     f.write("}\n\n")
492
493 def emit_conversions_module(f, to_upper, to_lower, to_title):
494     f.write("pub mod conversions {")
495     f.write("""
496     use core::option::Option;
497     use core::option::Option::{Some, None};
498
499     pub fn to_lower(c: char) -> [char; 3] {
500         match bsearch_case_table(c, to_lowercase_table) {
501             None        => [c, '\\0', '\\0'],
502             Some(index) => to_lowercase_table[index].1,
503         }
504     }
505
506     pub fn to_upper(c: char) -> [char; 3] {
507         match bsearch_case_table(c, to_uppercase_table) {
508             None        => [c, '\\0', '\\0'],
509             Some(index) => to_uppercase_table[index].1,
510         }
511     }
512
513     fn bsearch_case_table(c: char, table: &'static [(char, [char; 3])]) -> Option<usize> {
514         table.binary_search_by(|&(key, _)| key.cmp(&c)).ok()
515     }
516
517 """)
518     t_type = "&'static [(char, [char; 3])]"
519     pfun = lambda x: "(%s,[%s,%s,%s])" % (
520         escape_char(x[0]), escape_char(x[1][0]), escape_char(x[1][1]), escape_char(x[1][2]))
521     emit_table(f, "to_lowercase_table",
522         sorted(to_lower.iteritems(), key=operator.itemgetter(0)),
523         is_pub=False, t_type = t_type, pfun=pfun)
524     emit_table(f, "to_uppercase_table",
525         sorted(to_upper.iteritems(), key=operator.itemgetter(0)),
526         is_pub=False, t_type = t_type, pfun=pfun)
527     f.write("}\n\n")
528
529 def emit_norm_module(f, canon, compat, combine, norm_props):
530     canon_keys = canon.keys()
531     canon_keys.sort()
532
533     compat_keys = compat.keys()
534     compat_keys.sort()
535
536     canon_comp = {}
537     comp_exclusions = norm_props["Full_Composition_Exclusion"]
538     for char in canon_keys:
539         if True in map(lambda (lo, hi): lo <= char <= hi, comp_exclusions):
540             continue
541         decomp = canon[char]
542         if len(decomp) == 2:
543             if not canon_comp.has_key(decomp[0]):
544                 canon_comp[decomp[0]] = []
545             canon_comp[decomp[0]].append( (decomp[1], char) )
546     canon_comp_keys = canon_comp.keys()
547     canon_comp_keys.sort()
548
549 if __name__ == "__main__":
550     r = "tables.rs"
551     if os.path.exists(r):
552         os.remove(r)
553     with open(r, "w") as rf:
554         # write the file's preamble
555         rf.write(preamble)
556
557         # download and parse all the data
558         fetch("ReadMe.txt")
559         with open("ReadMe.txt") as readme:
560             pattern = "for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
561             unicode_version = re.search(pattern, readme.read()).groups()
562         rf.write("""
563 /// The version of [Unicode](http://www.unicode.org/)
564 /// that the unicode parts of `CharExt` and `UnicodeStrPrelude` traits are based on.
565 pub const UNICODE_VERSION: (u64, u64, u64) = (%s, %s, %s);
566 """ % unicode_version)
567         (canon_decomp, compat_decomp, gencats, combines,
568                 to_upper, to_lower, to_title) = load_unicode_data("UnicodeData.txt")
569         load_special_casing("SpecialCasing.txt", to_upper, to_lower, to_title)
570         want_derived = ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase",
571                         "Cased", "Case_Ignorable"]
572         derived = load_properties("DerivedCoreProperties.txt", want_derived)
573         scripts = load_properties("Scripts.txt", [])
574         props = load_properties("PropList.txt",
575                 ["White_Space", "Join_Control", "Noncharacter_Code_Point", "Pattern_White_Space"])
576         norm_props = load_properties("DerivedNormalizationProps.txt",
577                      ["Full_Composition_Exclusion"])
578
579         # trie_lookup_table is used in all the property modules below
580         emit_trie_lookup_range_table(rf)
581         # emit_bsearch_range_table(rf)
582
583         # category tables
584         for (name, cat, pfuns) in ("general_category", gencats, ["N", "Cc"]), \
585                                   ("derived_property", derived, want_derived), \
586                                   ("property", props, ["White_Space", "Pattern_White_Space"]):
587             emit_property_module(rf, name, cat, pfuns)
588
589         # normalizations and conversions module
590         emit_norm_module(rf, canon_decomp, compat_decomp, combines, norm_props)
591         emit_conversions_module(rf, to_upper, to_lower, to_title)