Changeset 1434
- Timestamp:
- 11/14/07 09:23:51 (6 years ago)
- Location:
- lang/perl/Encode-JP-Mobile/trunk
- Files:
-
- 2 modified
-
lib/Encode/JP/Mobile/KDDIJIS.pm (modified) (3 diffs)
-
t/kddi-jis.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Encode-JP-Mobile/trunk/lib/Encode/JP/Mobile/KDDIJIS.pm
r1392 r1434 13 13 __PACKAGE__->Define(qw(x-iso-2022-jp-kddi)); 14 14 15 # JIS<->EUC16 15 my $re_scan_jis = qr{ 17 16 (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*) … … 32 31 33 32 sub encode($$;$) { 34 my($self, $str, $check) = @_; 35 croak "ENCODE IS NOT SUPPORTED YET"; 33 my ( $obj, $utf8, $chk ) = @_; 34 my $octet = Encode::encode( 'x-sjis-kddi', $utf8, $chk ); 35 return sjis_jis( $octet ); 36 } 37 38 sub ASC () { 1 } 39 sub JIS_0208 () { 2 } 40 sub KANA () { 3 } 41 sub sjis_jis { 42 my $octet = shift; 43 44 use bytes; 45 46 my @chars = split //, $octet; 47 my $mode = ASC; 48 my $res = ''; 49 50 for (my $i=0; $i<@chars; $i++) { 51 my $x = ord $chars[$i]; 52 if ($x < 0x80) { 53 if ($mode != ASC) { 54 $res .= $ESC{ASC}; 55 $mode = ASC; 56 } 57 $res .= chr $x; 58 } elsif (0xA1 <= $x && $x <= 0xDF) { 59 if ($mode != KANA) { 60 $res .= $ESC{KANA}; 61 $mode = KANA; 62 } 63 $mode = KANA; 64 $res .= chr($x - 0x80); 65 } else { 66 if ($mode != JIS_0208) { 67 $res .= $ESC{JIS_0208}; 68 $mode = JIS_0208; 69 } 70 $i++; 71 last unless $i<@chars; 72 my ($c1, $c2) = sjis2jis_one($x, ord $chars[$i]); 73 $res .= chr($c1).chr($c2); 74 } 75 } 76 77 if ($mode != ASC) { 78 $res .= $ESC{ASC}; 79 } 80 81 $res; 82 } 83 sub sjis2jis_one { 84 my ($c1, $c2) = @_; 85 86 # 0x0600 : 0xF340 - 0xF48D 87 # 0x0B00 : 0xF640 - 0xF7FC 88 89 my $c = ($c1<<8) + $c2; 90 if (0xF340 <= $c && $c <= 0xF48D) { 91 $c1 -= 0x06; 92 } elsif (0xF640 <= $c && $c <= 0xF7FC) { 93 $c1 -= 0x0B; 94 } 95 96 $c1 -= ($c1 <= 0x9f) ? 0x71 : 0xB1; 97 $c1 = $c1*2 + 1; 98 99 if ($c2 > 0x7F) { 100 $c2 -= 0x01; 101 } 102 103 if ($c2>=0x9E) { 104 $c2 = $c2-0x7D; 105 $c1++; 106 } else { 107 $c2 -= 0x1F; 108 } 109 110 return ($c1, $c2); 36 111 } 37 112 … … 128 203 この後で、x-sjis-kddi で decode すれば OK. 129 204 205 encode の場合はこの逆をやればよい。unicode 文字列を sjis のバイト列に encode してやり、 206 下記のエリアにある文字列をシフトしてやる。 207 208 * 0x0600 : 0xF340 - 0xF48D 209 * 0x0B00 : 0xF640 - 0xF7FC 210 211 こうしてシフトしつつ、iso-2022-jp に変換してやればよい。 212 130 213 =head1 TODO 131 214 -
lang/perl/Encode-JP-Mobile/trunk/t/kddi-jis.t
r1392 r1434 1 1 use strict; 2 2 use warnings; 3 use Test::More tests => 5;3 use Test::More tests => 17; 4 4 use Encode::JP::Mobile; 5 5 use Encode::JP::Mobile::KDDIJIS; 6 6 use Encode; 7 use Data::Dumper;8 7 9 is decode("x-iso-2022-jp-kddi", "\e\$B\x75\x41\e(B"), "\x{E488}"; 10 is decode("x-iso-2022-jp-kddi", "\e\$B\x75\x41\x76\x76\e(B"), "\x{e488}\x{e51b}"; 11 is decode('x-iso-2022-jp-kddi', encode('iso-2022-jp', decode("utf8", "お"))), decode('utf8', "お"), 'o'; 12 is decode('x-iso-2022-jp-kddi', encode('iso-2022-jp', decode("utf8", "おいおい。山岡くん。kanbenしてくれよ!"))), decode('utf8', "おいおい。山岡くん。kanbenしてくれよ!"), 'kanji, hiragana, alphabet'; 13 is decode('x-iso-2022-jp-kddi', "\e\(I\x54\x2F\x4E\x5F\e(B"), decode('utf8', "ヤッポ"), 'half width katakana'; 8 sub test_it { 9 my ($jis, $uni, $case) = @_; 10 $case ||= unpack "H*", $uni; 11 12 is decode("x-iso-2022-jp-kddi", $jis), $uni, "decoding $case"; 13 is $jis, encode("x-iso-2022-jp-kddi", $uni), "encoding $case"; 14 } 15 16 test_it("\e\$B\x75\x41\e(B", "\x{E488}", "pictogram"); 17 18 test_it "a", decode('utf8', 'a'), 'alphabet'; 19 test_it "\e\$B\x24\x57\e\(B", "\x{3077}", 'kanji(tora)'; 20 21 is encode('x-iso-2022-jp-kddi', "\x{5bc5}"), encode('iso-2022-jp', "\x{5bc5}"), "kanji"; 22 23 test_it "\e\$B\x75\x41\e(B", "\x{E488}", 'pictogram'; 24 test_it "\e\$B\x75\x41\x76\x76\e(B", "\x{e488}\x{e51b}", 'pictogram'; 25 test_it encode('iso-2022-jp', decode("utf8", "お")), decode('utf8', "お"), 'o'; 26 test_it encode('iso-2022-jp', decode("utf8", "おいおい。山岡くん。kanbenしてくれよ!表示。")), decode('utf8', "おいおい。山岡くん。kanbenしてくれよ!表示。"), 'kanji, hiragana, alphabet'; 27 test_it "\e\(I\x54\x2F\x4E\x5F\e(B", decode('utf8', "ヤッポ"), 'half width katakana'; 28 14 29 # is decode('x-iso-2022-jp-kddi', "\e\$(D\x2B\x21\x30\x57\e(B"), "\x{00E1}\x{4F0C}", 'JIS X 0212'; 15
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)