root/lang/perl/Encode-JP-Mobile/trunk/lib/Encode/JP/Mobile/Vodafone.pm @ 5259

Revision 5259, 1.7 kB (checked in by tokuhirom, 5 years ago)

FULLWIDTH TILDE をよしなにはからう。
x-sjis-*, x-utf8-* で U+301C を encode 可能にした。

  • Property svn:keywords set to Id Revision
Line 
1package Encode::JP::Mobile::Vodafone;
2use strict;
3use base qw(Encode::Encoding);
4__PACKAGE__->Define(qw(x-sjis-vodafone-raw));
5
6use Encode::Alias;
7define_alias('x-sjis-softbank-raw' => 'x-sjis-vodafone-raw');
8
9# G! => E001, G" => E002, G# => E003 ...
10# E! => E101, F! => E201, O! => E301, P! => E401, Q! => E501
11my %HighCharToBit = (G => 0xE000, E => 0xE100, F => 0xE200,
12                     O => 0xE300, P => 0xE400, Q => 0xE500);
13my %HighBitToChar = reverse %HighCharToBit;
14
15my $range = '\x{E001}-\x{E05A}\x{E101}-\x{E15A}\x{E201}-\x{E25A}\x{E301}-\x{E34D}\x{E401}-\x{E44C}\x{E501}-\x{E539}';
16my $InRange  = "[$range]";
17my $OutRange = "[^$range]";
18
19sub decode($$;$) {
20    my($self, $char, $check) = @_;
21    my $str = Encode::decode("cp932", $char, Encode::FB_PERLQQ);
22    $str =~ s{\x1b\x24([GEFOPQ])([\x20-\x7F]+)\x0f}{
23        join '', map chr($HighCharToBit{$1} | ord($_) - 32), split //, $2;
24    }ge;
25    $_[1] = $str if $check;
26    $str;
27}
28
29sub encode($$;$) {
30    my($self, $str, $check) = @_;
31    my $res = '';
32    $str =~ tr/\x{301C}/\x{FF5E}/; # ad-hoc solution for  FULLWIDTH TILDE Problem
33    $str =~ s{($InRange+)|($OutRange+)}{
34        my $in = defined $1;
35        my $m  = $in ? $1 : $2;
36        $res .= $in ? _encode_vodafone($m)
37            : Encode::encode("cp932", $m, $check);
38        ''
39    }egs;
40    $_[1] = $res if $check;
41    $res;
42}
43
44sub _encode_vodafone {
45    my $str = shift;
46    my @str = split //, $str;
47    my $res = "\x1b\x24";
48    my $buf = '';
49    for my $str (@str) {
50        my $high = ord($str) & 0xEF00;
51        my $low  = ord($str) & 0x00FF;
52        if ($buf ne $high) {
53            $res .= $HighBitToChar{$high};
54        }
55        $res .= chr($low+32);
56        $buf = $high;
57    }
58    $res . "\x0f";
59}
60
611;
Note: See TracBrowser for help on using the browser.