Skip to content

Commit 7cffc9b

Browse files
committed
Auto merge of #30695 - ranma42:cleanup-unicode, r=alexcrichton
and the associated update of tables.rs The last commit is related to my comment to #29734.
2 parents 3246eae + 3fff634 commit 7cffc9b

File tree

2 files changed

+22
-150
lines changed

2 files changed

+22
-150
lines changed

src/etc/unicode.py

+7-131
Original file line numberDiff line numberDiff line change
@@ -271,43 +271,6 @@ def load_properties(f, interestingprops):
271271

272272
return props
273273

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"
277-
fetch(f)
278-
widths = {}
279-
re1 = re.compile("^([0-9A-F]+);(\w+) +# (\w+)")
280-
re2 = re.compile("^([0-9A-F]+)\.\.([0-9A-F]+);(\w+) +# (\w+)")
281-
282-
for line in fileinput.input(f):
283-
width = None
284-
d_lo = 0
285-
d_hi = 0
286-
cat = None
287-
m = re1.match(line)
288-
if m:
289-
d_lo = m.group(1)
290-
d_hi = m.group(1)
291-
width = m.group(2)
292-
cat = m.group(3)
293-
else:
294-
m = re2.match(line)
295-
if m:
296-
d_lo = m.group(1)
297-
d_hi = m.group(2)
298-
width = m.group(3)
299-
cat = m.group(4)
300-
else:
301-
continue
302-
if cat in except_cats or width not in want_widths:
303-
continue
304-
d_lo = int(d_lo, 16)
305-
d_hi = int(d_hi, 16)
306-
if width not in widths:
307-
widths[width] = []
308-
widths[width].append((d_lo, d_hi))
309-
return widths
310-
311274
def escape_char(c):
312275
return "'\\u{%x}'" % c if c != 0 else "'\\0'"
313276

@@ -316,12 +279,12 @@ def emit_bsearch_range_table(f):
316279
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
317280
use core::cmp::Ordering::{Equal, Less, Greater};
318281
r.binary_search_by(|&(lo, hi)| {
319-
if lo <= c && c <= hi {
320-
Equal
282+
if c < lo {
283+
Greater
321284
} else if hi < c {
322285
Less
323286
} else {
324-
Greater
287+
Equal
325288
}
326289
})
327290
.is_ok()
@@ -356,34 +319,25 @@ def emit_property_module(f, mod, tbl, emit):
356319
def emit_conversions_module(f, to_upper, to_lower, to_title):
357320
f.write("pub mod conversions {")
358321
f.write("""
359-
use core::cmp::Ordering::{Equal, Less, Greater};
360322
use core::option::Option;
361323
use core::option::Option::{Some, None};
362-
use core::result::Result::{Ok, Err};
363324
364325
pub fn to_lower(c: char) -> [char; 3] {
365326
match bsearch_case_table(c, to_lowercase_table) {
366-
None => [c, '\\0', '\\0'],
367-
Some(index) => to_lowercase_table[index].1
327+
None => [c, '\\0', '\\0'],
328+
Some(index) => to_lowercase_table[index].1,
368329
}
369330
}
370331
371332
pub fn to_upper(c: char) -> [char; 3] {
372333
match bsearch_case_table(c, to_uppercase_table) {
373334
None => [c, '\\0', '\\0'],
374-
Some(index) => to_uppercase_table[index].1
335+
Some(index) => to_uppercase_table[index].1,
375336
}
376337
}
377338
378339
fn bsearch_case_table(c: char, table: &'static [(char, [char; 3])]) -> Option<usize> {
379-
match table.binary_search_by(|&(key, _)| {
380-
if c == key { Equal }
381-
else if key < c { Less }
382-
else { Greater }
383-
}) {
384-
Ok(i) => Some(i),
385-
Err(_) => None,
386-
}
340+
table.binary_search_by(|&(key, _)| key.cmp(&c)).ok()
387341
}
388342
389343
""")
@@ -398,47 +352,6 @@ def emit_conversions_module(f, to_upper, to_lower, to_title):
398352
is_pub=False, t_type = t_type, pfun=pfun)
399353
f.write("}\n\n")
400354

401-
def emit_charwidth_module(f, width_table):
402-
f.write("pub mod charwidth {\n")
403-
f.write(" use core::option::Option;\n")
404-
f.write(" use core::option::Option::{Some, None};\n")
405-
f.write(" use core::result::Result::{Ok, Err};\n")
406-
f.write("""
407-
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
408-
use core::cmp::Ordering::{Equal, Less, Greater};
409-
match r.binary_search_by(|&(lo, hi, _, _)| {
410-
if lo <= c && c <= hi { Equal }
411-
else if hi < c { Less }
412-
else { Greater }
413-
}) {
414-
Ok(idx) => {
415-
let (_, _, r_ncjk, r_cjk) = r[idx];
416-
if is_cjk { r_cjk } else { r_ncjk }
417-
}
418-
Err(_) => 1
419-
}
420-
}
421-
""")
422-
423-
f.write("""
424-
pub fn width(c: char, is_cjk: bool) -> Option<usize> {
425-
match c as usize {
426-
_c @ 0 => Some(0), // null is zero width
427-
cu if cu < 0x20 => None, // control sequences have no width
428-
cu if cu < 0x7F => Some(1), // ASCII
429-
cu if cu < 0xA0 => None, // more control sequences
430-
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as usize)
431-
}
432-
}
433-
434-
""")
435-
436-
f.write(" // character width table. Based on Markus Kuhn's free wcwidth() implementation,\n")
437-
f.write(" // http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c\n")
438-
emit_table(f, "charwidth_table", width_table, "&'static [(char, char, u8, u8)]", is_pub=False,
439-
pfun=lambda x: "(%s,%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), x[2], x[3]))
440-
f.write("}\n\n")
441-
442355
def emit_norm_module(f, canon, compat, combine, norm_props):
443356
canon_keys = canon.keys()
444357
canon_keys.sort()
@@ -459,43 +372,6 @@ def emit_norm_module(f, canon, compat, combine, norm_props):
459372
canon_comp_keys = canon_comp.keys()
460373
canon_comp_keys.sort()
461374

462-
def remove_from_wtable(wtable, val):
463-
wtable_out = []
464-
while wtable:
465-
if wtable[0][1] < val:
466-
wtable_out.append(wtable.pop(0))
467-
elif wtable[0][0] > val:
468-
break
469-
else:
470-
(wt_lo, wt_hi, width, width_cjk) = wtable.pop(0)
471-
if wt_lo == wt_hi == val:
472-
continue
473-
elif wt_lo == val:
474-
wtable_out.append((wt_lo+1, wt_hi, width, width_cjk))
475-
elif wt_hi == val:
476-
wtable_out.append((wt_lo, wt_hi-1, width, width_cjk))
477-
else:
478-
wtable_out.append((wt_lo, val-1, width, width_cjk))
479-
wtable_out.append((val+1, wt_hi, width, width_cjk))
480-
if wtable:
481-
wtable_out.extend(wtable)
482-
return wtable_out
483-
484-
485-
486-
def optimize_width_table(wtable):
487-
wtable_out = []
488-
w_this = wtable.pop(0)
489-
while wtable:
490-
if w_this[1] == wtable[0][0] - 1 and w_this[2:3] == wtable[0][2:3]:
491-
w_tmp = wtable.pop(0)
492-
w_this = (w_this[0], w_tmp[1], w_tmp[2], w_tmp[3])
493-
else:
494-
wtable_out.append(w_this)
495-
w_this = wtable.pop(0)
496-
wtable_out.append(w_this)
497-
return wtable_out
498-
499375
if __name__ == "__main__":
500376
r = "tables.rs"
501377
if os.path.exists(r):

src/librustc_unicode/tables.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
/// that the unicode parts of `CharExt` and `UnicodeStrPrelude` traits are based on.
1717
pub const UNICODE_VERSION: (u64, u64, u64) = (8, 0, 0);
1818

19-
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
19+
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
2020
use core::cmp::Ordering::{Equal, Less, Greater};
21-
r.binary_search_by(|&(lo,hi)| {
22-
if lo <= c && c <= hi { Equal }
23-
else if hi < c { Less }
24-
else { Greater }
25-
}).is_ok()
21+
r.binary_search_by(|&(lo, hi)| {
22+
if c < lo {
23+
Greater
24+
} else if hi < c {
25+
Less
26+
} else {
27+
Equal
28+
}
29+
})
30+
.is_ok()
2631
}
2732

2833
pub mod general_category {
@@ -1188,34 +1193,25 @@ pub mod property {
11881193
}
11891194

11901195
pub mod conversions {
1191-
use core::cmp::Ordering::{Equal, Less, Greater};
11921196
use core::option::Option;
11931197
use core::option::Option::{Some, None};
1194-
use core::result::Result::{Ok, Err};
11951198

11961199
pub fn to_lower(c: char) -> [char; 3] {
11971200
match bsearch_case_table(c, to_lowercase_table) {
1198-
None => [c, '\0', '\0'],
1199-
Some(index) => to_lowercase_table[index].1
1201+
None => [c, '\0', '\0'],
1202+
Some(index) => to_lowercase_table[index].1,
12001203
}
12011204
}
12021205

12031206
pub fn to_upper(c: char) -> [char; 3] {
12041207
match bsearch_case_table(c, to_uppercase_table) {
12051208
None => [c, '\0', '\0'],
1206-
Some(index) => to_uppercase_table[index].1
1209+
Some(index) => to_uppercase_table[index].1,
12071210
}
12081211
}
12091212

12101213
fn bsearch_case_table(c: char, table: &'static [(char, [char; 3])]) -> Option<usize> {
1211-
match table.binary_search_by(|&(key, _)| {
1212-
if c == key { Equal }
1213-
else if key < c { Less }
1214-
else { Greater }
1215-
}) {
1216-
Ok(i) => Some(i),
1217-
Err(_) => None,
1218-
}
1214+
table.binary_search_by(|&(key, _)| key.cmp(&c)).ok()
12191215
}
12201216

12211217
const to_lowercase_table: &'static [(char, [char; 3])] = &[

0 commit comments

Comments
 (0)