Commit 741e712
authored
refactor: simplify encoding mode detection logic (#201)
* Simplify encoding mode detection logic
Refactor to simplify checks for numeric and alphanumeric content.
* Fix empty string encoding regression: use Mode::BYTE()
Fixes a regression introduced in the previous refactor where an empty string ('') was incorrectly encoded as Mode::ALPHANUMERIC() instead of Mode::BYTE().
While technically valid, Mode::BYTE() is a more appropriate choice—it reflects general-purpose encoding and avoids the limitations of the alphanumeric character set.
This change also improves clarity and helps prevent similar regressions in the future.
Similarly, it ensures correct behavior for Encoder::chooseMode('', 'SHIFT-JIS').
* Add unit test for Encoder::chooseMode with empty string and Shift-JIS encoding
* Minor fixes and clarifications
getAlphanumericCode():
- rename parameter from $code to $byte for clarity and consistency with docblock
- simplify using null coalescing (also reduces lookups)
isOnlyDoubleByteKanji():
- update docblock to note empty string returns true (which is important to consider in the caller)
* Extract alphanumeric check into a dedicated method
* Optimize isOnlyAlphanumeric() using strspn-based technique
Implemented a technique that takes advantage of strspn() to optimize isOnlyAlphanumeric().
This approach is much, much faster (hundreds or even thousands of times) and highly scalable,
appearing to run in close to O(1).
* Use class const for alphanumeric characters1 parent 50e742e commit 741e712
2 files changed
Lines changed: 27 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
| |||
148 | 153 | | |
149 | 154 | | |
150 | 155 | | |
151 | | - | |
| 156 | + | |
152 | 157 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 158 | + | |
158 | 159 | | |
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
165 | 170 | | |
166 | 171 | | |
167 | 172 | | |
168 | 173 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 174 | + | |
| 175 | + | |
183 | 176 | | |
184 | 177 | | |
185 | | - | |
| 178 | + | |
186 | 179 | | |
187 | | - | |
188 | | - | |
189 | 180 | | |
190 | 181 | | |
191 | 182 | | |
| |||
205 | 196 | | |
206 | 197 | | |
207 | 198 | | |
208 | | - | |
| 199 | + | |
209 | 200 | | |
210 | 201 | | |
211 | 202 | | |
| |||
232 | 223 | | |
233 | 224 | | |
234 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
235 | 234 | | |
236 | 235 | | |
237 | 236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
| |||
77 | 81 | | |
78 | 82 | | |
79 | 83 | | |
80 | | - | |
81 | 84 | | |
82 | 85 | | |
83 | 86 | | |
| |||
0 commit comments