Skip to content

Commit 777dcab

Browse files
committed
Prepare to make mapping::lookup so it is a const function
This commit does everything except add the `const` annotation to the function signature. Adding `const` is blocked on rust-lang/rust#79549. See #49.
1 parent 264d843 commit 777dcab

File tree

4 files changed

+100
-98
lines changed

4 files changed

+100
-98
lines changed

scripts/gen_case_lookups.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@
7676
#[allow(clippy::match_same_arms)]
7777
#[allow(clippy::too_many_lines)]
7878
pub fn lookup(c: char, mode: Mode) -> Mapping {
79-
let char_bytes = u32::from(c).to_be_bytes();
79+
let codepoint = c as u32;
80+
let char_bytes = codepoint.to_be_bytes();
8081
let mid_byte = char_bytes[2];
8182
let high_bytes = u16::from_be_bytes([char_bytes[0], char_bytes[1]]);
8283
match (high_bytes, mid_byte) {
8384
(0x0000, 0x00) => match c {
8485
// Turkic mapping in ASCII range
8586
// 0049; T; 0131; # LATIN CAPITAL LETTER I
86-
'\\u{0049}' if mode == Mode::Turkic => Mapping::Single(0x0131),
87-
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase().into()),
87+
'\\u{0049}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x0131),
88+
c if c.is_ascii() => Mapping::Single(c.to_ascii_lowercase() as u32),
8889
AUTOGEN
8990

9091
last_high_bytes = 0x00
@@ -102,7 +103,7 @@
102103
high_bytes = ((start >> 16) & 0xFFFF)
103104

104105
if high_bytes != last_high_bytes || mid_byte != last_mid_byte
105-
rs.puts ' _ => Mapping::Single(c.into()),'
106+
rs.puts ' _ => Mapping::Single(codepoint),'
106107
rs.puts ' },'
107108
rs.puts " (0x#{high_bytes.to_s(16).upcase.rjust(4, '0')}, 0x#{mid_byte.to_s(16).upcase.rjust(2, '0')}) => match c {"
108109
last_high_bytes = high_bytes
@@ -116,22 +117,22 @@
116117
full = mapping[:full].map { |ch| ch.to_s(16).upcase.rjust(4, '0') }
117118
case full.length
118119
when 1
119-
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Single(0x#{full[0]}),"
120+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Single(0x#{full[0]}),"
120121
when 2
121-
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Double(0x#{full[0]}, 0x#{full[1]}),"
122+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Double(0x#{full[0]}, 0x#{full[1]}),"
122123
when 3
123-
rs.puts " '\\u{#{char}}' if mode == Mode::Full => Mapping::Triple(0x#{full[0]}, 0x#{full[1]}}, 0x#{full[2]}),"
124+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Full) => Mapping::Triple(0x#{full[0]}, 0x#{full[1]}}, 0x#{full[2]}),"
124125
else
125126
raise "Unsupported mapping length: #{map.inspect} for code #{code}"
126127
end
127128
turkic = mapping[:turkic].map { |ch| ch.to_s(16).upcase.rjust(4, '0') }
128129
case turkic.length
129130
when 1
130-
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Single(0x#{turkic[0]}),"
131+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Single(0x#{turkic[0]}),"
131132
when 2
132-
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Double(0x#{turkic[0]}, 0x#{turkic[1]}),"
133+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Double(0x#{turkic[0]}, 0x#{turkic[1]}),"
133134
when 3
134-
rs.puts " '\\u{#{char}}' if mode == Mode::Turkic => Mapping::Triple(0x#{turkic[0]}, 0x#{turkic[1]}, 0x#{turkic[2]}),"
135+
rs.puts " '\\u{#{char}}' if matches!(mode, Mode::Turkic) => Mapping::Triple(0x#{turkic[0]}, 0x#{turkic[1]}, 0x#{turkic[2]}),"
135136
else
136137
raise "Unsupported mapping length: #{map.inspect} for code #{code}"
137138
end
@@ -150,17 +151,17 @@
150151
rs.puts " '\\u{#{base}}' => Mapping::Single(0x#{full[0]}),"
151152
elsif full.length == 1
152153
finish = last.to_s(16).upcase.rjust(4, '0')
153-
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Single(u32::from(c).wrapping_#{op}(0x#{op_offset})),"
154+
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Single(codepoint.wrapping_#{op}(0x#{op_offset})),"
154155
elsif (last - start).zero? && full.length == 2
155156
rs.puts " '\\u{#{base}}' => Mapping::Double(0x#{full[0]}, 0x#{full[1]}),"
156157
elsif full.length == 2
157158
finish = last.to_s(16).upcase.rjust(4, '0')
158-
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Double(u32::from(c).wrapping_#{op}(0x#{op_offset}), 0x#{full[1]}),"
159+
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Double(codepoint.wrapping_#{op}(0x#{op_offset}), 0x#{full[1]}),"
159160
elsif (last - start).zero? && full.length == 3
160161
rs.puts " '\\u{#{base}}' => Mapping::Triple(0x#{full[0]}, 0x#{full[1]}, 0x#{full[2]}),"
161162
elsif full.length == 3
162163
finish = last.to_s(16).upcase.rjust(4, '0')
163-
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Triple(u32::from(c).wrapping_#{op}(0x#{op_offset}), 0x#{full[1]}, 0x#{full[2]}),"
164+
rs.puts " '\\u{#{base}}'..='\\u{#{finish}}' => Mapping::Triple(codepoint.wrapping_#{op}(0x#{op_offset}), 0x#{full[1]}, 0x#{full[2]}),"
164165
end
165166
elsif mapping.key?(:full)
166167
char = start.to_s(16).upcase.rjust(4, '0')
@@ -180,9 +181,9 @@
180181
end
181182
end
182183

183-
rs.puts ' _ => Mapping::Single(c.into()),'
184+
rs.puts ' _ => Mapping::Single(codepoint),'
184185
rs.puts ' },'
185-
rs.puts ' _ => Mapping::Single(c.into()),'
186+
rs.puts ' _ => Mapping::Single(codepoint),'
186187

187188
rs.puts ' }'
188189
rs.puts '}'

0 commit comments

Comments
 (0)