-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift_jis.rs
More file actions
442 lines (376 loc) · 12.8 KB
/
shift_jis.rs
File metadata and controls
442 lines (376 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Shift_JIS encoding.
//!
//! Shift_JIS is a variable-width encoding used for Japanese text, primarily
//! on Windows systems. It is compatible with JIS X 0201 (ASCII + half-width
//! katakana) and JIS X 0208 (Japanese characters).
//!
//! # Structure
//!
//! Shift_JIS uses variable-width encoding:
//! - 1 byte: ASCII (0x00-0x7F, except 0x5C and 0x7E which may vary)
//! - 1 byte: Half-width katakana (0xA1-0xDF)
//! - 2 bytes: JIS X 0208 (lead 0x81-0x9F or 0xE0-0xFC, trail 0x40-0x7E or 0x80-0xFC)
//!
//! This implementation follows the WHATWG encoding standard.
use alloc::vec::Vec;
use crate::encoding::Encoding;
use crate::error::EncodingError;
// Include generated JIS X 0208 tables (shared with EUC-JP)
include!(concat!(env!("OUT_DIR"), "/jis0208_tables.rs"));
/// Shift_JIS encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ShiftJis;
/// Look up a character by pointer in the JIS X 0208 decode table.
fn jis0208_lookup(pointer: u16) -> Option<char> {
JIS0208_DECODE
.binary_search_by_key(&pointer, |&(p, _)| p)
.ok()
.map(|idx| JIS0208_DECODE[idx].1)
}
/// Look up a pointer by character in the JIS X 0208 encode table.
fn jis0208_encode_lookup(c: char) -> Option<u16> {
JIS0208_ENCODE
.binary_search_by_key(&c, |&(ch, _)| ch)
.ok()
.map(|idx| JIS0208_ENCODE[idx].1)
}
/// Check if a byte is a valid lead byte in Shift_JIS.
#[inline]
fn is_lead_byte(b: u8) -> bool {
(0x81..=0x9F).contains(&b) || (0xE0..=0xFC).contains(&b)
}
/// Check if a byte is a valid trail byte in Shift_JIS.
#[inline]
fn is_trail_byte(b: u8) -> bool {
(0x40..=0x7E).contains(&b) || (0x80..=0xFC).contains(&b)
}
impl Encoding for ShiftJis {
const NAME: &'static str = "Shift_JIS";
const IS_FIXED_WIDTH: bool = false;
const BYTES_PER_CHAR: Option<usize> = None;
const MAX_CHAR_LEN: usize = 2;
fn validate(bytes: &[u8]) -> Result<(), EncodingError> {
let mut i = 0;
while i < bytes.len() {
let b1 = bytes[i];
if b1 < 0x80 {
// ASCII
i += 1;
} else if (0xA1..=0xDF).contains(&b1) {
// Half-width katakana (single byte)
i += 1;
} else if is_lead_byte(b1) {
// Lead byte of 2-byte sequence
if i + 1 >= bytes.len() {
return Err(EncodingError::new(i, Some(1)));
}
let b2 = bytes[i + 1];
if !is_trail_byte(b2) {
return Err(EncodingError::new(i, Some(2)));
}
// Validate the sequence maps to a valid character
let pointer = pointer_from_bytes(b1, b2);
if jis0208_lookup(pointer).is_none() {
return Err(EncodingError::new(i, Some(2)));
}
i += 2;
} else if b1 == 0x80 || (0xA0..=0xA0).contains(&b1) || b1 > 0xFC {
// Invalid single bytes
return Err(EncodingError::new(i, Some(1)));
} else {
return Err(EncodingError::new(i, Some(1)));
}
}
Ok(())
}
fn decode_char_at(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
let b1 = *bytes.get(offset)?;
// ASCII (0x00-0x7F)
if b1 < 0x80 {
return Some((b1 as char, offset + 1));
}
// Half-width katakana (0xA1-0xDF) -> U+FF61-U+FF9F
if (0xA1..=0xDF).contains(&b1) {
let c = char::from_u32(0xFF61 + (b1 - 0xA1) as u32)?;
return Some((c, offset + 1));
}
// 2-byte JIS X 0208 sequence
if is_lead_byte(b1) {
let b2 = *bytes.get(offset + 1)?;
if !is_trail_byte(b2) {
return None;
}
let pointer = pointer_from_bytes(b1, b2);
let c = jis0208_lookup(pointer)?;
return Some((c, offset + 2));
}
None
}
fn encoded_len(c: char) -> usize {
let cp = c as u32;
// ASCII
if cp < 0x80 {
return 1;
}
// Half-width katakana (U+FF61-U+FF9F)
if (0xFF61..=0xFF9F).contains(&cp) {
return 1;
}
// Everything else is 2 bytes (or unencodable)
2
}
fn encode_char(c: char, buf: &mut [u8]) -> usize {
let cp = c as u32;
// ASCII
if cp < 0x80 {
buf[0] = cp as u8;
return 1;
}
// Half-width katakana (U+FF61-U+FF9F) -> 0xA1-0xDF
if (0xFF61..=0xFF9F).contains(&cp) {
buf[0] = (cp - 0xFF61 + 0xA1) as u8;
return 1;
}
// Look up in JIS X 0208 encode table
if let Some(pointer) = jis0208_encode_lookup(c) {
let (b1, b2) = bytes_from_pointer(pointer);
buf[0] = b1;
buf[1] = b2;
return 2;
}
panic!("character '{}' cannot be encoded in Shift_JIS", c);
}
fn try_encode_char(c: char, buf: &mut [u8]) -> Option<usize> {
let cp = c as u32;
// ASCII
if cp < 0x80 {
buf[0] = cp as u8;
return Some(1);
}
// Half-width katakana (U+FF61-U+FF9F) -> 0xA1-0xDF
if (0xFF61..=0xFF9F).contains(&cp) {
buf[0] = (cp - 0xFF61 + 0xA1) as u8;
return Some(1);
}
// Look up in JIS X 0208 encode table
let pointer = jis0208_encode_lookup(c)?;
let (b1, b2) = bytes_from_pointer(pointer);
buf[0] = b1;
buf[1] = b2;
Some(2)
}
fn can_encode(c: char) -> bool {
let cp = c as u32;
if cp < 0x80 {
return true;
}
if (0xFF61..=0xFF9F).contains(&cp) {
return true;
}
jis0208_encode_lookup(c).is_some()
}
fn is_char_boundary(bytes: &[u8], index: usize) -> bool {
if index == 0 || index >= bytes.len() {
return index <= bytes.len();
}
// Scan backward to find an anchor (start of buffer, ASCII, or half-width katakana)
let mut anchor = index;
while anchor > 0 {
let b = bytes[anchor - 1];
if b < 0x80 || (0xA1..=0xDF).contains(&b) {
// ASCII or half-width katakana - this is a boundary
break;
}
anchor -= 1;
}
// From anchor, parse forward to determine character boundaries
let mut pos = anchor;
while pos < index {
let b = bytes[pos];
if b < 0x80 {
// ASCII - single byte
pos += 1;
} else if (0xA1..=0xDF).contains(&b) {
// Half-width katakana - single byte
pos += 1;
} else if is_lead_byte(b) && pos + 1 < bytes.len() && is_trail_byte(bytes[pos + 1]) {
// Valid 2-byte sequence
pos += 2;
} else {
// Invalid or incomplete - treat as single byte boundary
pos += 1;
}
}
pos == index
}
fn decode_char_before(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset == 0 || offset > bytes.len() {
return None;
}
let b = bytes[offset - 1];
// ASCII
if b < 0x80 {
return Some((b as char, offset - 1));
}
// Half-width katakana (single byte)
if (0xA1..=0xDF).contains(&b) {
let c = char::from_u32(0xFF61 + (b - 0xA1) as u32)?;
return Some((c, offset - 1));
}
// Try 2-byte sequence if current byte could be a trail byte
if offset >= 2 && is_trail_byte(b) {
let b1 = bytes[offset - 2];
if is_lead_byte(b1) {
let pointer = pointer_from_bytes(b1, b);
if let Some(c) = jis0208_lookup(pointer) {
return Some((c, offset - 2));
}
}
}
None
}
}
/// Convert Shift_JIS lead and trail bytes to a JIS X 0208 pointer.
///
/// Shift_JIS to pointer formula:
/// lead_offset = lead < 0xA0 ? lead - 0x81 : lead - 0xC1
/// trail_offset = trail < 0x7F ? trail - 0x40 : trail - 0x41
/// pointer = lead_offset * 188 + trail_offset
#[inline]
fn pointer_from_bytes(lead: u8, trail: u8) -> u16 {
let lead_offset = if lead < 0xA0 {
lead - 0x81
} else {
lead - 0xC1
};
let trail_offset = if trail < 0x7F {
trail - 0x40
} else {
trail - 0x41
};
(lead_offset as u16) * 188 + trail_offset as u16
}
/// Convert a JIS X 0208 pointer to Shift_JIS lead and trail bytes.
#[inline]
fn bytes_from_pointer(pointer: u16) -> (u8, u8) {
let lead_offset = (pointer / 188) as u8;
let trail_offset = (pointer % 188) as u8;
let lead = if lead_offset < 0x1F {
lead_offset + 0x81
} else {
lead_offset + 0xC1
};
let trail = if trail_offset < 0x3F {
trail_offset + 0x40
} else {
trail_offset + 0x41
};
(lead, trail)
}
impl crate::encoding::LimitedEncoding for ShiftJis {}
#[cfg(feature = "registry")]
inventory::submit! {
crate::registry::EncodingEntry {
name: "Shift_JIS",
aliases: &["shift_jis", "shift-jis", "ShiftJIS", "shiftjis", "SJIS", "sjis", "Windows-932", "windows-932", "cp932", "CP932"],
is_unicode: false,
decode: |bytes| {
ShiftJis::validate(bytes)?;
let mut chars = Vec::new();
let mut offset = 0;
while let Some((c, next)) = ShiftJis::decode_char_at(bytes, offset) {
chars.push(c);
offset = next;
}
Ok(chars)
},
try_encode_char: |c| {
let mut buf = [0u8; 2];
ShiftJis::try_encode_char(c, &mut buf).map(|len| buf[..len].to_vec())
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ascii() {
let bytes = b"Hello";
assert!(ShiftJis::validate(bytes).is_ok());
let (c, next) = ShiftJis::decode_char_at(bytes, 0).unwrap();
assert_eq!(c, 'H');
assert_eq!(next, 1);
}
#[test]
fn test_halfwidth_katakana() {
// ア (U+FF71) - half-width katakana A
// In Shift_JIS: 0xB1
let bytes = [0xB1];
assert!(ShiftJis::validate(&bytes).is_ok());
let (c, next) = ShiftJis::decode_char_at(&bytes, 0).unwrap();
assert_eq!(c, 'ア'); // U+FF71 half-width, not U+30A2 full-width
assert_eq!(next, 1);
// Roundtrip
let mut buf = [0u8; 2];
let len = ShiftJis::encode_char('ア', &mut buf);
assert_eq!(len, 1);
assert_eq!(buf[0], 0xB1);
}
#[test]
fn test_japanese() {
// 日 (U+65E5) - "day/sun"
let mut buf = [0u8; 2];
if ShiftJis::can_encode('日') {
let len = ShiftJis::encode_char('日', &mut buf);
assert_eq!(len, 2);
let (c, _) = ShiftJis::decode_char_at(&buf, 0).unwrap();
assert_eq!(c, '日');
}
}
#[test]
fn test_roundtrip() {
let test_chars = ['A', '日', '本', '語', 'ア', 'イ'];
for &c in &test_chars {
if ShiftJis::can_encode(c) {
let mut buf = [0u8; 4];
let len = ShiftJis::encode_char(c, &mut buf);
let (decoded, _) = ShiftJis::decode_char_at(&buf[..len], 0).unwrap();
assert_eq!(c, decoded, "Roundtrip failed for '{}'", c);
}
}
}
#[test]
fn test_validate() {
// Valid ASCII
assert!(ShiftJis::validate(b"Hello").is_ok());
// Valid half-width katakana
assert!(ShiftJis::validate(&[0xB1, 0xB2, 0xB3]).is_ok());
// Invalid: lone lead byte
assert!(ShiftJis::validate(&[0x81]).is_err());
// Invalid byte 0x80
assert!(ShiftJis::validate(&[0x80]).is_err());
}
#[test]
fn test_is_char_boundary() {
// 日本 in Shift_JIS
let s: crate::String<ShiftJis> = "日本".chars().collect();
let bytes = s.as_bytes();
assert!(ShiftJis::is_char_boundary(bytes, 0));
assert!(!ShiftJis::is_char_boundary(bytes, 1));
assert!(ShiftJis::is_char_boundary(bytes, 2));
assert!(!ShiftJis::is_char_boundary(bytes, 3));
assert!(ShiftJis::is_char_boundary(bytes, 4));
}
#[test]
fn test_decode_char_before() {
let s: crate::String<ShiftJis> = "A日B".chars().collect();
let bytes = s.as_bytes();
// From end
let (c, pos) = ShiftJis::decode_char_before(bytes, bytes.len()).unwrap();
assert_eq!(c, 'B');
let (c, pos) = ShiftJis::decode_char_before(bytes, pos).unwrap();
assert_eq!(c, '日');
let (c, _) = ShiftJis::decode_char_before(bytes, pos).unwrap();
assert_eq!(c, 'A');
}
}