@@ -271,43 +271,6 @@ def load_properties(f, interestingprops):
271
271
272
272
return props
273
273
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
-
311
274
def escape_char (c ):
312
275
return "'\\ u{%x}'" % c if c != 0 else "'\\ 0'"
313
276
@@ -316,12 +279,12 @@ def emit_bsearch_range_table(f):
316
279
fn bsearch_range_table(c: char, r: &'static [(char, char)]) -> bool {
317
280
use core::cmp::Ordering::{Equal, Less, Greater};
318
281
r.binary_search_by(|&(lo, hi)| {
319
- if lo <= c && c <= hi {
320
- Equal
282
+ if c < lo {
283
+ Greater
321
284
} else if hi < c {
322
285
Less
323
286
} else {
324
- Greater
287
+ Equal
325
288
}
326
289
})
327
290
.is_ok()
@@ -356,34 +319,25 @@ def emit_property_module(f, mod, tbl, emit):
356
319
def emit_conversions_module (f , to_upper , to_lower , to_title ):
357
320
f .write ("pub mod conversions {" )
358
321
f .write ("""
359
- use core::cmp::Ordering::{Equal, Less, Greater};
360
322
use core::option::Option;
361
323
use core::option::Option::{Some, None};
362
- use core::result::Result::{Ok, Err};
363
324
364
325
pub fn to_lower(c: char) -> [char; 3] {
365
326
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,
368
329
}
369
330
}
370
331
371
332
pub fn to_upper(c: char) -> [char; 3] {
372
333
match bsearch_case_table(c, to_uppercase_table) {
373
334
None => [c, '\\ 0', '\\ 0'],
374
- Some(index) => to_uppercase_table[index].1
335
+ Some(index) => to_uppercase_table[index].1,
375
336
}
376
337
}
377
338
378
339
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()
387
341
}
388
342
389
343
""" )
@@ -398,47 +352,6 @@ def emit_conversions_module(f, to_upper, to_lower, to_title):
398
352
is_pub = False , t_type = t_type , pfun = pfun )
399
353
f .write ("}\n \n " )
400
354
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
-
442
355
def emit_norm_module (f , canon , compat , combine , norm_props ):
443
356
canon_keys = canon .keys ()
444
357
canon_keys .sort ()
@@ -459,43 +372,6 @@ def emit_norm_module(f, canon, compat, combine, norm_props):
459
372
canon_comp_keys = canon_comp .keys ()
460
373
canon_comp_keys .sort ()
461
374
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
-
499
375
if __name__ == "__main__" :
500
376
r = "tables.rs"
501
377
if os .path .exists (r ):
0 commit comments