0
0
mirror of https://github.com/rust-lang/rust.git synced 2024-11-29 10:05:55 +01:00

core: replace unicode match exprs with bsearch in const arrays, minor perf win.

This commit is contained in:
Graydon Hoare 2013-01-08 08:44:31 -08:00
parent 14b7277c4f
commit 5a3d26f271
4 changed files with 2616 additions and 4620 deletions

View File

@ -112,7 +112,49 @@ 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("""
pure fn bsearch_range_table(c: char, r: &[(char,char)]) -> bool {
use cmp::{EQ, LT, GT};
use vec::bsearch;
use option::None;
(do bsearch(r) |&(lo,hi)| {
if lo <= c && c <= hi { EQ }
else if hi < c { LT }
else { GT }
}) != None
}\n\n
""");
def emit_property_module(f, mod, tbl):
f.write("pub mod %s {\n" % mod)
keys = tbl.keys()
keys.sort()
emit_bsearch_range_table(f);
for cat in keys:
f.write(" const %s_table : &[(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 pure fn %s(c: char) -> bool {\n" % cat)
f.write(" bsearch_range_table(c, %s_table)\n" % cat)
f.write(" }\n\n")
f.write("}\n")
def emit_property_module_old(f, mod, tbl):
f.write("mod %s {\n" % mod)
keys = tbl.keys()
keys.sort()
@ -193,8 +235,9 @@ for i in [r]:
rf = open(r, "w")
(canon_decomp, compat_decomp, gencats) = load_unicode_data("UnicodeData.txt")
emit_decomp_module(rf, canon_decomp, compat_decomp)
emit_property_module(rf, "general_category", gencats)
#emit_decomp_module(rf, canon_decomp, compat_decomp)
derived = load_derived_core_properties("DerivedCoreProperties.txt")
emit_property_module(rf, "derived_property", derived)

File diff suppressed because it is too large Load Diff

View File

@ -1235,7 +1235,7 @@ pub fn rposition_between<T>(v: &[T], start: uint, end: uint,
* Returns the index where the comparator returned `Equal`, or `None` if
* not found.
*/
pub pure fn bsearch<T>(v: &[T], f: &fn(&T) -> Ordering) -> Option<uint> {
pub fn bsearch<T>(v: &[T], f: &fn(&T) -> Ordering) -> Option<uint> {
let mut base : uint = 0;
let mut lim : uint = v.len();
@ -1259,7 +1259,7 @@ pub pure fn bsearch<T>(v: &[T], f: &fn(&T) -> Ordering) -> Option<uint> {
*
* Returns the index of the element or None if not found.
*/
pub pure fn bsearch_elem<T:TotalOrd>(v: &[T], x: &T) -> Option<uint> {
pub fn bsearch_elem<T:TotalOrd>(v: &[T], x: &T) -> Option<uint> {
bsearch(v, |p| p.cmp(x))
}
@ -3752,47 +3752,47 @@ mod tests {
#[test]
fn test_bsearch_elem() {
fail_unless!(bsearch_elem([1,2,3,4,5], &5) == Some(4));
fail_unless!(bsearch_elem([1,2,3,4,5], &4) == Some(3));
fail_unless!(bsearch_elem([1,2,3,4,5], &3) == Some(2));
fail_unless!(bsearch_elem([1,2,3,4,5], &2) == Some(1));
fail_unless!(bsearch_elem([1,2,3,4,5], &1) == Some(0));
assert!(bsearch_elem([1,2,3,4,5], &5) == Some(4));
assert!(bsearch_elem([1,2,3,4,5], &4) == Some(3));
assert!(bsearch_elem([1,2,3,4,5], &3) == Some(2));
assert!(bsearch_elem([1,2,3,4,5], &2) == Some(1));
assert!(bsearch_elem([1,2,3,4,5], &1) == Some(0));
fail_unless!(bsearch_elem([2,4,6,8,10], &1) == None);
fail_unless!(bsearch_elem([2,4,6,8,10], &5) == None);
fail_unless!(bsearch_elem([2,4,6,8,10], &4) == Some(1));
fail_unless!(bsearch_elem([2,4,6,8,10], &10) == Some(4));
assert!(bsearch_elem([2,4,6,8,10], &1) == None);
assert!(bsearch_elem([2,4,6,8,10], &5) == None);
assert!(bsearch_elem([2,4,6,8,10], &4) == Some(1));
assert!(bsearch_elem([2,4,6,8,10], &10) == Some(4));
fail_unless!(bsearch_elem([2,4,6,8], &1) == None);
fail_unless!(bsearch_elem([2,4,6,8], &5) == None);
fail_unless!(bsearch_elem([2,4,6,8], &4) == Some(1));
fail_unless!(bsearch_elem([2,4,6,8], &8) == Some(3));
assert!(bsearch_elem([2,4,6,8], &1) == None);
assert!(bsearch_elem([2,4,6,8], &5) == None);
assert!(bsearch_elem([2,4,6,8], &4) == Some(1));
assert!(bsearch_elem([2,4,6,8], &8) == Some(3));
fail_unless!(bsearch_elem([2,4,6], &1) == None);
fail_unless!(bsearch_elem([2,4,6], &5) == None);
fail_unless!(bsearch_elem([2,4,6], &4) == Some(1));
fail_unless!(bsearch_elem([2,4,6], &6) == Some(2));
assert!(bsearch_elem([2,4,6], &1) == None);
assert!(bsearch_elem([2,4,6], &5) == None);
assert!(bsearch_elem([2,4,6], &4) == Some(1));
assert!(bsearch_elem([2,4,6], &6) == Some(2));
fail_unless!(bsearch_elem([2,4], &1) == None);
fail_unless!(bsearch_elem([2,4], &5) == None);
fail_unless!(bsearch_elem([2,4], &2) == Some(0));
fail_unless!(bsearch_elem([2,4], &4) == Some(1));
assert!(bsearch_elem([2,4], &1) == None);
assert!(bsearch_elem([2,4], &5) == None);
assert!(bsearch_elem([2,4], &2) == Some(0));
assert!(bsearch_elem([2,4], &4) == Some(1));
fail_unless!(bsearch_elem([2], &1) == None);
fail_unless!(bsearch_elem([2], &5) == None);
fail_unless!(bsearch_elem([2], &2) == Some(0));
assert!(bsearch_elem([2], &1) == None);
assert!(bsearch_elem([2], &5) == None);
assert!(bsearch_elem([2], &2) == Some(0));
fail_unless!(bsearch_elem([], &1) == None);
fail_unless!(bsearch_elem([], &5) == None);
assert!(bsearch_elem([], &1) == None);
assert!(bsearch_elem([], &5) == None);
fail_unless!(bsearch_elem([1,1,1,1,1], &1) != None);
fail_unless!(bsearch_elem([1,1,1,1,2], &1) != None);
fail_unless!(bsearch_elem([1,1,1,2,2], &1) != None);
fail_unless!(bsearch_elem([1,1,2,2,2], &1) != None);
fail_unless!(bsearch_elem([1,2,2,2,2], &1) == Some(0));
assert!(bsearch_elem([1,1,1,1,1], &1) != None);
assert!(bsearch_elem([1,1,1,1,2], &1) != None);
assert!(bsearch_elem([1,1,1,2,2], &1) != None);
assert!(bsearch_elem([1,1,2,2,2], &1) != None);
assert!(bsearch_elem([1,2,2,2,2], &1) == Some(0));
fail_unless!(bsearch_elem([1,2,3,4,5], &6) == None);
fail_unless!(bsearch_elem([1,2,3,4,5], &0) == None);
assert!(bsearch_elem([1,2,3,4,5], &6) == None);
assert!(bsearch_elem([1,2,3,4,5], &0) == None);
}
#[test]

View File

@ -535,7 +535,7 @@ fn ident_start(c: char) -> bool {
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| (c > 'z' && char::is_XID_start(c))
|| (c > '\x7f' && char::is_XID_start(c))
}
fn ident_continue(c: char) -> bool {
@ -543,7 +543,7 @@ fn ident_continue(c: char) -> bool {
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_'
|| (c > 'z' && char::is_XID_continue(c))
|| (c > '\x7f' && char::is_XID_continue(c))
}
// return the next token from the string