File Coverage

blib/lib/Lingua/JA/Regular.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Lingua::JA::Regular;
2              
3 8     8   132853 use strict;
  8         16  
  8         299  
4 8     8   41 use vars qw($VERSION);
  8         11  
  8         464  
5             $VERSION = '0.09';
6              
7 8     8   186 use 5.005;
  8         27  
  8         376  
8 8     8   11677 use Jcode;
  0            
  0            
9              
10             use Lingua::JA::Regular::Table;
11              
12             use vars qw(
13             $HANKAKU_ASCII
14             $ZENKAKU_ASCII
15             $KATAKANA
16             $HIRAGANA
17             $CHARACTER_STRICT_REGEX
18             $CHARACTER_UNDEF_REGEX
19              
20             %KANJI_ALT_TABLE
21             %WIN_ALT_TABLE
22             %MAC_ALT_TABLE
23             );
24              
25             use overload '""' => \&to_s;
26              
27             sub new {
28             my $class = shift;
29             my $str = shift;
30             my $icode = shift || getcode($str);
31              
32             if (defined $icode and $icode =~ /^(:?jis|sjis|utf8)$/) {
33             return bless {
34             str => Jcode->new($str, $icode)->euc,
35             icode => $icode
36             }, $class;
37             }
38             else {
39             return bless {str => $str}, $class;
40             }
41             }
42              
43             sub to_s {
44             my $self = shift;
45              
46             if (defined $self->{icode}) {
47             my $icode = $self->{icode};
48             $self->{str} = Jcode->new($self->{str}, 'euc')->$icode();
49             }
50              
51             return $self->{str};
52             }
53              
54             sub linefeed {
55             my $self = shift;
56             my $lf = shift;
57              
58             $lf = "\n" unless(defined $lf);
59              
60             $self->{str} =~ s/\r\n|\r|\n/$lf/g;
61              
62             return $self;
63             }
64              
65             sub strip {
66             my $self = shift;
67              
68             $self->{str} =~ s/^\s+//;
69             $self->{str} =~ s/\s+$//;
70              
71             return $self;
72             }
73              
74             sub uc {
75             my $self = shift;
76             $self->{str} = CORE::uc $self->{str};
77             return $self;
78             }
79              
80             sub lc {
81             my $self = shift;
82             $self->{str} = CORE::lc $self->{str};
83             return $self;
84              
85             }
86              
87             sub z_ascii {
88             my $self = shift;
89             my $str = Jcode->new($self->{str}, 'euc');
90              
91             $str->tr('-', "\xA1\xDD");
92             $str->tr($HANKAKU_ASCII, $ZENKAKU_ASCII);
93              
94             $self->{str} = $str->euc;
95             return $self;
96             }
97              
98             sub h_ascii {
99             my $self = shift;
100             my $str = Jcode->new($self->{str}, 'euc');
101              
102             $str->tr("\xA1\xDD", '-');
103             $str->tr($ZENKAKU_ASCII, $HANKAKU_ASCII);
104              
105             $self->{str} = $str->euc;
106             return $self;
107             }
108              
109             sub z_kana {
110             my $self = shift;
111             my $str = Jcode->new($self->{str}, 'euc');
112              
113             $str->h2z;
114              
115             $self->{str} = $str->euc;
116             return $self;
117             }
118              
119             sub h_kana {
120             my $self = shift;
121             my $str = Jcode->new($self->{str}, 'euc');
122              
123             $str->z2h;
124              
125             $self->{str} = $str->euc;
126             return $self;
127             }
128              
129             sub z_space {
130             my $self = shift;
131             my $str = Jcode->new($self->{str}, 'euc');
132              
133             $str->tr(' ', "\xA1\xA1");
134              
135             $self->{str} = $str->euc;
136             return $self;
137             }
138              
139             sub h_space {
140             my $self = shift;
141              
142             my $str = Jcode->new($self->{str}, 'euc');
143             $str->tr("\xA1\xA1", ' ');
144              
145             $self->{str} = $str->euc;
146             return $self;
147             }
148              
149             sub z_strip {
150             my $self = shift;
151              
152             $self->{str} =~ s/^(?:\xA1\xA1|\s)+//;
153             $self->{str} =~ s/(?:\xA1\xA1|\s)+$//;
154              
155             return $self;
156             }
157              
158             sub hiragana {
159             my $self = shift;
160             my $str = Jcode->new($self->{str}, 'euc');
161              
162             $str->tr($KATAKANA, $HIRAGANA);
163              
164             $self->{str} = $str->euc;
165             return $self;
166             }
167              
168             sub katakana {
169             my $self = shift;
170              
171             my $str = Jcode->new($self->{str}, 'euc');
172             $str->tr($HIRAGANA, $KATAKANA);
173              
174             $self->{str} = $str->euc;
175             return $self;
176             }
177              
178             sub kanji {
179             my $self = shift;
180              
181             require Lingua::JA::Regular::Table::Kanji;
182             import Lingua::JA::Regular::Table::Kanji;
183              
184             $self->{str} =~ s{($CHARACTER_UNDEF_REGEX)}{
185             defined($KANJI_ALT_TABLE{$1})? $KANJI_ALT_TABLE{$1} : $1
186             }ogex;
187              
188             return $self;
189             }
190              
191             sub win {
192             my $self = shift;
193              
194             require Lingua::JA::Regular::Table::Windows;
195             import Lingua::JA::Regular::Table::Windows;
196              
197             $self->{str} =~ s{($CHARACTER_UNDEF_REGEX)}{
198             defined($WIN_ALT_TABLE{$1})? $WIN_ALT_TABLE{$1} : $1
199             }ogex;
200              
201             return $self;
202             }
203              
204             sub mac {
205             my $self = shift;
206              
207             require Lingua::JA::Regular::Table::Macintosh;
208             import Lingua::JA::Regular::Table::Macintosh;
209              
210             $self->{str} =~ s{($CHARACTER_UNDEF_REGEX)}{
211             defined($MAC_ALT_TABLE{$1})? $MAC_ALT_TABLE{$1} : $1
212             }ogex;
213              
214             return $self;
215             }
216              
217             sub geta {
218             my $self = shift;
219              
220             #
221             # EUC-JP undef character to GETA
222             #
223             $self->{str} =~ s/$CHARACTER_UNDEF_REGEX/\xA2\xAE/go;
224              
225             #
226             # measures of binary code
227             # - delete EUC-JP character
228             # - binary to GETA
229             #
230             my $tmp = $self->{str};
231             $tmp =~ s/$CHARACTER_STRICT_REGEX/ /go;
232             $tmp =~ s/^\s+//;
233             $tmp =~ s/\s+$//;
234              
235             for my $undef (split /\s+/, $tmp) {
236             $self->{str} =~
237             s/
238             (?
239             $undef
240             (?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))
241             /\xA2\xAE/x;
242             }
243              
244             return $self;
245             }
246              
247              
248             sub regular {
249             my $self = shift;
250              
251              
252             if (defined $ENV{HTTP_USER_AGENT}) {
253             if ($ENV{HTTP_USER_AGENT} =~ /Windows/) {
254             $self->win;
255             }
256             elsif ($ENV{HTTP_USER_AGENT} =~ /Mac/) {
257             $self->mac;
258             }
259             }
260              
261             $self->strip->linefeed->z_kana->h_ascii->kanji;
262              
263             return $self->geta->to_s;
264             }
265              
266             1;
267              
268             __END__