Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mapping::lookup const #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions scripts/gen_case_lookups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@
#[must_use]
#[allow(clippy::match_same_arms)]
#[allow(clippy::too_many_lines)]
pub fn lookup(c: char, mode: Mode) -> Mapping {
let char_bytes = u32::from(c).to_be_bytes();
pub const fn lookup(c: char, mode: Mode) -> Mapping {
let char_bytes = (c as u32).to_be_bytes();
let mid_byte = char_bytes[2];
let high_bytes = u16::from_be_bytes([char_bytes[0], char_bytes[1]]);
match (high_bytes, mid_byte) {
(0x0000, 0x00) => match c {
// Turkic mapping in ASCII range
// 0049; T; 0131; # LATIN CAPITAL LETTER I
'\\u{0049}' if mode == Mode::Turkic => Mapping::Single(0x0131),
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase().into()),
'\\u{0049}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x0131),
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase() as u32),
AUTOGEN

last_high_bytes = 0x00
Expand All @@ -102,7 +102,7 @@
high_bytes = ((start >> 16) & 0xFFFF)

if high_bytes != last_high_bytes || mid_byte != last_mid_byte
rs.puts ' _ => Mapping::Single(c.into()),'
rs.puts ' _ => Mapping::Single(c as u32),'
rs.puts ' },'
rs.puts " (0x#{high_bytes.to_s(16).upcase.rjust(4, '0')}, 0x#{mid_byte.to_s(16).upcase.rjust(2, '0')}) => match c {"
last_high_bytes = high_bytes
Expand All @@ -116,22 +116,22 @@
full = mapping[:full].map { |ch| ch.to_s(16).upcase.rjust(4, '0') }
case full.length
when 1
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Single(0x#{full[0]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Single(0x#{full[0]}),"
when 2
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Double(0x#{full[0]}, 0x#{full[1]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Double(0x#{full[0]}, 0x#{full[1]}),"
when 3
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Triple(0x#{full[0]}, 0x#{full[1]}}, 0x#{full[2]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Triple(0x#{full[0]}, 0x#{full[1]}}, 0x#{full[2]}),"
else
raise "Unsupported mapping length: #{map.inspect} for code #{code}"
end
turkic = mapping[:turkic].map { |ch| ch.to_s(16).upcase.rjust(4, '0') }
case turkic.length
when 1
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Single(0x#{turkic[0]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x#{turkic[0]}),"
when 2
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Double(0x#{turkic[0]}, 0x#{turkic[1]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Double(0x#{turkic[0]}, 0x#{turkic[1]}),"
when 3
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Triple(0x#{turkic[0]}, 0x#{turkic[1]}, 0x#{turkic[2]}),"
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Triple(0x#{turkic[0]}, 0x#{turkic[1]}, 0x#{turkic[2]}),"
else
raise "Unsupported mapping length: #{map.inspect} for code #{code}"
end
Expand Down Expand Up @@ -180,9 +180,9 @@
end
end

rs.puts ' _ => Mapping::Single(c.into()),'
rs.puts ' _ => Mapping::Single(c as u32),'
rs.puts ' },'
rs.puts ' _ => Mapping::Single(c.into()),'
rs.puts ' _ => Mapping::Single(c as u32),'

rs.puts ' }'
rs.puts '}'
Expand Down
64 changes: 32 additions & 32 deletions src/folding/mapping/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This source is autogenerated. Do not modify it directly.
// To make modfications to this code, see `scripts/gen_case_lookups.rb`.
//
// Last generated on 2020-08-22 00:52:59 UTC.
// Last generated on 2020-08-28 17:40:44 UTC.
//
// Unicode version:
// CaseFolding-13.0.0.txt
Expand All @@ -13,21 +13,21 @@ use super::{Mapping, Mode};
#[must_use]
#[allow(clippy::match_same_arms)]
#[allow(clippy::too_many_lines)]
pub fn lookup(c: char, mode: Mode) -> Mapping {
let char_bytes = u32::from(c).to_be_bytes();
pub const fn lookup(c: char, mode: Mode) -> Mapping {
let char_bytes = (c as u32).to_be_bytes();
let mid_byte = char_bytes[2];
let high_bytes = u16::from_be_bytes([char_bytes[0], char_bytes[1]]);
match (high_bytes, mid_byte) {
(0x0000, 0x00) => match c {
// Turkic mapping in ASCII range
// 0049; T; 0131; # LATIN CAPITAL LETTER I
'\u{0049}' if mode == Mode::Turkic => Mapping::Single(0x0131),
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase().into()),
'\u{0049}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x0131),
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase() as u32),
'\u{00B5}' => Mapping::Single(0x03BC),
'\u{00C0}'..='\u{00D6}' => Mapping::Single(u32::from(c).wrapping_add(0x0020)),
'\u{00D8}'..='\u{00DE}' => Mapping::Single(u32::from(c).wrapping_add(0x0020)),
'\u{00DF}' => Mapping::Double(0x0073, 0x0073),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x01) => match c {
'\u{0100}' => Mapping::Single(0x0101),
Expand All @@ -54,8 +54,8 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{012A}' => Mapping::Single(0x012B),
'\u{012C}' => Mapping::Single(0x012D),
'\u{012E}' => Mapping::Single(0x012F),
'\u{0130}' if mode == Mode::Full => Mapping::Double(0x0069, 0x0307),
'\u{0130}' if mode == Mode::Turkic => Mapping::Single(0x0069),
'\u{0130}' if matches!(mode, Mode::Full) => Mapping::Double(0x0069, 0x0307),
'\u{0130}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x0069),
'\u{0132}' => Mapping::Single(0x0133),
'\u{0134}' => Mapping::Single(0x0135),
'\u{0136}' => Mapping::Single(0x0137),
Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{01FA}' => Mapping::Single(0x01FB),
'\u{01FC}' => Mapping::Single(0x01FD),
'\u{01FE}' => Mapping::Single(0x01FF),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x02) => match c {
'\u{0200}' => Mapping::Single(0x0201),
Expand Down Expand Up @@ -205,7 +205,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{024A}' => Mapping::Single(0x024B),
'\u{024C}' => Mapping::Single(0x024D),
'\u{024E}' => Mapping::Single(0x024F),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x03) => match c {
'\u{0345}' => Mapping::Single(0x03B9),
Expand Down Expand Up @@ -247,7 +247,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{03F9}' => Mapping::Single(0x03F2),
'\u{03FA}' => Mapping::Single(0x03FB),
'\u{03FD}'..='\u{03FF}' => Mapping::Single(u32::from(c).wrapping_sub(0x0082)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x04) => match c {
'\u{0400}'..='\u{040F}' => Mapping::Single(u32::from(c).wrapping_add(0x0050)),
Expand Down Expand Up @@ -328,7 +328,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{04FA}' => Mapping::Single(0x04FB),
'\u{04FC}' => Mapping::Single(0x04FD),
'\u{04FE}' => Mapping::Single(0x04FF),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x05) => match c {
'\u{0500}' => Mapping::Single(0x0501),
Expand Down Expand Up @@ -357,17 +357,17 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{052E}' => Mapping::Single(0x052F),
'\u{0531}'..='\u{0556}' => Mapping::Single(u32::from(c).wrapping_add(0x0030)),
'\u{0587}' => Mapping::Double(0x0565, 0x0582),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x10) => match c {
'\u{10A0}'..='\u{10C5}' => Mapping::Single(u32::from(c).wrapping_add(0x1c60)),
'\u{10C7}' => Mapping::Single(0x2D27),
'\u{10CD}' => Mapping::Single(0x2D2D),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x13) => match c {
'\u{13F8}'..='\u{13FD}' => Mapping::Single(u32::from(c).wrapping_sub(0x0008)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x1C) => match c {
'\u{1C80}' => Mapping::Single(0x0432),
Expand All @@ -380,7 +380,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{1C88}' => Mapping::Single(0xA64B),
'\u{1C90}'..='\u{1CBA}' => Mapping::Single(u32::from(c).wrapping_sub(0x0bc0)),
'\u{1CBD}'..='\u{1CBF}' => Mapping::Single(u32::from(c).wrapping_sub(0x0bc0)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x1E) => match c {
'\u{1E00}' => Mapping::Single(0x1E01),
Expand Down Expand Up @@ -513,7 +513,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{1EFA}' => Mapping::Single(0x1EFB),
'\u{1EFC}' => Mapping::Single(0x1EFD),
'\u{1EFE}' => Mapping::Single(0x1EFF),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x1F) => match c {
'\u{1F08}'..='\u{1F0F}' => Mapping::Single(u32::from(c).wrapping_sub(0x0008)),
Expand Down Expand Up @@ -574,7 +574,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{1FF8}'..='\u{1FF9}' => Mapping::Single(u32::from(c).wrapping_sub(0x0080)),
'\u{1FFA}'..='\u{1FFB}' => Mapping::Single(u32::from(c).wrapping_sub(0x007e)),
'\u{1FFC}' => Mapping::Double(0x03C9, 0x03B9),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x21) => match c {
'\u{2126}' => Mapping::Single(0x03C9),
Expand All @@ -583,11 +583,11 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{2132}' => Mapping::Single(0x214E),
'\u{2160}'..='\u{216F}' => Mapping::Single(u32::from(c).wrapping_add(0x0010)),
'\u{2183}' => Mapping::Single(0x2184),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x24) => match c {
'\u{24B6}'..='\u{24CF}' => Mapping::Single(u32::from(c).wrapping_add(0x001a)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0x2C) => match c {
'\u{2C00}'..='\u{2C2E}' => Mapping::Single(u32::from(c).wrapping_add(0x0030)),
Expand Down Expand Up @@ -658,7 +658,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{2CEB}' => Mapping::Single(0x2CEC),
'\u{2CED}' => Mapping::Single(0x2CEE),
'\u{2CF2}' => Mapping::Single(0x2CF3),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0xA6) => match c {
'\u{A640}' => Mapping::Single(0xA641),
Expand Down Expand Up @@ -698,7 +698,7 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{A696}' => Mapping::Single(0xA697),
'\u{A698}' => Mapping::Single(0xA699),
'\u{A69A}' => Mapping::Single(0xA69B),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0xA7) => match c {
'\u{A722}' => Mapping::Single(0xA723),
Expand Down Expand Up @@ -783,11 +783,11 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{A7C7}' => Mapping::Single(0xA7C8),
'\u{A7C9}' => Mapping::Single(0xA7CA),
'\u{A7F5}' => Mapping::Single(0xA7F6),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0xAB) => match c {
'\u{AB70}'..='\u{ABBF}' => Mapping::Single(u32::from(c).wrapping_sub(0x97d0)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0xFB) => match c {
'\u{FB00}' => Mapping::Double(0x0066, 0x0066),
Expand All @@ -802,33 +802,33 @@ pub fn lookup(c: char, mode: Mode) -> Mapping {
'\u{FB15}' => Mapping::Double(0x0574, 0x056B),
'\u{FB16}' => Mapping::Double(0x057E, 0x0576),
'\u{FB17}' => Mapping::Double(0x0574, 0x056D),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0000, 0xFF) => match c {
'\u{FF21}'..='\u{FF3A}' => Mapping::Single(u32::from(c).wrapping_add(0x0020)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0001, 0x04) => match c {
'\u{10400}'..='\u{10427}' => Mapping::Single(u32::from(c).wrapping_add(0x0028)),
'\u{104B0}'..='\u{104D3}' => Mapping::Single(u32::from(c).wrapping_add(0x0028)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0001, 0x0C) => match c {
'\u{10C80}'..='\u{10CB2}' => Mapping::Single(u32::from(c).wrapping_add(0x0040)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0001, 0x18) => match c {
'\u{118A0}'..='\u{118BF}' => Mapping::Single(u32::from(c).wrapping_add(0x0020)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0001, 0x6E) => match c {
'\u{16E40}'..='\u{16E5F}' => Mapping::Single(u32::from(c).wrapping_add(0x0020)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
(0x0001, 0xE9) => match c {
'\u{1E900}'..='\u{1E921}' => Mapping::Single(u32::from(c).wrapping_add(0x0022)),
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
},
_ => Mapping::Single(c.into()),
_ => Mapping::Single(c as u32),
}
}
2 changes: 1 addition & 1 deletion tests/full_fold_exhaustive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This source is autogenerated. Do not modify it directly.
// To make modfications to this code, see `scripts/gen_case_lookups.rb`.
//
// Last generated on 2020-08-22 00:52:59 UTC.
// Last generated on 2020-08-28 17:40:44 UTC.
//
// Unicode version:
// CaseFolding-13.0.0.txt
Expand Down
2 changes: 1 addition & 1 deletion tests/full_turkic_fold_exhaustive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This source is autogenerated. Do not modify it directly.
// To make modfications to this code, see `scripts/gen_case_lookups.rb`.
//
// Last generated on 2020-08-22 00:52:59 UTC.
// Last generated on 2020-08-28 17:40:44 UTC.
//
// Unicode version:
// CaseFolding-13.0.0.txt
Expand Down