Skip to content

Commit 83f4bee

Browse files
committed
Add Unicode decomposition mappings to std::unicode
1 parent d4d856b commit 83f4bee

File tree

3 files changed

+2196
-32
lines changed

3 files changed

+2196
-32
lines changed

src/etc/unicode.py

Lines changed: 99 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -178,50 +178,118 @@ def emit_property_module_old(f, mod, tbl):
178178
f.write(" }\n\n")
179179
f.write("}\n")
180180

181+
def format_table_content(f, content, indent):
182+
line = " "*indent
183+
first = True
184+
for chunk in content.split(","):
185+
if len(line) + len(chunk) < 98:
186+
if first:
187+
line += chunk
188+
else:
189+
line += ", " + chunk
190+
first = False
191+
else:
192+
f.write(line + ",\n")
193+
line = " "*indent + chunk
194+
f.write(line)
195+
181196
def emit_decomp_module(f, canon, compat):
182197
canon_keys = canon.keys()
183198
canon_keys.sort()
184199

185200
compat_keys = compat.keys()
186201
compat_keys.sort()
187-
f.write("mod decompose {\n\n");
188-
f.write(" export canonical, compatibility;\n\n")
189-
f.write(" fn canonical(c: char, i: block(char)) "
190-
+ "{ d(c, i, false); }\n\n")
191-
f.write(" fn compatibility(c: char, i: block(char)) "
202+
f.write("pub mod decompose {\n");
203+
f.write(" use option::Option;\n");
204+
f.write(" use option::{Some, None};\n");
205+
f.write(" use vec::ImmutableVector;\n");
206+
f.write("""
207+
fn bsearch_table(c: char, r: &'static [(char, &'static [char])]) -> Option<&'static [char]> {
208+
use cmp::{Equal, Less, Greater};
209+
match r.bsearch(|&(val, _)| {
210+
if c == val { Equal }
211+
else if val < c { Less }
212+
else { Greater }
213+
}) {
214+
Some(idx) => {
215+
let (_, result) = r[idx];
216+
Some(result)
217+
}
218+
None => None
219+
}
220+
}\n\n
221+
""")
222+
f.write(" // Canonical decompositions\n")
223+
f.write(" static canonical_table : &'static [(char, &'static [char])] = &[\n")
224+
data = ""
225+
first = True
226+
for char in canon_keys:
227+
if not first:
228+
data += ","
229+
first = False
230+
data += "(%s,&[" % escape_char(char)
231+
first2 = True
232+
for d in canon[char]:
233+
if not first2:
234+
data += ","
235+
first2 = False
236+
data += escape_char(d)
237+
data += "])"
238+
format_table_content(f, data, 8)
239+
f.write("\n ];\n\n")
240+
f.write(" // Compatibility decompositions\n")
241+
f.write(" static compatibility_table : &'static [(char, &'static [char])] = &[\n")
242+
data = ""
243+
first = True
244+
for char in compat_keys:
245+
if not first:
246+
data += ","
247+
first = False
248+
data += "(%s,&[" % escape_char(char)
249+
first2 = True
250+
for d in compat[char]:
251+
if not first2:
252+
data += ","
253+
first2 = False
254+
data += escape_char(d)
255+
data += "])"
256+
format_table_content(f, data, 8)
257+
f.write("\n ];\n\n")
258+
f.write(" pub fn canonical(c: char, i: &fn(char)) "
259+
+ "{ d(c, i, false); }\n\n")
260+
f.write(" pub fn compatibility(c: char, i: &fn(char)) "
192261
+"{ d(c, i, true); }\n\n")
193-
f.write(" fn d(c: char, i: block(char), k: bool) {\n")
262+
f.write(" fn d(c: char, i: &fn(char), k: bool) {\n")
263+
f.write(" use iterator::Iterator;\n");
194264

195-
f.write(" if c <= '\\x7f' { i(c); ret; }\n")
265+
f.write(" if c <= '\\x7f' { i(c); return; }\n")
196266

197267
# First check the canonical decompositions
198-
f.write(" // Canonical decomposition\n")
199-
f.write(" alt c {\n")
200-
for char in canon_keys:
201-
f.write(" %s {\n" % escape_char(char))
202-
for d in canon[char]:
203-
f.write(" d(%s, i, k);\n"
204-
% escape_char(d))
205-
f.write(" }\n")
206-
207-
f.write(" _ { }\n")
208-
f.write(" }\n\n")
268+
f.write("""
269+
match bsearch_table(c, canonical_table) {
270+
Some(canon) => {
271+
for x in canon.iter() {
272+
d(*x, |b| i(b), k);
273+
}
274+
return;
275+
}
276+
None => ()
277+
}\n\n""")
209278

210279
# Bottom out if we're not doing compat.
211-
f.write(" if !k { i(c); ret; }\n\n ")
280+
f.write(" if !k { i(c); return; }\n")
212281

213282
# Then check the compatibility decompositions
214-
f.write(" // Compatibility decomposition\n")
215-
f.write(" alt c {\n")
216-
for char in compat_keys:
217-
f.write(" %s {\n" % escape_char(char))
218-
for d in compat[char]:
219-
f.write(" d(%s, i, k);\n"
220-
% escape_char(d))
221-
f.write(" }\n")
222-
223-
f.write(" _ { }\n")
224-
f.write(" }\n\n")
283+
f.write("""
284+
match bsearch_table(c, compatibility_table) {
285+
Some(compat) => {
286+
for x in compat.iter() {
287+
d(*x, |b| i(b), k);
288+
}
289+
return;
290+
}
291+
None => ()
292+
}\n\n""")
225293

226294
# Finally bottom out.
227295
f.write(" i(c);\n")
@@ -256,7 +324,7 @@ def emit_decomp_module(f, canon, compat):
256324

257325
emit_property_module(rf, "general_category", gencats)
258326

259-
#emit_decomp_module(rf, canon_decomp, compat_decomp)
327+
emit_decomp_module(rf, canon_decomp, compat_decomp)
260328

261329
derived = load_derived_core_properties("DerivedCoreProperties.txt")
262330
emit_property_module(rf, "derived_property", derived)

src/libstd/char.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use option::{None, Option, Some};
1414
use int;
1515
use str::StrSlice;
16-
use unicode::{derived_property, general_category};
16+
use unicode::{derived_property, general_category, decompose};
1717

1818
#[cfg(test)] use str::OwnedStr;
1919

@@ -202,6 +202,51 @@ pub fn from_digit(num: uint, radix: uint) -> Option<char> {
202202
}
203203
}
204204

205+
// Constants from Unicode 6.2.0 Section 3.12 Conjoining Jamo Behavior
206+
static S_BASE: uint = 0xAC00;
207+
static L_BASE: uint = 0x1100;
208+
static V_BASE: uint = 0x1161;
209+
static T_BASE: uint = 0x11A7;
210+
static L_COUNT: uint = 19;
211+
static V_COUNT: uint = 21;
212+
static T_COUNT: uint = 28;
213+
static N_COUNT: uint = (V_COUNT * T_COUNT);
214+
static S_COUNT: uint = (L_COUNT * N_COUNT);
215+
216+
// Decompose a precomposed Hangul syllable
217+
fn decompose_hangul(s: char, f: &fn(char)) {
218+
let si = s as uint - S_BASE;
219+
220+
let li = si / N_COUNT;
221+
f((L_BASE + li) as char);
222+
223+
let vi = (si % N_COUNT) / T_COUNT;
224+
f((V_BASE + vi) as char);
225+
226+
let ti = si % T_COUNT;
227+
if ti > 0 {
228+
f((T_BASE + ti) as char);
229+
}
230+
}
231+
232+
/// Returns the canonical decompostion of a character
233+
pub fn decompose_canonical(c: char, f: &fn(char)) {
234+
if (c as uint) < S_BASE || (c as uint) >= (S_BASE + S_COUNT) {
235+
decompose::canonical(c, f);
236+
} else {
237+
decompose_hangul(c, f);
238+
}
239+
}
240+
241+
/// Returns the compatibility decompostion of a character
242+
pub fn decompose_compatible(c: char, f: &fn(char)) {
243+
if (c as uint) < S_BASE || (c as uint) >= (S_BASE + S_COUNT) {
244+
decompose::compatibility(c, f);
245+
} else {
246+
decompose_hangul(c, f);
247+
}
248+
}
249+
205250
///
206251
/// Return the hexadecimal unicode escape of a char.
207252
///

0 commit comments

Comments
 (0)