Skip to content

Commit

Permalink
[ruby/prism] Add MacJapanese encoding
Browse files Browse the repository at this point in the history
MacJapanese (also aliased as MacJapan) is a modified Shift_JIS
encoding, but is implemented identically in Ruby

ruby/prism@9e0a097699
  • Loading branch information
mattboldt authored and kddnewton committed Nov 29, 2023
1 parent 86d9a6d commit 9fc40d2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/prism/prism.gemspec
Expand Up @@ -91,6 +91,7 @@ Gem::Specification.new do |spec|
"src/enc/pm_cp950.c",
"src/enc/pm_euc_jp.c",
"src/enc/pm_gbk.c",
"src/enc/pm_mac_japanese.c",
"src/enc/pm_shift_jis.c",
"src/enc/pm_tables.c",
"src/enc/pm_unicode.c",
Expand Down
1 change: 1 addition & 0 deletions prism/enc/pm_encoding.h
Expand Up @@ -206,6 +206,7 @@ extern pm_encoding_t pm_encoding_mac_croatian;
extern pm_encoding_t pm_encoding_mac_cyrillic;
extern pm_encoding_t pm_encoding_mac_greek;
extern pm_encoding_t pm_encoding_mac_iceland;
extern pm_encoding_t pm_encoding_mac_japanese;
extern pm_encoding_t pm_encoding_mac_roman;
extern pm_encoding_t pm_encoding_mac_romania;
extern pm_encoding_t pm_encoding_mac_thai;
Expand Down
57 changes: 57 additions & 0 deletions prism/enc/pm_mac_japanese.c
@@ -0,0 +1,57 @@
#include "prism/enc/pm_encoding.h"

static size_t
pm_encoding_mac_japanese_char_width(const uint8_t *b, ptrdiff_t n) {
// These are the single byte characters.
if (*b < 0x80 || (*b >= 0xA1 && *b <= 0xDF)) {
return 1;
}

// These are the double byte characters.
if (
(n > 1) &&
((b[0] >= 0x81 && b[0] <= 0x9F) || (b[0] >= 0xE0 && b[0] <= 0xFC)) &&
(b[1] >= 0x40 && b[1] <= 0xFC)
) {
return 2;
}

return 0;
}

static size_t
pm_encoding_mac_japanese_alpha_char(const uint8_t *b, ptrdiff_t n) {
if (pm_encoding_mac_japanese_char_width(b, n) == 1) {
return pm_encoding_ascii_alpha_char(b, n);
} else {
return 0;
}
}

static size_t
pm_encoding_mac_japanese_alnum_char(const uint8_t *b, ptrdiff_t n) {
if (pm_encoding_mac_japanese_char_width(b, n) == 1) {
return pm_encoding_ascii_alnum_char(b, n);
} else {
return 0;
}
}

static bool
pm_encoding_mac_japanese_isupper_char(const uint8_t *b, ptrdiff_t n) {
if (pm_encoding_mac_japanese_char_width(b, n) == 1) {
return pm_encoding_ascii_isupper_char(b, n);
} else {
return 0;
}
}

/** MacJapanese encoding */
pm_encoding_t pm_encoding_mac_japanese = {
.name = "MacJapanese",
.char_width = pm_encoding_mac_japanese_char_width,
.alnum_char = pm_encoding_mac_japanese_alnum_char,
.alpha_char = pm_encoding_mac_japanese_alpha_char,
.isupper_char = pm_encoding_mac_japanese_isupper_char,
.multibyte = true
};
2 changes: 2 additions & 0 deletions prism/prism.c
Expand Up @@ -6303,6 +6303,8 @@ parser_lex_magic_comment_encoding_value(pm_parser_t *parser, const uint8_t *star
ENCODING1("macCyrillic", pm_encoding_mac_cyrillic);
ENCODING1("macGreek", pm_encoding_mac_greek);
ENCODING1("macIceland", pm_encoding_mac_iceland);
ENCODING1("MacJapanese", pm_encoding_mac_japanese);
ENCODING1("MacJapan", pm_encoding_mac_japanese);
ENCODING1("macRoman", pm_encoding_mac_roman);
ENCODING1("macRomania", pm_encoding_mac_romania);
ENCODING1("macThai", pm_encoding_mac_thai);
Expand Down
1 change: 1 addition & 0 deletions test/prism/encoding_test.rb
Expand Up @@ -72,6 +72,7 @@ class EncodingTest < TestCase
Encoding::CP950 => 0x00...0x10000,
Encoding::CP51932 => 0x00...0x10000,
Encoding::GBK => 0x00...0x10000,
Encoding::MACJAPANESE => 0x00...0x10000,
Encoding::Shift_JIS => 0x00...0x10000,
Encoding::Windows_31J => 0x00...0x10000
}
Expand Down

0 comments on commit 9fc40d2

Please sign in to comment.