Skip to content

Commit 103ae30

Browse files
committed
Remove unneeded assert
1 parent d8402c2 commit 103ae30

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

build/ucd_generator/generators.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn generate_enum_table(code_dir : &std::path::Path, name : &str, enum_values
6868
write!(fd, "///\n")?;
6969
write!(fd, "/// # Returns\n")?;
7070
write!(fd, "/// A {} attribute of the Unicode code-point.\n", camel_name)?;
71-
write!(fd, "pub const fn get_{}(code_point: char) -> {}\n", name, camel_name)?;
71+
write!(fd, "#[must_use] pub const fn get_{}(code_point: char) -> {}\n", name, camel_name)?;
7272
write!(fd, "{{\n")?;
7373
write!(fd, " const INDEX_MASK : usize = (1 << {}_INDEX_BITS) - 1;\n", upper_name)?;
7474
write!(fd, " const COLUMN_MASK : usize = (1 << {}_COLUMN_BITS) - 1;\n\n", upper_name)?;
@@ -85,7 +85,7 @@ pub fn generate_enum_table(code_dir : &std::path::Path, name : &str, enum_values
8585
write!(fd, " let index_bit_offset = index_offset % 8;\n")?;
8686
write!(fd, " let mut index: usize = 0;\n")?;
8787
let index_bytes_to_read = (index_bits + 7) / 8 + 1;
88-
for i in 0..index_bytes_to_read {
88+
for i in (0..index_bytes_to_read).rev() {
8989
write!(fd, " index |= ({}_INDEX[index_byte_offset + {}] as usize) << {};\n", upper_name, i, i * 8)?;
9090
}
9191
write!(fd, " index >>= index_bit_offset;\n")?;
@@ -94,11 +94,10 @@ pub fn generate_enum_table(code_dir : &std::path::Path, name : &str, enum_values
9494
write!(fd, " let column_offset = (index * {}_CHUNK_SIZE + code_point_lo) * {}_COLUMN_BITS;\n", upper_name, upper_name)?;
9595
write!(fd, " let column_byte_offset = column_offset / 8;\n")?;
9696
write!(fd, " let column_bit_offset = column_offset % 8;\n\n")?;
97-
write!(fd, " // Explicitly assert to replace a double bound check with single bound check.\n")?;
98-
write!(fd, " assert!(column_byte_offset + 1 < {}_COLUMN.len());\n", upper_name)?;
97+
9998
write!(fd, " let mut value: usize = 0;\n")?;
10099
let column_bytes_to_read = (column_bits + 7) / 8 + 1;
101-
for i in 0..column_bytes_to_read {
100+
for i in (0..column_bytes_to_read).rev() {
102101
write!(fd, " value |= ({}_COLUMN[column_byte_offset + {}] as usize) << {};\n", upper_name, i, i * 8)?;
103102
}
104103
write!(fd, " value >>= column_bit_offset;\n")?;

src/east_asian_width.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub enum EastAsianWidth {
283283
///
284284
/// # Returns
285285
/// A EastAsianWidth attribute of the Unicode code-point.
286-
pub const fn get_east_asian_width(code_point: char) -> EastAsianWidth
286+
#[must_use] pub const fn get_east_asian_width(code_point: char) -> EastAsianWidth
287287
{
288288
const INDEX_MASK : usize = (1 << EAST_ASIAN_WIDTH_INDEX_BITS) - 1;
289289
const COLUMN_MASK : usize = (1 << EAST_ASIAN_WIDTH_COLUMN_BITS) - 1;
@@ -299,20 +299,18 @@ pub const fn get_east_asian_width(code_point: char) -> EastAsianWidth
299299
let index_byte_offset = index_offset / 8;
300300
let index_bit_offset = index_offset % 8;
301301
let mut index: usize = 0;
302-
index |= (EAST_ASIAN_WIDTH_INDEX[index_byte_offset + 0] as usize) << 0;
303302
index |= (EAST_ASIAN_WIDTH_INDEX[index_byte_offset + 1] as usize) << 8;
303+
index |= (EAST_ASIAN_WIDTH_INDEX[index_byte_offset + 0] as usize) << 0;
304304
index >>= index_bit_offset;
305305
index &= INDEX_MASK;
306306

307307
let column_offset = (index * EAST_ASIAN_WIDTH_CHUNK_SIZE + code_point_lo) * EAST_ASIAN_WIDTH_COLUMN_BITS;
308308
let column_byte_offset = column_offset / 8;
309309
let column_bit_offset = column_offset % 8;
310310

311-
// Explicitly assert to replace a double bound check with single bound check.
312-
assert!(column_byte_offset + 1 < EAST_ASIAN_WIDTH_COLUMN.len());
313311
let mut value: usize = 0;
314-
value |= (EAST_ASIAN_WIDTH_COLUMN[column_byte_offset + 0] as usize) << 0;
315312
value |= (EAST_ASIAN_WIDTH_COLUMN[column_byte_offset + 1] as usize) << 8;
313+
value |= (EAST_ASIAN_WIDTH_COLUMN[column_byte_offset + 0] as usize) << 0;
316314
value >>= column_bit_offset;
317315
value &= COLUMN_MASK;
318316

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ pub use east_asian_width::EastAsianWidth;
55
pub use east_asian_width::get_east_asian_width;
66

77
#[cfg(test)]
8-
mod tests {
8+
mod hikoru_ucdb {
99
use super::*;
1010

1111
#[test]
12-
fn east_asian_width() {
12+
fn east_asian_width()
13+
{
1314
assert_eq!(get_east_asian_width('a'), EastAsianWidth::Na);
1415
assert_eq!(get_east_asian_width('あ'), EastAsianWidth::W);
1516
}
17+
18+
#[test]
19+
fn east_asian_width_full_coverage()
20+
{
21+
for c in '\u{0000}'..='\u{d7ff}' {
22+
let _ = get_east_asian_width(c);
23+
}
24+
for c in '\u{e000}'..='\u{10ffff}' {
25+
let _ = get_east_asian_width(c);
26+
}
27+
}
1628
}

0 commit comments

Comments
 (0)