]>
git.proxmox.com Git - rustc.git/blob - src/etc/unicode.py
f580127cddaf8386515919b10902772a45aacbcf
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 # - DerivedNormalizationProps.txt
16 # - EastAsianWidth.txt
17 # - auxiliary/GraphemeBreakProperty.txt
23 # Since this should not require frequent updates, we just store this
24 # out-of-line and check the unicode.rs file into git.
26 import fileinput
, re
, os
, sys
, operator
28 preamble
= '''// Copyright 2012-2015 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.
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.
38 // NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
40 #![allow(missing_docs, non_upper_case_globals, non_snake_case)]
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'],
57 # these are the surrogate codepoints, which are not valid rust characters
58 surrogate_codepoints
= (0xd800, 0xdfff)
61 if not os
.path
.exists(os
.path
.basename(f
)):
62 os
.system("curl -O http://www.unicode.org/Public/UNIDATA/%s"
65 if not os
.path
.exists(os
.path
.basename(f
)):
66 sys
.stderr
.write("cannot load %s" % f
)
70 return surrogate_codepoints
[0] <= n
<= surrogate_codepoints
[1]
72 def load_unicode_data(f
):
84 for line
in fileinput
.input(f
):
85 data
= line
.split(';');
88 cp
= int(data
[0], 16);
92 for i
in xrange(range_start
, cp
):
95 if data
[1].endswith(", First>"):
101 [code_org
, name
, gencat
, combine
, bidi
,
102 decomp
, deci
, digit
, num
, mirror
,
103 old
, iso
, upcase
, lowcase
, titlecase
] = udict
[code
];
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)
110 # lowercase to uppercase
111 if upcase
!= "" and code_org
!= upcase
:
112 to_upper
[code
] = (int(upcase
, 16), 0, 0)
115 if titlecase
.strip() != "" and code_org
!= titlecase
:
116 to_title
[code
] = (int(titlecase
, 16), 0, 0)
118 # store decomposition, if given
120 if decomp
.startswith('<'):
122 for i
in decomp
.split()[1:]:
123 seq
.append(int(i
, 16))
124 compat_decomp
[code
] = seq
127 for i
in decomp
.split():
128 seq
.append(int(i
, 16))
129 canon_decomp
[code
] = seq
131 # place letter in categories as appropriate
132 for cat
in [gencat
, "Assigned"] + expanded_categories
.get(gencat
, []):
133 if cat
not in gencats
:
135 gencats
[cat
].append(code
)
137 # record combining class, if any
139 if combine
not in combines
:
140 combines
[combine
] = []
141 combines
[combine
].append(code
)
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
))
152 return (canon_decomp
, compat_decomp
, gencats
, combines
, to_upper
, to_lower
, to_title
)
154 def load_special_casing(f
, to_upper
, to_lower
, to_title
):
156 for line
in fileinput
.input(f
):
157 data
= line
.split('#')[0].split(';')
159 code
, lower
, title
, upper
, _comment
= data
161 code
, lower
, title
, upper
, condition
, _comment
= data
162 if condition
.strip(): # Only keep unconditional mappins
167 lower
= lower
.strip()
168 title
= title
.strip()
169 upper
= upper
.strip()
171 for (map_
, values
) in [(to_lower
, lower
), (to_upper
, upper
), (to_title
, title
)]:
173 values
= [int(i
, 16) for i
in values
.split()]
174 for _
in range(len(values
), 3):
176 assert len(values
) == 3
179 def group_cats(cats
):
182 cats_out
[cat
] = group_cat(cats
[cat
])
187 letters
= sorted(set(cat
))
188 cur_start
= letters
.pop(0)
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:
196 cat_out
.append((cur_start
, cur_end
))
197 cur_start
= cur_end
= letter
198 cat_out
.append((cur_start
, cur_end
))
201 def ungroup_cat(cat
):
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
])
214 def to_combines(combs
):
217 for (lo
, hi
) in combs
[comb
]:
218 combs_out
.append((lo
, hi
, comb
))
219 combs_out
.sort(key
=lambda comb
: comb
[0])
222 def format_table_content(f
, content
, indent
):
225 for chunk
in content
.split(","):
226 if len(line
) + len(chunk
) < 98:
233 f
.write(line
+ ",\n")
234 line
= " "*indent
+ chunk
237 def load_properties(f
, interestingprops
):
240 re1
= re
.compile("^ *([0-9A-F]+) *; *(\w+)")
241 re2
= re
.compile("^ *([0-9A-F]+)\.\.([0-9A-F]+) *; *(\w+)")
243 for line
in fileinput
.input(os
.path
.basename(f
)):
260 if interestingprops
and prop
not in interestingprops
:
264 if prop
not in props
:
266 props
[prop
].append((d_lo
, d_hi
))
268 # optimize if possible
270 props
[prop
] = group_cat(ungroup_cat(props
[prop
]))
274 # load all widths of want_widths, except those in except_cats
275 def load_east_asian_width(want_widths
, except_cats
):
276 f
= "EastAsianWidth.txt"
279 re1
= re
.compile("^([0-9A-F]+);(\w+) +# (\w+)")
280 re2
= re
.compile("^([0-9A-F]+)\.\.([0-9A-F]+);(\w+) +# (\w+)")
282 for line
in fileinput
.input(f
):
302 if cat
in except_cats
or width
not in want_widths
:
306 if width
not in widths
:
308 widths
[width
].append((d_lo
, d_hi
))
312 return "'\\u{%x}'" % c
if c
!= 0 else "'\\0'"
314 def emit_bsearch_range_table(f
):
316 fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
317 use core::cmp::Ordering::{Equal, Less, Greater};
318 use core::slice::SliceExt;
319 r.binary_search_by(|&(lo,hi)| {
320 if lo <= c && c <= hi { Equal }
321 else if hi < c { Less }
327 def emit_table(f
, name
, t_data
, t_type
= "&'static [(char, char)]", is_pub
=True,
328 pfun
=lambda x
: "(%s,%s)" % (escape_char(x
[0]), escape_char(x
[1]))):
332 f
.write(" %sconst %s: %s = &[\n" % (pub_string
, name
, t_type
))
340 format_table_content(f
, data
, 8)
343 def emit_property_module(f
, mod
, tbl
, emit
):
344 f
.write("pub mod %s {\n" % mod
)
345 for cat
in sorted(emit
):
346 emit_table(f
, "%s_table" % cat
, tbl
[cat
])
347 f
.write(" pub fn %s(c: char) -> bool {\n" % cat
)
348 f
.write(" super::bsearch_range_table(c, %s_table)\n" % cat
)
352 def emit_conversions_module(f
, to_upper
, to_lower
, to_title
):
353 f
.write("pub mod conversions {")
355 use core::cmp::Ordering::{Equal, Less, Greater};
356 use core::slice::SliceExt;
357 use core::option::Option;
358 use core::option::Option::{Some, None};
359 use core::result::Result::{Ok, Err};
361 pub fn to_lower(c: char) -> [char; 3] {
362 match bsearch_case_table(c, to_lowercase_table) {
363 None => [c, '\\0', '\\0'],
364 Some(index) => to_lowercase_table[index].1
368 pub fn to_upper(c: char) -> [char; 3] {
369 match bsearch_case_table(c, to_uppercase_table) {
370 None => [c, '\\0', '\\0'],
371 Some(index) => to_uppercase_table[index].1
375 pub fn to_title(c: char) -> [char; 3] {
376 match bsearch_case_table(c, to_titlecase_table) {
377 None => [c, '\\0', '\\0'],
378 Some(index) => to_titlecase_table[index].1
382 fn bsearch_case_table(c: char, table: &'static [(char, [char; 3])]) -> Option<usize> {
383 match table.binary_search_by(|&(key, _)| {
384 if c == key { Equal }
385 else if key < c { Less }
394 t_type
= "&'static [(char, [char; 3])]"
395 pfun
= lambda x
: "(%s,[%s,%s,%s])" % (
396 escape_char(x
[0]), escape_char(x
[1][0]), escape_char(x
[1][1]), escape_char(x
[1][2]))
397 emit_table(f
, "to_lowercase_table",
398 sorted(to_lower
.iteritems(), key
=operator
.itemgetter(0)),
399 is_pub
=False, t_type
= t_type
, pfun
=pfun
)
400 emit_table(f
, "to_uppercase_table",
401 sorted(to_upper
.iteritems(), key
=operator
.itemgetter(0)),
402 is_pub
=False, t_type
= t_type
, pfun
=pfun
)
403 emit_table(f
, "to_titlecase_table",
404 sorted(to_title
.iteritems(), key
=operator
.itemgetter(0)),
405 is_pub
=False, t_type
= t_type
, pfun
=pfun
)
408 def emit_grapheme_module(f
, grapheme_table
, grapheme_cats
):
409 f
.write("""pub mod grapheme {
410 use core::slice::SliceExt;
411 pub use self::GraphemeCat::*;
412 use core::result::Result::{Ok, Err};
414 #[allow(non_camel_case_types)]
415 #[derive(Clone, Copy)]
416 pub enum GraphemeCat {
418 for cat
in grapheme_cats
+ ["Any"]:
419 f
.write(" GC_" + cat
+ ",\n")
422 fn bsearch_range_value_table(c: char, r: &'static [(char, char, GraphemeCat)]) -> GraphemeCat {
423 use core::cmp::Ordering::{Equal, Less, Greater};
424 match r.binary_search_by(|&(lo, hi, _)| {
425 if lo <= c && c <= hi { Equal }
426 else if hi < c { Less }
430 let (_, _, cat) = r[idx];
437 pub fn grapheme_category(c: char) -> GraphemeCat {
438 bsearch_range_value_table(c, grapheme_cat_table)
443 emit_table(f
, "grapheme_cat_table", grapheme_table
, "&'static [(char, char, GraphemeCat)]",
444 pfun
=lambda x
: "(%s,%s,GC_%s)" % (escape_char(x
[0]), escape_char(x
[1]), x
[2]),
448 def emit_charwidth_module(f
, width_table
):
449 f
.write("pub mod charwidth {\n")
450 f
.write(" use core::option::Option;\n")
451 f
.write(" use core::option::Option::{Some, None};\n")
452 f
.write(" use core::slice::SliceExt;\n")
453 f
.write(" use core::result::Result::{Ok, Err};\n")
455 fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
456 use core::cmp::Ordering::{Equal, Less, Greater};
457 match r.binary_search_by(|&(lo, hi, _, _)| {
458 if lo <= c && c <= hi { Equal }
459 else if hi < c { Less }
463 let (_, _, r_ncjk, r_cjk) = r[idx];
464 if is_cjk { r_cjk } else { r_ncjk }
472 pub fn width(c: char, is_cjk: bool) -> Option<usize> {
474 _c @ 0 => Some(0), // null is zero width
475 cu if cu < 0x20 => None, // control sequences have no width
476 cu if cu < 0x7F => Some(1), // ASCII
477 cu if cu < 0xA0 => None, // more control sequences
478 _ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as usize)
484 f
.write(" // character width table. Based on Markus Kuhn's free wcwidth() implementation,\n")
485 f
.write(" // http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c\n")
486 emit_table(f
, "charwidth_table", width_table
, "&'static [(char, char, u8, u8)]", is_pub
=False,
487 pfun
=lambda x
: "(%s,%s,%s,%s)" % (escape_char(x
[0]), escape_char(x
[1]), x
[2], x
[3]))
490 def emit_norm_module(f
, canon
, compat
, combine
, norm_props
):
491 canon_keys
= canon
.keys()
494 compat_keys
= compat
.keys()
498 comp_exclusions
= norm_props
["Full_Composition_Exclusion"]
499 for char
in canon_keys
:
500 if True in map(lambda (lo
, hi
): lo
<= char
<= hi
, comp_exclusions
):
504 if not canon_comp
.has_key(decomp
[0]):
505 canon_comp
[decomp
[0]] = []
506 canon_comp
[decomp
[0]].append( (decomp
[1], char
) )
507 canon_comp_keys
= canon_comp
.keys()
508 canon_comp_keys
.sort()
510 f
.write("pub mod normalization {\n")
512 def mkdata_fun(table
):
514 data
= "(%s,&[" % escape_char(char
)
516 for d
in table
[char
]:
520 data
+= escape_char(d
)
525 f
.write(" // Canonical decompositions\n")
526 emit_table(f
, "canonical_table", canon_keys
, "&'static [(char, &'static [char])]",
527 pfun
=mkdata_fun(canon
))
529 f
.write(" // Compatibility decompositions\n")
530 emit_table(f
, "compatibility_table", compat_keys
, "&'static [(char, &'static [char])]",
531 pfun
=mkdata_fun(compat
))
534 data
= "(%s,&[" % escape_char(char
)
535 canon_comp
[char
].sort(lambda x
, y
: x
[0] - y
[0])
537 for pair
in canon_comp
[char
]:
541 data
+= "(%s,%s)" % (escape_char(pair
[0]), escape_char(pair
[1]))
545 f
.write(" // Canonical compositions\n")
546 emit_table(f
, "composition_table", canon_comp_keys
,
547 "&'static [(char, &'static [(char, char)])]", pfun
=comp_pfun
)
550 fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
551 use core::cmp::Ordering::{Equal, Less, Greater};
552 use core::slice::SliceExt;
553 use core::result::Result::{Ok, Err};
554 match r.binary_search_by(|&(lo, hi, _)| {
555 if lo <= c && c <= hi { Equal }
556 else if hi < c { Less }
560 let (_, _, result) = r[idx];
568 emit_table(f
, "combining_class_table", combine
, "&'static [(char, char, u8)]", is_pub
=False,
569 pfun
=lambda x
: "(%s,%s,%s)" % (escape_char(x
[0]), escape_char(x
[1]), x
[2]))
571 f
.write(""" #[deprecated(reason = "use the crates.io `unicode-normalization` lib instead",
573 #[unstable(feature = "unicode",
574 reason = "this functionality will be moved to crates.io")]
575 pub fn canonical_combining_class(c: char) -> u8 {
576 bsearch_range_value_table(c, combining_class_table)
583 def remove_from_wtable(wtable
, val
):
586 if wtable
[0][1] < val
:
587 wtable_out
.append(wtable
.pop(0))
588 elif wtable
[0][0] > val
:
591 (wt_lo
, wt_hi
, width
, width_cjk
) = wtable
.pop(0)
592 if wt_lo
== wt_hi
== val
:
595 wtable_out
.append((wt_lo
+1, wt_hi
, width
, width_cjk
))
597 wtable_out
.append((wt_lo
, wt_hi
-1, width
, width_cjk
))
599 wtable_out
.append((wt_lo
, val
-1, width
, width_cjk
))
600 wtable_out
.append((val
+1, wt_hi
, width
, width_cjk
))
602 wtable_out
.extend(wtable
)
607 def optimize_width_table(wtable
):
609 w_this
= wtable
.pop(0)
611 if w_this
[1] == wtable
[0][0] - 1 and w_this
[2:3] == wtable
[0][2:3]:
612 w_tmp
= wtable
.pop(0)
613 w_this
= (w_this
[0], w_tmp
[1], w_tmp
[2], w_tmp
[3])
615 wtable_out
.append(w_this
)
616 w_this
= wtable
.pop(0)
617 wtable_out
.append(w_this
)
620 if __name__
== "__main__":
622 if os
.path
.exists(r
):
624 with
open(r
, "w") as rf
:
625 # write the file's preamble
628 # download and parse all the data
630 with
open("ReadMe.txt") as readme
:
631 pattern
= "for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
632 unicode_version
= re
.search(pattern
, readme
.read()).groups()
634 /// The version of [Unicode](http://www.unicode.org/)
635 /// that the unicode parts of `CharExt` and `UnicodeStrPrelude` traits are based on.
636 pub const UNICODE_VERSION: (u64, u64, u64) = (%s, %s, %s);
637 """ % unicode_version
)
638 (canon_decomp
, compat_decomp
, gencats
, combines
,
639 to_upper
, to_lower
, to_title
) = load_unicode_data("UnicodeData.txt")
640 load_special_casing("SpecialCasing.txt", to_upper
, to_lower
, to_title
)
641 want_derived
= ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase",
642 "Cased", "Case_Ignorable"]
643 derived
= load_properties("DerivedCoreProperties.txt", want_derived
)
644 scripts
= load_properties("Scripts.txt", [])
645 props
= load_properties("PropList.txt",
646 ["White_Space", "Join_Control", "Noncharacter_Code_Point"])
647 norm_props
= load_properties("DerivedNormalizationProps.txt",
648 ["Full_Composition_Exclusion"])
650 # bsearch_range_table is used in all the property modules below
651 emit_bsearch_range_table(rf
)
654 for (name
, cat
, pfuns
) in ("general_category", gencats
, ["N", "Cc"]), \
655 ("derived_property", derived
, want_derived
), \
656 ("property", props
, ["White_Space"]):
657 emit_property_module(rf
, name
, cat
, pfuns
)
659 # normalizations and conversions module
660 emit_norm_module(rf
, canon_decomp
, compat_decomp
, combines
, norm_props
)
661 emit_conversions_module(rf
, to_upper
, to_lower
, to_title
)
663 ### character width module
665 for zwcat
in ["Me", "Mn", "Cf"]:
666 width_table
.extend(map(lambda (lo
, hi
): (lo
, hi
, 0, 0), gencats
[zwcat
]))
667 width_table
.append((4448, 4607, 0, 0))
669 # get widths, except those that are explicitly marked zero-width above
670 ea_widths
= load_east_asian_width(["W", "F", "A"], ["Me", "Mn", "Cf"])
671 # these are doublewidth
672 for dwcat
in ["W", "F"]:
673 width_table
.extend(map(lambda (lo
, hi
): (lo
, hi
, 2, 2), ea_widths
[dwcat
]))
674 width_table
.extend(map(lambda (lo
, hi
): (lo
, hi
, 1, 2), ea_widths
["A"]))
676 width_table
.sort(key
=lambda w
: w
[0])
678 # soft hyphen is not zero width in preformatted text; it's used to indicate
679 # a hyphen inserted to facilitate a linebreak.
680 width_table
= remove_from_wtable(width_table
, 173)
682 # optimize the width table by collapsing adjacent entities when possible
683 width_table
= optimize_width_table(width_table
)
684 emit_charwidth_module(rf
, width_table
)
686 ### grapheme cluster module
687 # from http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Break_Property_Values
688 grapheme_cats
= load_properties("auxiliary/GraphemeBreakProperty.txt", [])
692 # This category also includes Cs (surrogate codepoints), but Rust's `char`s are
693 # Unicode Scalar Values only, and surrogates are thus invalid `char`s.
694 # Thus, we have to remove Cs from the Control category
696 # 0x0a and 0x0d (CR and LF) are not in the Control category for Graphemes.
697 # However, the Graphemes iterator treats these as a special case, so they
698 # should be included in grapheme_cats["Control"] for our implementation.
699 grapheme_cats
["Control"] = group_cat(list(
700 (set(ungroup_cat(grapheme_cats
["Control"]))
701 |
set(ungroup_cat(grapheme_cats
["CR"]))
702 |
set(ungroup_cat(grapheme_cats
["LF"])))
703 - set(ungroup_cat([surrogate_codepoints
]))))
704 del(grapheme_cats
["CR"])
705 del(grapheme_cats
["LF"])
708 for cat
in grapheme_cats
:
709 grapheme_table
.extend([(x
, y
, cat
) for (x
, y
) in grapheme_cats
[cat
]])
710 grapheme_table
.sort(key
=lambda w
: w
[0])
711 emit_grapheme_module(rf
, grapheme_table
, grapheme_cats
.keys())