|
| 1 | +#include "prism/enc/pm_encoding.h" |
| 2 | + |
| 3 | +static size_t |
| 4 | +pm_encoding_cp51932_char_width(const uint8_t *b, ptrdiff_t n) { |
| 5 | + // These are the single byte characters. |
| 6 | + if (*b < 0x80) { |
| 7 | + return 1; |
| 8 | + } |
| 9 | + |
| 10 | + // These are the double byte characters. |
| 11 | + if ( |
| 12 | + (n > 1) && |
| 13 | + ((b[0] >= 0xa1 && b[0] <= 0xfe) || (b[0] == 0x8e)) && |
| 14 | + (b[1] >= 0xa1 && b[1] <= 0xfe) |
| 15 | + ) { |
| 16 | + return 2; |
| 17 | + } |
| 18 | + |
| 19 | + return 0; |
| 20 | +} |
| 21 | + |
| 22 | +static size_t |
| 23 | +pm_encoding_cp51932_alpha_char(const uint8_t *b, ptrdiff_t n) { |
| 24 | + if (pm_encoding_cp51932_char_width(b, n) == 1) { |
| 25 | + return pm_encoding_ascii_alpha_char(b, n); |
| 26 | + } else { |
| 27 | + return 0; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +static size_t |
| 32 | +pm_encoding_cp51932_alnum_char(const uint8_t *b, ptrdiff_t n) { |
| 33 | + if (pm_encoding_cp51932_char_width(b, n) == 1) { |
| 34 | + return pm_encoding_ascii_alnum_char(b, n); |
| 35 | + } else { |
| 36 | + return 0; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +static bool |
| 41 | +pm_encoding_cp51932_isupper_char(const uint8_t *b, ptrdiff_t n) { |
| 42 | + if (pm_encoding_cp51932_char_width(b, n) == 1) { |
| 43 | + return pm_encoding_ascii_isupper_char(b, n); |
| 44 | + } else { |
| 45 | + return 0; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** cp51932 encoding */ |
| 50 | +pm_encoding_t pm_encoding_cp51932 = { |
| 51 | + .name = "cp51932", |
| 52 | + .char_width = pm_encoding_cp51932_char_width, |
| 53 | + .alnum_char = pm_encoding_cp51932_alnum_char, |
| 54 | + .alpha_char = pm_encoding_cp51932_alpha_char, |
| 55 | + .isupper_char = pm_encoding_cp51932_isupper_char, |
| 56 | + .multibyte = true |
| 57 | +}; |
0 commit comments