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.
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:
14 # - DerivedCoreProperties.txt
15 # - EastAsianWidth.txt
20 # Since this should not require frequent updates, we just store this
21 # out-of-line and check the unicode.rs file into git.
23 import fileinput, re, os, sys, operator
25 preamble = '''// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
26 // file at the top-level directory of this distribution and at
27 // http://rust-lang.org/COPYRIGHT.
29 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
30 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
31 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
32 // option. This file may not be copied, modified, or distributed
33 // except according to those terms.
35 // NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
37 #![allow(missing_docs, non_upper_case_globals, non_snake_case)]
40 # Mapping taken from Table 12 from:
41 # http://www.unicode.org/reports/tr44/#General_Category_Values
42 expanded_categories = {
43 'Lu': ['LC', 'L'], 'Ll': ['LC', 'L'], 'Lt': ['LC', 'L'],
44 'Lm': ['L'], 'Lo': ['L'],
45 'Mn': ['M'], 'Mc': ['M'], 'Me': ['M'],
46 'Nd': ['N'], 'Nl': ['N'], 'No': ['No'],
47 'Pc': ['P'], 'Pd': ['P'], 'Ps': ['P'], 'Pe': ['P'],
48 'Pi': ['P'], 'Pf': ['P'], 'Po': ['P'],
49 'Sm': ['S'], 'Sc': ['S'], 'Sk': ['S'], 'So': ['S'],
50 'Zs': ['Z'], 'Zl': ['Z'], 'Zp': ['Z'],
51 'Cc': ['C'], 'Cf': ['C'], 'Cs': ['C'], 'Co': ['C'], 'Cn': ['C'],
55 # Grapheme cluster data
56 # taken from UAX29, http://www.unicode.org/reports/tr29/
57 # these code points are excluded from the Control category
58 # NOTE: CR and LF are also technically excluded, but for
59 # the sake of convenience we leave them in the Control group
60 # and manually check them in the appropriate place. This is
61 # still compliant with the implementation requirements.
62 grapheme_control_exceptions = set([0x200c, 0x200d])
64 # the Regional_Indicator category
65 grapheme_regional_indicator = [(0x1f1e6, 0x1f1ff)]
67 # "The following ... are specifically excluded" from the SpacingMark category
68 # http://www.unicode.org/reports/tr29/#SpacingMark
69 grapheme_spacingmark_exceptions = [(0x102b, 0x102c), (0x1038, 0x1038),
70 (0x1062, 0x1064), (0x1067, 0x106d), (0x1083, 0x1083), (0x1087, 0x108c),
71 (0x108f, 0x108f), (0x109a, 0x109c), (0x19b0, 0x19b4), (0x19b8, 0x19b9),
72 (0x19bb, 0x19c0), (0x19c8, 0x19c9), (0x1a61, 0x1a61), (0x1a63, 0x1a64),
73 (0xaa7b, 0xaa7b), (0xaa7d, 0xaa7d)]
75 # these are included in the SpacingMark category
76 grapheme_spacingmark_extra = set([0xe33, 0xeb3])
79 if not os.path.exists(f):
80 os.system("curl -O http://www.unicode.org/Public/UNIDATA/%s"
83 if not os.path.exists(f):
84 sys.stderr.write("cannot load %s" % f)
87 def is_valid_unicode(n):
88 return 0 <= n <= 0xD7FF or 0xE000 <= n <= 0x10FFFF
90 def load_unicode_data(f):
99 for line in fileinput.input(f):
100 fields = line.split(";")
101 if len(fields) != 15:
103 [code, name, gencat, combine, bidi,
104 decomp, deci, digit, num, mirror,
105 old, iso, upcase, lowcase, titlecase ] = fields
110 if not is_valid_unicode(code):
113 # generate char to char direct common and simple conversions
114 # uppercase to lowercase
115 if gencat == "Lu" and lowcase != "" and code_org != lowcase:
116 upperlower[code] = int(lowcase, 16)
118 # lowercase to uppercase
119 if gencat == "Ll" and upcase != "" and code_org != upcase:
120 lowerupper[code] = int(upcase, 16)
122 # store decomposition, if given
124 if decomp.startswith('<'):
126 for i in decomp.split()[1:]:
127 seq.append(int(i, 16))
128 compat_decomp[code] = seq
131 for i in decomp.split():
132 seq.append(int(i, 16))
133 canon_decomp[code] = seq
135 # place letter in categories as appropriate
136 for cat in [gencat, "Assigned"] + expanded_categories.get(gencat, []):
137 if cat not in gencats:
139 gencats[cat].append(code)
141 # record combining class, if any
143 if combine not in combines:
144 combines[combine] = []
145 combines[combine].append(code)
147 # generate Not_Assigned from Assigned
148 gencats["Cn"] = gen_unassigned(gencats["Assigned"])
149 # Assigned is not a real category
150 del(gencats["Assigned"])
151 # Other contains Not_Assigned
152 gencats["C"].extend(gencats["Cn"])
153 gencats = group_cats(gencats)
154 combines = to_combines(group_cats(combines))
156 return (canon_decomp, compat_decomp, gencats, combines, lowerupper, upperlower)
158 def group_cats(cats):
161 cats_out[cat] = group_cat(cats[cat])
166 letters = sorted(set(cat))
167 cur_start = letters.pop(0)
169 for letter in letters:
170 assert letter > cur_end, \
171 "cur_end: %s, letter: %s" % (hex(cur_end), hex(letter))
172 if letter == cur_end + 1:
175 cat_out.append((cur_start, cur_end))
176 cur_start = cur_end = letter
177 cat_out.append((cur_start, cur_end))
180 def ungroup_cat(cat):
188 def gen_unassigned(assigned):
189 assigned = set(assigned)
190 return ([i for i in range(0, 0xd800) if i not in assigned] +
191 [i for i in range(0xe000, 0x110000) if i not in assigned])
193 def to_combines(combs):
196 for (lo, hi) in combs[comb]:
197 combs_out.append((lo, hi, comb))
198 combs_out.sort(key=lambda comb: comb[0])
201 def format_table_content(f, content, indent):
204 for chunk in content.split(","):
205 if len(line) + len(chunk) < 98:
212 f.write(line + ",\n")
213 line = " "*indent + chunk
216 def load_properties(f, interestingprops):
219 re1 = re.compile("^([0-9A-F]+) +; (\w+)")
220 re2 = re.compile("^([0-9A-F]+)\.\.([0-9A-F]+) +; (\w+)")
222 for line in fileinput.input(f):
239 if interestingprops and prop not in interestingprops:
243 if prop not in props:
245 props[prop].append((d_lo, d_hi))
248 # load all widths of want_widths, except those in except_cats
249 def load_east_asian_width(want_widths, except_cats):
250 f = "EastAsianWidth.txt"
253 re1 = re.compile("^([0-9A-F]+);(\w+) +# (\w+)")
254 re2 = re.compile("^([0-9A-F]+)\.\.([0-9A-F]+);(\w+) +# (\w+)")
256 for line in fileinput.input(f):
276 if cat in except_cats or width not in want_widths:
280 if width not in widths:
282 widths[width].append((d_lo, d_hi))
286 return "'\\u{%x}'" % c
288 def emit_bsearch_range_table(f):
290 fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
291 use core::cmp::Ordering::{Equal, Less, Greater};
292 use core::slice::SliceExt;
293 r.binary_search(|&(lo,hi)| {
294 if lo <= c && c <= hi { Equal }
295 else if hi < c { Less }
301 def emit_table(f, name, t_data, t_type = "&'static [(char, char)]", is_pub=True,
302 pfun=lambda x: "(%s,%s)" % (escape_char(x[0]), escape_char(x[1]))):
306 f.write(" %sstatic %s: %s = &[\n" % (pub_string, name, t_type))
314 format_table_content(f, data, 8)
317 def emit_property_module(f, mod, tbl, emit_fn):
318 f.write("pub mod %s {\n" % mod)
322 emit_table(f, "%s_table" % cat, tbl[cat])
324 f.write(" pub fn %s(c: char) -> bool {\n" % cat)
325 f.write(" super::bsearch_range_table(c, %s_table)\n" % cat)
329 def emit_regex_module(f, cats, w_data):
330 f.write("pub mod regex {\n")
331 regex_class = "&'static [(char, char)]"
332 class_table = "&'static [(&'static str, &'static %s)]" % regex_class
334 emit_table(f, "UNICODE_CLASSES", cats, class_table,
335 pfun=lambda x: "(\"%s\",&super::%s::%s_table)" % (x[0], x[1], x[0]))
337 f.write(" pub static PERLD: &'static %s = &super::general_category::Nd_table;\n\n"
339 f.write(" pub static PERLS: &'static %s = &super::property::White_Space_table;\n\n"
342 emit_table(f, "PERLW", w_data, regex_class)
346 def emit_conversions_module(f, lowerupper, upperlower):
347 f.write("pub mod conversions {")
349 use core::cmp::Ordering::{Equal, Less, Greater};
350 use core::slice::SliceExt;
351 use core::option::Option;
352 use core::option::Option::{Some, None};
355 pub fn to_lower(c: char) -> char {
356 match bsearch_case_table(c, LuLl_table) {
358 Some(index) => LuLl_table[index].1
362 pub fn to_upper(c: char) -> char {
363 match bsearch_case_table(c, LlLu_table) {
365 Some(index) => LlLu_table[index].1
369 fn bsearch_case_table(c: char, table: &'static [(char, char)]) -> Option<uint> {
370 match table.binary_search(|&(key, _)| {
371 if c == key { Equal }
372 else if key < c { Less }
375 slice::BinarySearchResult::Found(i) => Some(i),
376 slice::BinarySearchResult::NotFound(_) => None,
381 emit_table(f, "LuLl_table",
382 sorted(upperlower.iteritems(), key=operator.itemgetter(0)), is_pub=False)
383 emit_table(f, "LlLu_table",
384 sorted(lowerupper.iteritems(), key=operator.itemgetter(0)), is_pub=False)
387 def emit_grapheme_module(f, grapheme_table, grapheme_cats):
388 f.write("""pub mod grapheme {
389 use core::kinds::Copy;
390 use core::slice::SliceExt;
391 pub use self::GraphemeCat::*;
394 #[allow(non_camel_case_types)]
395 #[derive(Clone, Copy)]
396 pub enum GraphemeCat {
398 for cat in grapheme_cats + ["Any"]:
399 f.write(" GC_" + cat + ",\n")
402 fn bsearch_range_value_table(c: char, r: &'static [(char, char, GraphemeCat)]) -> GraphemeCat {
403 use core::cmp::Ordering::{Equal, Less, Greater};
404 match r.binary_search(|&(lo, hi, _)| {
405 if lo <= c && c <= hi { Equal }
406 else if hi < c { Less }
409 slice::BinarySearchResult::Found(idx) => {
410 let (_, _, cat) = r[idx];
413 slice::BinarySearchResult::NotFound(_) => GC_Any
417 pub fn grapheme_category(c: char) -> GraphemeCat {
418 bsearch_range_value_table(c, grapheme_cat_table)
423 emit_table(f, "grapheme_cat_table", grapheme_table, "&'static [(char, char, GraphemeCat)]",
424 pfun=lambda x: "(%s,%s,GC_%s)" % (escape_char(x[0]), escape_char(x[1]), x[2]),
428 def emit_charwidth_module(f, width_table):
429 f.write("pub mod charwidth {\n")
430 f.write(" use core::option::Option;\n")
431 f.write(" use core::option::Option::{Some, None};\n")
432 f.write(" use core::slice::SliceExt;\n")
433 f.write(" use core::slice;\n")
435 fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
436 use core::cmp::Ordering::{Equal, Less, Greater};
437 match r.binary_search(|&(lo, hi, _, _)| {
438 if lo <= c && c <= hi { Equal }
439 else if hi < c { Less }
442 slice::BinarySearchResult::Found(idx) => {
443 let (_, _, r_ncjk, r_cjk) = r[idx];
444 if is_cjk { r_cjk } else { r_ncjk }
446 slice::BinarySearchResult::NotFound(_) => 1
452 pub fn width(c: char, is_cjk: bool) -> Option<uint> {
454 _c @ 0 => Some(0), // null is zero width
455 cu if cu < 0x20 => None, // control sequences have no width
456 cu if cu < 0x7F => Some(1), // ASCII
457 cu if cu < 0xA0 => None, // more control sequences
458 _ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as uint)
464 f.write(" // character width table. Based on Markus Kuhn's free wcwidth() implementation,\n")
465 f.write(" // http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c\n")
466 emit_table(f, "charwidth_table", width_table, "&'static [(char, char, u8, u8)]", is_pub=False,
467 pfun=lambda x: "(%s,%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), x[2], x[3]))
470 def emit_norm_module(f, canon, compat, combine, norm_props):
471 canon_keys = canon.keys()
474 compat_keys = compat.keys()
478 comp_exclusions = norm_props["Full_Composition_Exclusion"]
479 for char in canon_keys:
480 if True in map(lambda (lo, hi): lo <= char <= hi, comp_exclusions):
484 if not canon_comp.has_key(decomp[0]):
485 canon_comp[decomp[0]] = []
486 canon_comp[decomp[0]].append( (decomp[1], char) )
487 canon_comp_keys = canon_comp.keys()
488 canon_comp_keys.sort()
490 f.write("pub mod normalization {\n")
492 def mkdata_fun(table):
494 data = "(%s,&[" % escape_char(char)
496 for d in table[char]:
500 data += escape_char(d)
505 f.write(" // Canonical decompositions\n")
506 emit_table(f, "canonical_table", canon_keys, "&'static [(char, &'static [char])]",
507 pfun=mkdata_fun(canon))
509 f.write(" // Compatibility decompositions\n")
510 emit_table(f, "compatibility_table", compat_keys, "&'static [(char, &'static [char])]",
511 pfun=mkdata_fun(compat))
514 data = "(%s,&[" % escape_char(char)
515 canon_comp[char].sort(lambda x, y: x[0] - y[0])
517 for pair in canon_comp[char]:
521 data += "(%s,%s)" % (escape_char(pair[0]), escape_char(pair[1]))
525 f.write(" // Canonical compositions\n")
526 emit_table(f, "composition_table", canon_comp_keys,
527 "&'static [(char, &'static [(char, char)])]", pfun=comp_pfun)
530 fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
531 use core::cmp::Ordering::{Equal, Less, Greater};
532 use core::slice::SliceExt;
534 match r.binary_search(|&(lo, hi, _)| {
535 if lo <= c && c <= hi { Equal }
536 else if hi < c { Less }
539 slice::BinarySearchResult::Found(idx) => {
540 let (_, _, result) = r[idx];
543 slice::BinarySearchResult::NotFound(_) => 0
548 emit_table(f, "combining_class_table", combine, "&'static [(char, char, u8)]", is_pub=False,
549 pfun=lambda x: "(%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), x[2]))
551 f.write(" pub fn canonical_combining_class(c: char) -> u8 {\n"
552 + " bsearch_range_value_table(c, combining_class_table)\n"
560 def remove_from_wtable(wtable, val):
563 if wtable[0][1] < val:
564 wtable_out.append(wtable.pop(0))
565 elif wtable[0][0] > val:
568 (wt_lo, wt_hi, width, width_cjk) = wtable.pop(0)
569 if wt_lo == wt_hi == val:
572 wtable_out.append((wt_lo+1, wt_hi, width, width_cjk))
574 wtable_out.append((wt_lo, wt_hi-1, width, width_cjk))
576 wtable_out.append((wt_lo, val-1, width, width_cjk))
577 wtable_out.append((val+1, wt_hi, width, width_cjk))
579 wtable_out.extend(wtable)
584 def optimize_width_table(wtable):
586 w_this = wtable.pop(0)
588 if w_this[1] == wtable[0][0] - 1 and w_this[2:3] == wtable[0][2:3]:
589 w_tmp = wtable.pop(0)
590 w_this = (w_this[0], w_tmp[1], w_tmp[2], w_tmp[3])
592 wtable_out.append(w_this)
593 w_this = wtable.pop(0)
594 wtable_out.append(w_this)
597 if __name__ == "__main__":
599 if os.path.exists(r):
601 with open(r, "w") as rf:
602 # write the file's preamble
605 # download and parse all the data
607 with open("ReadMe.txt") as readme:
608 pattern = "for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
609 unicode_version = re.search(pattern, readme.read()).groups()
611 /// The version of [Unicode](http://www.unicode.org/)
612 /// that the `UnicodeChar` and `UnicodeStrPrelude` traits are based on.
613 pub const UNICODE_VERSION: (uint, uint, uint) = (%s, %s, %s);
614 """ % unicode_version)
615 (canon_decomp, compat_decomp, gencats, combines,
616 lowerupper, upperlower) = load_unicode_data("UnicodeData.txt")
617 want_derived = ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase"]
618 other_derived = ["Default_Ignorable_Code_Point", "Grapheme_Extend"]
619 derived = load_properties("DerivedCoreProperties.txt", want_derived + other_derived)
620 scripts = load_properties("Scripts.txt", [])
621 props = load_properties("PropList.txt",
622 ["White_Space", "Join_Control", "Noncharacter_Code_Point"])
623 norm_props = load_properties("DerivedNormalizationProps.txt",
624 ["Full_Composition_Exclusion"])
626 # grapheme cluster category from DerivedCoreProperties
627 # the rest are defined below
629 grapheme_cats["Extend"] = derived["Grapheme_Extend"]
630 del(derived["Grapheme_Extend"])
632 # bsearch_range_table is used in all the property modules below
633 emit_bsearch_range_table(rf)
635 # all of these categories will also be available as \p{} in libregex
637 for (name, cat, pfuns) in ("general_category", gencats, ["N", "Cc"]), \
638 ("derived_property", derived, want_derived), \
639 ("script", scripts, []), \
640 ("property", props, ["White_Space"]):
641 emit_property_module(rf, name, cat, pfuns)
642 allcats.extend(map(lambda x: (x, name), cat))
643 allcats.sort(key=lambda c: c[0])
645 # the \w regex corresponds to Alphabetic + Mark + Decimal_Number +
646 # Connector_Punctuation + Join-Control according to UTS#18
647 # http://www.unicode.org/reports/tr18/#Compatibility_Properties
649 for cat in derived["Alphabetic"], gencats["M"], gencats["Nd"], \
650 gencats["Pc"], props["Join_Control"]:
651 perl_words.extend(ungroup_cat(cat))
652 perl_words = group_cat(perl_words)
654 # emit lookup tables for \p{}, along with \d, \w, and \s for libregex
655 emit_regex_module(rf, allcats, perl_words)
657 # normalizations and conversions module
658 emit_norm_module(rf, canon_decomp, compat_decomp, combines, norm_props)
659 emit_conversions_module(rf, lowerupper, upperlower)
661 ### character width module
663 for zwcat in ["Me", "Mn", "Cf"]:
664 width_table.extend(map(lambda (lo, hi): (lo, hi, 0, 0), gencats[zwcat]))
665 width_table.append((4448, 4607, 0, 0))
667 # get widths, except those that are explicitly marked zero-width above
668 ea_widths = load_east_asian_width(["W", "F", "A"], ["Me", "Mn", "Cf"])
669 # these are doublewidth
670 for dwcat in ["W", "F"]:
671 width_table.extend(map(lambda (lo, hi): (lo, hi, 2, 2), ea_widths[dwcat]))
672 width_table.extend(map(lambda (lo, hi): (lo, hi, 1, 2), ea_widths["A"]))
674 width_table.sort(key=lambda w: w[0])
676 # soft hyphen is not zero width in preformatted text; it's used to indicate
677 # a hyphen inserted to facilitate a linebreak.
678 width_table = remove_from_wtable(width_table, 173)
680 # optimize the width table by collapsing adjacent entities when possible
681 width_table = optimize_width_table(width_table)
682 emit_charwidth_module(rf, width_table)
684 ### grapheme cluster module
685 # from http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Break_Property_Values
686 # Hangul syllable categories
687 want_hangul = ["L", "V", "T", "LV", "LVT"]
688 grapheme_cats.update(load_properties("HangulSyllableType.txt", want_hangul))
691 # This category also includes Cs (surrogate codepoints), but Rust's `char`s are
692 # Unicode Scalar Values only, and surrogates are thus invalid `char`s.
693 grapheme_cats["Control"] = set()
694 for cat in ["Zl", "Zp", "Cc", "Cf"]:
695 grapheme_cats["Control"] |= set(ungroup_cat(gencats[cat]))
696 grapheme_cats["Control"] = group_cat(list(
697 grapheme_cats["Control"]
698 - grapheme_control_exceptions
699 | (set(ungroup_cat(gencats["Cn"]))
700 & set(ungroup_cat(derived["Default_Ignorable_Code_Point"])))))
703 grapheme_cats["RegionalIndicator"] = grapheme_regional_indicator
705 # Prepend - "Currently there are no characters with this value"
706 # (from UAX#29, Unicode 7.0)
709 grapheme_cats["SpacingMark"] = group_cat(list(
710 set(ungroup_cat(gencats["Mc"]))
711 - set(ungroup_cat(grapheme_cats["Extend"]))
712 | grapheme_spacingmark_extra
713 - set(ungroup_cat(grapheme_spacingmark_exceptions))))
716 for cat in grapheme_cats:
717 grapheme_table.extend([(x, y, cat) for (x, y) in grapheme_cats[cat]])
718 grapheme_table.sort(key=lambda w: w[0])
719 emit_grapheme_module(rf, grapheme_table, grapheme_cats.keys())