Skip to content

Commit 4bcd646

Browse files
committed
Make core::unicode::printable more readable.
1 parent f820b75 commit 4bcd646

File tree

2 files changed

+49
-83
lines changed

2 files changed

+49
-83
lines changed

library/core/src/unicode/printable.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,30 +178,30 @@ def main():
178178
else:
179179
normal0.append((a, b - a))
180180

181-
singletons0u, singletons0l = compress_singletons(singletons0)
182-
singletons1u, singletons1l = compress_singletons(singletons1)
181+
SINGLETONS0_UPPER, SINGLETONS0_LOWER = compress_singletons(singletons0)
182+
SINGLETONS1_UPPER, SINGLETONS1_LOWER = compress_singletons(singletons1)
183183
normal0 = compress_normal(normal0)
184184
normal1 = compress_normal(normal1)
185185

186186
print("""\
187187
// NOTE: The following code was generated by "library/core/src/unicode/printable.py",
188188
// do not edit directly!
189189
190-
fn check(x: u16, singletonuppers: &[(u8, u8)], singletonlowers: &[u8], normal: &[u8]) -> bool {
191-
let xupper = (x >> 8) as u8;
192-
let mut lowerstart = 0;
193-
for &(upper, lowercount) in singletonuppers {
194-
let lowerend = lowerstart + lowercount as usize;
195-
if xupper == upper {
196-
for &lower in &singletonlowers[lowerstart..lowerend] {
190+
fn check(x: u16, singletons_upper: &[(u8, u8)], singletons_lower: &[u8], normal: &[u8]) -> bool {
191+
let x_upper = (x >> 8) as u8;
192+
let mut lower_start = 0;
193+
for &(upper, lower_count) in singletons_upper {
194+
let lower_end = lower_start + lower_count as usize;
195+
if x_upper == upper {
196+
for &lower in &singletons_lower[lower_start..lower_end] {
197197
if lower == x as u8 {
198198
return false;
199199
}
200200
}
201-
} else if xupper < upper {
201+
} else if x_upper < upper {
202202
break;
203203
}
204-
lowerstart = lowerend;
204+
lower_start = lower_end;
205205
}
206206
207207
let mut x = x as i32;
@@ -226,30 +226,22 @@ def main():
226226
let x = x as u32;
227227
let lower = x as u16;
228228
229-
if x < 32 {
230-
// ASCII fast path
231-
false
232-
} else if x < 127 {
233-
// ASCII fast path
234-
true
235-
} else if x < 0x10000 {
236-
check(lower, SINGLETONS0U, SINGLETONS0L, NORMAL0)
237-
} else if x < 0x20000 {
238-
check(lower, SINGLETONS1U, SINGLETONS1L, NORMAL1)
239-
} else {\
229+
match x {
230+
..32 => false, // ASCII fast path
231+
..127 => true, // ASCII fast path
232+
..0x10000 => check(lower, SINGLETONS0_UPPER, SINGLETONS0_LOWER, NORMAL0),
233+
..0x20000 => check(lower, SINGLETONS1_UPPER, SINGLETONS1_LOWER, NORMAL1),\
240234
""")
241235
for a, b in extra:
242-
print(" if 0x{:x} <= x && x < 0x{:x} {{".format(a, a + b))
243-
print(" return false;")
244-
print(" }")
236+
print(" 0x{:x}..0x{:x} => false,".format(a, a + b))
245237
print("""\
246-
true
238+
_ => true,
247239
}
248240
}\
249241
""")
250242
print()
251-
print_singletons(singletons0u, singletons0l, "SINGLETONS0U", "SINGLETONS0L")
252-
print_singletons(singletons1u, singletons1l, "SINGLETONS1U", "SINGLETONS1L")
243+
print_singletons(SINGLETONS0_UPPER, SINGLETONS0_LOWER, "SINGLETONS0_UPPER", "SINGLETONS0_LOWER")
244+
print_singletons(SINGLETONS1_UPPER, SINGLETONS1_LOWER, "SINGLETONS1_UPPER", "SINGLETONS1_LOWER")
253245
print_normal(normal0, "NORMAL0")
254246
print_normal(normal1, "NORMAL1")
255247

library/core/src/unicode/printable.rs

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// NOTE: The following code was generated by "library/core/src/unicode/printable.py",
22
// do not edit directly!
33

4-
fn check(x: u16, singletonuppers: &[(u8, u8)], singletonlowers: &[u8], normal: &[u8]) -> bool {
5-
let xupper = (x >> 8) as u8;
6-
let mut lowerstart = 0;
7-
for &(upper, lowercount) in singletonuppers {
8-
let lowerend = lowerstart + lowercount as usize;
9-
if xupper == upper {
10-
for &lower in &singletonlowers[lowerstart..lowerend] {
4+
fn check(x: u16, singletons_upper: &[(u8, u8)], singletons_lower: &[u8], normal: &[u8]) -> bool {
5+
let x_upper = (x >> 8) as u8;
6+
let mut lower_start = 0;
7+
for &(upper, lower_count) in singletons_upper {
8+
let lower_end = lower_start + lower_count as usize;
9+
if x_upper == upper {
10+
for &lower in &singletons_lower[lower_start..lower_end] {
1111
if lower == x as u8 {
1212
return false;
1313
}
1414
}
15-
} else if xupper < upper {
15+
} else if x_upper < upper {
1616
break;
1717
}
18-
lowerstart = lowerend;
18+
lower_start = lower_end;
1919
}
2020

2121
let mut x = x as i32;
@@ -40,53 +40,27 @@ pub(crate) fn is_printable(x: char) -> bool {
4040
let x = x as u32;
4141
let lower = x as u16;
4242

43-
if x < 32 {
44-
// ASCII fast path
45-
false
46-
} else if x < 127 {
47-
// ASCII fast path
48-
true
49-
} else if x < 0x10000 {
50-
check(lower, SINGLETONS0U, SINGLETONS0L, NORMAL0)
51-
} else if x < 0x20000 {
52-
check(lower, SINGLETONS1U, SINGLETONS1L, NORMAL1)
53-
} else {
54-
if 0x2a6e0 <= x && x < 0x2a700 {
55-
return false;
56-
}
57-
if 0x2b73a <= x && x < 0x2b740 {
58-
return false;
59-
}
60-
if 0x2b81e <= x && x < 0x2b820 {
61-
return false;
62-
}
63-
if 0x2cea2 <= x && x < 0x2ceb0 {
64-
return false;
65-
}
66-
if 0x2ebe1 <= x && x < 0x2ebf0 {
67-
return false;
68-
}
69-
if 0x2ee5e <= x && x < 0x2f800 {
70-
return false;
71-
}
72-
if 0x2fa1e <= x && x < 0x30000 {
73-
return false;
74-
}
75-
if 0x3134b <= x && x < 0x31350 {
76-
return false;
77-
}
78-
if 0x323b0 <= x && x < 0xe0100 {
79-
return false;
80-
}
81-
if 0xe01f0 <= x && x < 0x110000 {
82-
return false;
83-
}
84-
true
43+
match x {
44+
..32 => false, // ASCII fast path
45+
..127 => true, // ASCII fast path
46+
..0x10000 => check(lower, SINGLETONS0_UPPER, SINGLETONS0_LOWER, NORMAL0),
47+
..0x20000 => check(lower, SINGLETONS1_UPPER, SINGLETONS1_LOWER, NORMAL1),
48+
0x2a6e0..0x2a700 => false,
49+
0x2b73a..0x2b740 => false,
50+
0x2b81e..0x2b820 => false,
51+
0x2cea2..0x2ceb0 => false,
52+
0x2ebe1..0x2ebf0 => false,
53+
0x2ee5e..0x2f800 => false,
54+
0x2fa1e..0x30000 => false,
55+
0x3134b..0x31350 => false,
56+
0x323b0..0xe0100 => false,
57+
0xe01f0..0x110000 => false,
58+
_ => true,
8559
}
8660
}
8761

8862
#[rustfmt::skip]
89-
const SINGLETONS0U: &[(u8, u8)] = &[
63+
const SINGLETONS0_UPPER: &[(u8, u8)] = &[
9064
(0x00, 1),
9165
(0x03, 5),
9266
(0x05, 6),
@@ -129,7 +103,7 @@ const SINGLETONS0U: &[(u8, u8)] = &[
129103
(0xff, 9),
130104
];
131105
#[rustfmt::skip]
132-
const SINGLETONS0L: &[u8] = &[
106+
const SINGLETONS0_LOWER: &[u8] = &[
133107
0xad, 0x78, 0x79, 0x8b, 0x8d, 0xa2, 0x30, 0x57,
134108
0x58, 0x8b, 0x8c, 0x90, 0x1c, 0xdd, 0x0e, 0x0f,
135109
0x4b, 0x4c, 0xfb, 0xfc, 0x2e, 0x2f, 0x3f, 0x5c,
@@ -169,7 +143,7 @@ const SINGLETONS0L: &[u8] = &[
169143
0xfe, 0xff,
170144
];
171145
#[rustfmt::skip]
172-
const SINGLETONS1U: &[(u8, u8)] = &[
146+
const SINGLETONS1_UPPER: &[(u8, u8)] = &[
173147
(0x00, 6),
174148
(0x01, 1),
175149
(0x03, 1),
@@ -216,7 +190,7 @@ const SINGLETONS1U: &[(u8, u8)] = &[
216190
(0xfb, 1),
217191
];
218192
#[rustfmt::skip]
219-
const SINGLETONS1L: &[u8] = &[
193+
const SINGLETONS1_LOWER: &[u8] = &[
220194
0x0c, 0x27, 0x3b, 0x3e, 0x4e, 0x4f, 0x8f, 0x9e,
221195
0x9e, 0x9f, 0x7b, 0x8b, 0x93, 0x96, 0xa2, 0xb2,
222196
0xba, 0x86, 0xb1, 0x06, 0x07, 0x09, 0x36, 0x3d,

0 commit comments

Comments
 (0)