about summary refs log tree commit diff
path: root/src/etc/unicode.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/etc/unicode.py')
-rwxr-xr-xsrc/etc/unicode.py636
1 files changed, 351 insertions, 285 deletions
diff --git a/src/etc/unicode.py b/src/etc/unicode.py
index 586890ebe4c..a87c755397d 100755
--- a/src/etc/unicode.py
+++ b/src/etc/unicode.py
@@ -10,17 +10,46 @@
 # option. This file may not be copied, modified, or distributed
 # except according to those terms.
 
-# This digests UnicodeData.txt and DerivedCoreProperties.txt and emits rust
-# code covering the core properties. Since this is a pretty rare event we
-# just store this out-of-line and check the unicode.rs file into git.
+# This script uses the following Unicode tables:
+# - DerivedCoreProperties.txt
+# - EastAsianWidth.txt
+# - PropList.txt
+# - Scripts.txt
+# - UnicodeData.txt
 #
-# The emitted code is "the minimum we think is necessary for libstd", that
-# is, to support basic operations of the compiler and "most nontrivial rust
-# programs". It is not meant to be a complete implementation of unicode.
-# For that we recommend you use a proper binding to libicu.
+# Since this should not require frequent updates, we just store this
+# out-of-line and check the unicode.rs file into git.
 
 import fileinput, re, os, sys, operator
 
+preamble = '''// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
+
+#![allow(missing_doc, non_uppercase_statics, non_snake_case_functions)]
+'''
+
+# Mapping taken from Table 12 from:
+# http://www.unicode.org/reports/tr44/#General_Category_Values
+expanded_categories = {
+    'Lu': ['LC', 'L'], 'Ll': ['LC', 'L'], 'Lt': ['LC', 'L'],
+    'Lm': ['L'], 'Lo': ['L'],
+    'Mn': ['M'], 'Mc': ['M'], 'Me': ['M'],
+    'Nd': ['N'], 'Nl': ['N'], 'No': ['No'],
+    'Pc': ['P'], 'Pd': ['P'], 'Ps': ['P'], 'Pe': ['P'],
+    'Pi': ['P'], 'Pf': ['P'], 'Po': ['P'],
+    'Sm': ['S'], 'Sc': ['S'], 'Sk': ['S'], 'So': ['S'],
+    'Zs': ['Z'], 'Zl': ['Z'], 'Zp': ['Z'],
+    'Cc': ['C'], 'Cf': ['C'], 'Cs': ['C'], 'Co': ['C'], 'Cn': ['C'],
+}
 
 def fetch(f):
     if not os.path.exists(f):
@@ -31,21 +60,17 @@ def fetch(f):
         sys.stderr.write("cannot load %s" % f)
         exit(1)
 
+def is_valid_unicode(n):
+    return 0 <= n <= 0xD7FF or 0xE000 <= n <= 0x10FFFF
 
 def load_unicode_data(f):
     fetch(f)
     gencats = {}
     upperlower = {}
     lowerupper = {}
-    combines = []
+    combines = {}
     canon_decomp = {}
     compat_decomp = {}
-    curr_cat = ""
-    curr_combine = ""
-    c_lo = 0
-    c_hi = 0
-    com_lo = 0
-    com_hi = 0
 
     for line in fileinput.input(f):
         fields = line.split(";")
@@ -58,6 +83,9 @@ def load_unicode_data(f):
         code_org = code
         code     = int(code, 16)
 
+        if not is_valid_unicode(code):
+            continue
+
         # generate char to char direct common and simple conversions
         # uppercase to lowercase
         if gencat == "Lu" and lowcase != "" and code_org != lowcase:
@@ -67,6 +95,7 @@ def load_unicode_data(f):
         if gencat == "Ll" and upcase != "" and code_org != upcase:
             lowerupper[code] = int(upcase, 16)
 
+        # store decomposition, if given
         if decomp != "":
             if decomp.startswith('<'):
                 seq = []
@@ -79,37 +108,75 @@ def load_unicode_data(f):
                     seq.append(int(i, 16))
                 canon_decomp[code] = seq
 
-        if curr_cat == "":
-            curr_cat = gencat
-            c_lo = code
-            c_hi = code
+        # place letter in categories as appropriate
+        for cat in [gencat] + expanded_categories.get(gencat, []):
+            if cat not in gencats:
+                gencats[cat] = []
+            gencats[cat].append(code)
 
-        if curr_cat == gencat:
-            c_hi = code
-        else:
-            if curr_cat not in gencats:
-                gencats[curr_cat] = []
+        # record combining class, if any
+        if combine != "0":
+            if combine not in combines:
+                combines[combine] = []
+            combines[combine].append(code)
 
-            gencats[curr_cat].append((c_lo, c_hi))
-            curr_cat = gencat
-            c_lo = code
-            c_hi = code
+    gencats = group_cats(gencats)
+    combines = to_combines(group_cats(combines))
 
-        if curr_combine == "":
-            curr_combine = combine
-            com_lo = code
-            com_hi = code
+    return (canon_decomp, compat_decomp, gencats, combines, lowerupper, upperlower)
 
-        if curr_combine == combine:
-            com_hi = code
+def group_cats(cats):
+    cats_out = {}
+    for cat in cats:
+        cats_out[cat] = group_cat(cats[cat])
+    return cats_out
+
+def group_cat(cat):
+    cat_out = []
+    letters = sorted(set(cat))
+    cur_start = letters.pop(0)
+    cur_end = cur_start
+    for letter in letters:
+        assert letter > cur_end, \
+            "cur_end: %s, letter: %s" % (hex(cur_end), hex(letter))
+        if letter == cur_end + 1:
+            cur_end = letter
         else:
-            if curr_combine != "0":
-                combines.append((com_lo, com_hi, curr_combine))
-            curr_combine = combine
-            com_lo = code
-            com_hi = code
+            cat_out.append((cur_start, cur_end))
+            cur_start = cur_end = letter
+    cat_out.append((cur_start, cur_end))
+    return cat_out
+
+def ungroup_cat(cat):
+    cat_out = []
+    for (lo, hi) in cat:
+        while lo <= hi:
+            cat_out.append(lo)
+            lo += 1
+    return cat_out
+
+def to_combines(combs):
+    combs_out = []
+    for comb in combs:
+        for (lo, hi) in combs[comb]:
+            combs_out.append((lo, hi, comb))
+    combs_out.sort(key=lambda comb: comb[0])
+    return combs_out
 
-    return (canon_decomp, compat_decomp, gencats, combines, lowerupper, upperlower)
+def format_table_content(f, content, indent):
+    line = " "*indent
+    first = True
+    for chunk in content.split(","):
+        if len(line) + len(chunk) < 98:
+            if first:
+                line += chunk
+            else:
+                line += ", " + chunk
+            first = False
+        else:
+            f.write(line + ",\n")
+            line = " "*indent + chunk
+    f.write(line)
 
 def load_properties(f, interestingprops):
     fetch(f)
@@ -134,7 +201,7 @@ def load_properties(f, interestingprops):
                 prop = m.group(3)
             else:
                 continue
-        if prop not in interestingprops:
+        if interestingprops and prop not in interestingprops:
             continue
         d_lo = int(d_lo, 16)
         d_hi = int(d_hi, 16)
@@ -143,6 +210,43 @@ def load_properties(f, interestingprops):
         props[prop].append((d_lo, d_hi))
     return props
 
+# load all widths of want_widths, except those in except_cats
+def load_east_asian_width(want_widths, except_cats):
+    f = "EastAsianWidth.txt"
+    fetch(f)
+    widths = {}
+    re1 = re.compile("^([0-9A-F]+);(\w+) +# (\w+)")
+    re2 = re.compile("^([0-9A-F]+)\.\.([0-9A-F]+);(\w+) +# (\w+)")
+
+    for line in fileinput.input(f):
+        width = None
+        d_lo = 0
+        d_hi = 0
+        cat = None
+        m = re1.match(line)
+        if m:
+            d_lo = m.group(1)
+            d_hi = m.group(1)
+            width = m.group(2)
+            cat = m.group(3)
+        else:
+            m = re2.match(line)
+            if m:
+                d_lo = m.group(1)
+                d_hi = m.group(2)
+                width = m.group(3)
+                cat = m.group(4)
+            else:
+                continue
+        if cat in except_cats or width not in want_widths:
+            continue
+        d_lo = int(d_lo, 16)
+        d_hi = int(d_hi, 16)
+        if width not in widths:
+            widths[width] = []
+        widths[width].append((d_lo, d_hi))
+    return widths
+
 def escape_char(c):
     if c <= 0xff:
         return "'\\x%2.2x'" % c
@@ -150,59 +254,72 @@ def escape_char(c):
         return "'\\u%4.4x'" % c
     return "'\\U%8.8x'" % c
 
-def ch_prefix(ix):
-    if ix == 0:
-        return "        "
-    if ix % 2 == 0:
-        return ",\n        "
-    else:
-        return ", "
-
 def emit_bsearch_range_table(f):
     f.write("""
 fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
-    use cmp::{Equal, Less, Greater};
-    use slice::ImmutableVector;
-    use option::None;
+    use core::cmp::{Equal, Less, Greater};
+    use core::slice::ImmutableVector;
+    use core::option::None;
     r.bsearch(|&(lo,hi)| {
         if lo <= c && c <= hi { Equal }
         else if hi < c { Less }
         else { Greater }
     }) != None
 }\n
-""");
+""")
+
+def emit_table(f, name, t_data, t_type = "&'static [(char, char)]", is_pub=True,
+        pfun=lambda x: "(%s,%s)" % (escape_char(x[0]), escape_char(x[1]))):
+    pub_string = ""
+    if is_pub:
+        pub_string = "pub "
+    f.write("    %sstatic %s: %s = &[\n" % (pub_string, name, t_type))
+    data = ""
+    first = True
+    for dat in t_data:
+        if not first:
+            data += ","
+        first = False
+        data += pfun(dat)
+    format_table_content(f, data, 8)
+    f.write("\n    ];\n\n")
 
-def emit_property_module(f, mod, tbl):
+def emit_property_module(f, mod, tbl, emit_fn):
     f.write("pub mod %s {\n" % mod)
     keys = tbl.keys()
     keys.sort()
-
     for cat in keys:
-        if cat not in ["Nd", "Nl", "No", "Cc",
-            "XID_Start", "XID_Continue", "Alphabetic",
-            "Lowercase", "Uppercase", "White_Space"]:
-            continue
-        f.write("    static %s_table : &'static [(char,char)] = &[\n" % cat)
-        ix = 0
-        for pair in tbl[cat]:
-            f.write(ch_prefix(ix))
-            f.write("(%s, %s)" % (escape_char(pair[0]), escape_char(pair[1])))
-            ix += 1
-        f.write("\n    ];\n\n")
-
-        f.write("    pub fn %s(c: char) -> bool {\n" % cat)
-        f.write("        super::bsearch_range_table(c, %s_table)\n" % cat)
-        f.write("    }\n\n")
+        emit_table(f, "%s_table" % cat, tbl[cat])
+        if cat in emit_fn:
+            f.write("    pub fn %s(c: char) -> bool {\n" % cat)
+            f.write("        super::bsearch_range_table(c, %s_table)\n" % cat)
+            f.write("    }\n\n")
     f.write("}\n\n")
 
+def emit_regex_module(f, cats, w_data):
+    f.write("pub mod regex {\n")
+    regex_class = "&'static [(char, char)]"
+    class_table = "&'static [(&'static str, %s)]" % regex_class
+
+    emit_table(f, "UNICODE_CLASSES", cats, class_table,
+        pfun=lambda x: "(\"%s\",super::%s::%s_table)" % (x[0], x[1], x[0]))
+
+    f.write("    pub static PERLD: %s = super::general_category::Nd_table;\n\n"
+            % regex_class)
+    f.write("    pub static PERLS: %s = super::property::White_Space_table;\n\n"
+            % regex_class)
+
+    emit_table(f, "PERLW", w_data, regex_class)
+
+    f.write("}\n\n")
 
 def emit_conversions_module(f, lowerupper, upperlower):
     f.write("pub mod conversions {")
     f.write("""
-    use cmp::{Equal, Less, Greater};
-    use slice::ImmutableVector;
-    use tuple::Tuple2;
-    use option::{Option, Some, None};
+    use core::cmp::{Equal, Less, Greater};
+    use core::slice::ImmutableVector;
+    use core::tuple::Tuple2;
+    use core::option::{Option, Some, None};
 
     pub fn to_lower(c: char) -> char {
         match bsearch_case_table(c, LuLl_table) {
@@ -226,189 +343,88 @@ def emit_conversions_module(f, lowerupper, upperlower):
         })
     }
 
-""");
-    emit_caseconversion_table(f, "LuLl", upperlower)
-    emit_caseconversion_table(f, "LlLu", lowerupper)
-    f.write("}\n")
-
-def emit_caseconversion_table(f, name, table):
-    f.write("    static %s_table : &'static [(char, char)] = &[\n" % name)
-    sorted_table = sorted(table.iteritems(), key=operator.itemgetter(0))
-    ix = 0
-    for key, value in sorted_table:
-        f.write(ch_prefix(ix))
-        f.write("(%s, %s)" % (escape_char(key), escape_char(value)))
-        ix += 1
-    f.write("\n    ];\n\n")
-
-def format_table_content(f, content, indent):
-    line = " "*indent
-    first = True
-    for chunk in content.split(","):
-        if len(line) + len(chunk) < 98:
-            if first:
-                line += chunk
-            else:
-                line += ", " + chunk
-            first = False
-        else:
-            f.write(line + ",\n")
-            line = " "*indent + chunk
-    f.write(line)
-
-def emit_core_norm_module(f, canon, compat):
-    canon_keys = canon.keys()
-    canon_keys.sort()
+""")
+    emit_table(f, "LuLl_table",
+        sorted(upperlower.iteritems(), key=operator.itemgetter(0)), is_pub=False)
+    emit_table(f, "LlLu_table",
+        sorted(lowerupper.iteritems(), key=operator.itemgetter(0)), is_pub=False)
+    f.write("}\n\n")
 
-    compat_keys = compat.keys()
-    compat_keys.sort()
-    f.write("pub mod normalization {\n");
-    f.write("    use option::Option;\n");
-    f.write("    use option::{Some, None};\n");
-    f.write("    use slice::ImmutableVector;\n");
+def emit_charwidth_module(f, width_table):
+    f.write("pub mod charwidth {\n")
+    f.write("    use core::option::{Option, Some, None};\n")
+    f.write("    use core::slice::ImmutableVector;\n")
     f.write("""
-    fn bsearch_table(c: char, r: &'static [(char, &'static [char])]) -> Option<&'static [char]> {
-        use cmp::{Equal, Less, Greater};
-        match r.bsearch(|&(val, _)| {
-            if c == val { Equal }
-            else if val < c { Less }
+    fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
+        use core::cmp::{Equal, Less, Greater};
+        match r.bsearch(|&(lo, hi, _, _)| {
+            if lo <= c && c <= hi { Equal }
+            else if hi < c { Less }
             else { Greater }
         }) {
             Some(idx) => {
-                let (_, result) = r[idx];
-                Some(result)
+                let (_, _, r_ncjk, r_cjk) = r[idx];
+                if is_cjk { r_cjk } else { r_ncjk }
             }
-            None => None
+            None => 1
         }
-    }\n\n
+    }
 """)
 
-    f.write("    // Canonical decompositions\n")
-    f.write("    static canonical_table : &'static [(char, &'static [char])] = &[\n")
-    data = ""
-    first = True
-    for char in canon_keys:
-        if not first:
-            data += ","
-        first = False
-        data += "(%s,&[" % escape_char(char)
-        first2 = True
-        for d in canon[char]:
-            if not first2:
-                data += ","
-            first2 = False
-            data += escape_char(d)
-        data += "])"
-    format_table_content(f, data, 8)
-    f.write("\n    ];\n\n")
-
-    f.write("    // Compatibility decompositions\n")
-    f.write("    static compatibility_table : &'static [(char, &'static [char])] = &[\n")
-    data = ""
-    first = True
-    for char in compat_keys:
-        if not first:
-            data += ","
-        first = False
-        data += "(%s,&[" % escape_char(char)
-        first2 = True
-        for d in compat[char]:
-            if not first2:
-                data += ","
-            first2 = False
-            data += escape_char(d)
-        data += "])"
-    format_table_content(f, data, 8)
-    f.write("\n    ];\n\n")
-
     f.write("""
-    pub fn decompose_canonical(c: char, i: |char|) { d(c, i, false); }
-
-    pub fn decompose_compatible(c: char, i: |char|) { d(c, i, true); }
-
-    fn d(c: char, i: |char|, k: bool) {
-        use iter::Iterator;
-
-        // 7-bit ASCII never decomposes
-        if c <= '\\x7f' { i(c); return; }
-
-        // Perform decomposition for Hangul
-        if (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT) {
-            decompose_hangul(c, i);
-            return;
+    pub fn width(c: char, is_cjk: bool) -> Option<uint> {
+        match c as uint {
+            _c @ 0 => Some(0),          // null is zero width
+            cu if cu < 0x20 => None,    // control sequences have no width
+            cu if cu < 0x7F => Some(1), // ASCII
+            cu if cu < 0xA0 => None,    // more control sequences
+            _ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as uint)
         }
+    }
 
-        // First check the canonical decompositions
-        match bsearch_table(c, canonical_table) {
-            Some(canon) => {
-                for x in canon.iter() {
-                    d(*x, |b| i(b), k);
-                }
-                return;
-            }
-            None => ()
-        }
+""")
 
-        // Bottom out if we're not doing compat.
-        if !k { i(c); return; }
+    f.write("    // character width table. Based on Markus Kuhn's free wcwidth() implementation,\n")
+    f.write("    //     http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c\n")
+    emit_table(f, "charwidth_table", width_table, "&'static [(char, char, u8, u8)]", is_pub=False,
+            pfun=lambda x: "(%s,%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), x[2], x[3]))
+    f.write("}\n")
 
-        // Then check the compatibility decompositions
-        match bsearch_table(c, compatibility_table) {
-            Some(compat) => {
-                for x in compat.iter() {
-                    d(*x, |b| i(b), k);
-                }
-                return;
-            }
-            None => ()
-        }
+def emit_norm_module(f, canon, compat, combine):
+    canon_keys = canon.keys()
+    canon_keys.sort()
 
-        // Finally bottom out.
-        i(c);
-    }
+    compat_keys = compat.keys()
+    compat_keys.sort()
 
-    // Constants from Unicode 6.2.0 Section 3.12 Conjoining Jamo Behavior
-    static S_BASE: u32 = 0xAC00;
-    static L_BASE: u32 = 0x1100;
-    static V_BASE: u32 = 0x1161;
-    static T_BASE: u32 = 0x11A7;
-    static L_COUNT: u32 = 19;
-    static V_COUNT: u32 = 21;
-    static T_COUNT: u32 = 28;
-    static N_COUNT: u32 = (V_COUNT * T_COUNT);
-    static S_COUNT: u32 = (L_COUNT * N_COUNT);
-
-    // Decompose a precomposed Hangul syllable
-    fn decompose_hangul(s: char, f: |char|) {
-        use cast::transmute;
-
-        let si = s as u32 - S_BASE;
-
-        let li = si / N_COUNT;
-        unsafe {
-            f(transmute(L_BASE + li));
-
-            let vi = (si % N_COUNT) / T_COUNT;
-            f(transmute(V_BASE + vi));
-
-            let ti = si % T_COUNT;
-            if ti > 0 {
-                f(transmute(T_BASE + ti));
-            }
-        }
-    }
-}
+    f.write("pub mod normalization {\n")
+
+    def mkdata_fun(table):
+        def f(char):
+            data = "(%s,&[" % escape_char(char)
+            first = True
+            for d in table[char]:
+                if not first:
+                    data += ","
+                first = False
+                data += escape_char(d)
+            data += "])"
+            return data
+        return f
 
-""")
+    f.write("    // Canonical decompositions\n")
+    emit_table(f, "canonical_table", canon_keys, "&'static [(char, &'static [char])]",
+        pfun=mkdata_fun(canon))
 
-def emit_std_norm_module(f, combine):
-    f.write("pub mod normalization {\n");
-    f.write("    use option::{Some, None};\n");
-    f.write("    use slice::ImmutableVector;\n");
+    f.write("    // Compatibility decompositions\n")
+    emit_table(f, "compatibility_table", compat_keys, "&'static [(char, &'static [char])]",
+        pfun=mkdata_fun(compat))
 
     f.write("""
     fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
-        use cmp::{Equal, Less, Greater};
+        use core::option::{Some, None};
+        use core::cmp::{Equal, Less, Greater};
+        use core::slice::ImmutableVector;
         match r.bsearch(|&(lo, hi, _)| {
             if lo <= c && c <= hi { Equal }
             else if hi < c { Less }
@@ -420,72 +436,122 @@ def emit_std_norm_module(f, combine):
             }
             None => 0
         }
-    }\n\n
+    }\n
 """)
 
-    f.write("    static combining_class_table : &'static [(char, char, u8)] = &[\n")
-    ix = 0
-    for pair in combine:
-        f.write(ch_prefix(ix))
-        f.write("(%s, %s, %s)" % (escape_char(pair[0]), escape_char(pair[1]), pair[2]))
-        ix += 1
-    f.write("\n    ];\n\n")
+    emit_table(f, "combining_class_table", combine, "&'static [(char, char, u8)]", is_pub=False,
+            pfun=lambda x: "(%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), x[2]))
 
     f.write("    pub fn canonical_combining_class(c: char) -> u8 {\n"
         + "        bsearch_range_value_table(c, combining_class_table)\n"
         + "    }\n")
-    f.write("}\n")
-
-
-preamble = '''// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
 
-// NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
-
-#![allow(missing_doc, non_uppercase_statics)]
+    f.write("""
+}
 
-'''
+""")
 
-(canon_decomp, compat_decomp, gencats,
- combines, lowerupper, upperlower) = load_unicode_data("UnicodeData.txt")
+def remove_from_wtable(wtable, val):
+    wtable_out = []
+    while wtable:
+        if wtable[0][1] < val:
+            wtable_out.append(wtable.pop(0))
+        elif wtable[0][0] > val:
+            break
+        else:
+            (wt_lo, wt_hi, width, width_cjk) = wtable.pop(0)
+            if wt_lo == wt_hi == val:
+                continue
+            elif wt_lo == val:
+                wtable_out.append((wt_lo+1, wt_hi, width, width_cjk))
+            elif wt_hi == val:
+                wtable_out.append((wt_lo, wt_hi-1, width, width_cjk))
+            else:
+                wtable_out.append((wt_lo, val-1, width, width_cjk))
+                wtable_out.append((val+1, wt_hi, width, width_cjk))
+    if wtable:
+        wtable_out.extend(wtable)
+    return wtable_out
+
+def optimize_width_table(wtable):
+    wtable_out = []
+    w_this = wtable.pop(0)
+    while wtable:
+        if w_this[1] == wtable[0][0] - 1 and w_this[2:3] == wtable[0][2:3]:
+            w_tmp = wtable.pop(0)
+            w_this = (w_this[0], w_tmp[1], w_tmp[2], w_tmp[3])
+        else:
+            wtable_out.append(w_this)
+            w_this = wtable.pop(0)
+    wtable_out.append(w_this)
+    return wtable_out
 
-def gen_core_unicode():
-    r = "core_unicode.rs"
+if __name__ == "__main__":
+    r = "unicode.rs"
     if os.path.exists(r):
-        os.remove(r);
+        os.remove(r)
     with open(r, "w") as rf:
-        # Preamble
+        # write the file's preamble
         rf.write(preamble)
 
-        emit_bsearch_range_table(rf);
-        emit_property_module(rf, "general_category", gencats)
-
-        emit_core_norm_module(rf, canon_decomp, compat_decomp)
+        # download and parse all the data
+        (canon_decomp, compat_decomp, gencats, combines,
+                lowerupper, upperlower) = load_unicode_data("UnicodeData.txt")
+        want_derived = ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase"]
+        other_derived = ["Default_Ignorable_Code_Point"]
+        derived = load_properties("DerivedCoreProperties.txt", want_derived + other_derived)
+        scripts = load_properties("Scripts.txt", [])
+        props = load_properties("PropList.txt",
+                ["White_Space", "Join_Control", "Noncharacter_Code_Point"])
+
+        # bsearch_range_table is used in all the property modules below
+        emit_bsearch_range_table(rf)
+
+        # all of these categories will also be available as \p{} in libregex
+        allcats = []
+        for (name, cat, pfuns) in ("general_category", gencats, ["N", "Cc"]), \
+                                  ("derived_property", derived, want_derived), \
+                                  ("script", scripts, []), \
+                                  ("property", props, ["White_Space"]):
+            emit_property_module(rf, name, cat, pfuns)
+            allcats.extend(map(lambda x: (x, name), cat))
+        allcats.sort(key=lambda c: c[0])
+
+        # the \w regex corresponds to Alphabetic + Mark + Decimal_Number +
+        # Connector_Punctuation + Join-Control according to UTS#18
+        # http://www.unicode.org/reports/tr18/#Compatibility_Properties
+        perl_words = []
+        for cat in derived["Alphabetic"], gencats["M"], gencats["Nd"], \
+                   gencats["Pc"], props["Join_Control"]:
+            perl_words.extend(ungroup_cat(cat))
+        perl_words = group_cat(perl_words)
+
+        # emit lookup tables for \p{}, along with \d, \w, and \s for libregex
+        emit_regex_module(rf, allcats, perl_words)
+
+        # normalizations and conversions module
+        emit_norm_module(rf, canon_decomp, compat_decomp, combines)
+        emit_conversions_module(rf, lowerupper, upperlower)
 
-        derived = load_properties("DerivedCoreProperties.txt",
-                ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase"])
+        # character width module
+        width_table = []
+        for zwcat in ["Me", "Mn", "Cf"]:
+            width_table.extend(map(lambda (lo, hi): (lo, hi, 0, 0), gencats[zwcat]))
+        width_table.append((4448, 4607, 0, 0))
 
-        emit_property_module(rf, "derived_property", derived)
+        # get widths, except those that are explicitly marked zero-width above
+        ea_widths = load_east_asian_width(["W", "F", "A"], ["Me", "Mn", "Cf"])
+        # these are doublewidth
+        for dwcat in ["W", "F"]:
+            width_table.extend(map(lambda (lo, hi): (lo, hi, 2, 2), ea_widths[dwcat]))
+        width_table.extend(map(lambda (lo, hi): (lo, hi, 1, 2), ea_widths["A"]))
 
-        props = load_properties("PropList.txt", ["White_Space"])
-        emit_property_module(rf, "property", props)
-        emit_conversions_module(rf, lowerupper, upperlower)
+        width_table.sort(key=lambda w: w[0])
 
-def gen_std_unicode():
-    r = "std_unicode.rs"
-    if os.path.exists(r):
-        os.remove(r);
-    with open(r, "w") as rf:
-        # Preamble
-        rf.write(preamble)
-        emit_std_norm_module(rf, combines)
+        # soft hyphen is not zero width in preformatted text; it's used to indicate
+        # a hyphen inserted to facilitate a linebreak.
+        width_table = remove_from_wtable(width_table, 173)
 
-gen_core_unicode()
-gen_std_unicode()
+        # optimize the width table by collapsing adjacent entities when possible
+        width_table = optimize_width_table(width_table)
+        emit_charwidth_module(rf, width_table)