1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#-*- perl -*-
#
# Copyright (C) 2001,2002 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Utils.pm,v 1.6 2002/02/01 12:03:56 fukachan Exp $
#
package FML::Language::Japanese::Utils;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
my $debug = 0;
=head1 NAME
FML::Language::Japanese::Utils - what is this
=head1 SYNOPSIS
use FML::Language::Japanese::Utils qw(is_iso2022jp_string);
if ( is_iso2022jp_string($string) ) { do_something_if_Japanese;}
=head1 DESCRIPTION
Utilities for Japanse string
=head1 METHODS
=head2 <is_iso2022jp_string($string)>
check whether $string looks Japanese.
return 1 if it looks so.
=cut
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(is_iso2022jp_string);
# Descriptions: $buf looks like Japanese or not ?
# Arguments: STR($buf)
# Side Effects: none
# Return Value: 1 or 0
sub is_iso2022jp_string
{
my ($buf) = @_;
return (not _look_not_iso2022jp_string($buf));
}
# Descriptions: $buf looks like Japanese or not ?
# based on fml-support: 07020, 07029
# Koji Sudo <koji@cherry.or.jp>
# Takahiro Kambe <taca@sky.yamashina.kyoto.jp>
# check the given buffer has unusual Japanese (not ISO-2022-JP)
# Arguments: OBJ($buf)
# Side Effects: none
# Return Value: 1 or 0
sub _look_not_iso2022jp_string
{
my ($buf) = @_;
# check 8 bit on
if ($buf =~ /[\x80-\xFF]/){
return 1;
}
# check SI/SO
if ($buf =~ /[\016\017]/) {
return 1;
}
# HANKAKU KANA
if ($buf =~ /\033\(I/) {
return 1;
}
# MSB flag or other control sequences
if ($buf =~ /[\001-\007\013\015\020-\032\034-\037\177-\377]/) {
return 1;
}
0; # O.K.
}
=head2 C<compare_euc_string($buf, $pat)>
search $pat in EUC string $buf.
return 1 if found or 0 if not.
=cut
# Descriptions: compare Japanese EUC strings
# fml 4.0: EUCCompare($buf, $pat)
# where $pat should be $& (matched pattern)
# Arguments: OBJ($self) STR($a) STR($pat)
# Side Effects: none
# Return Value: 1 or 0
sub compare_euc_string
{
my ($self, $a, $pat) = @_;
# (Refeence: jcode 2.12)
# $re_euc_c = '[\241-\376][\241-\376]';
# $re_euc_kana = '\216[\241-\337]';
# $re_euc_0212 = '\217[\241-\376][\241-\376]';
my ($re_euc_c, $re_euc_kana, $re_euc_0212);
$re_euc_c = '[\241-\376][\241-\376]';
$re_euc_kana = '\216[\241-\337]';
$re_euc_0212 = '\217[\241-\376][\241-\376]';
# always true if given buffer is not EUC.
if ($a !~ /($re_euc_c|$re_euc_kana|$re_euc_0212)/) {
&Log("EUCCompare: do nothing for non EUC strings");# if $debug;
return 1;
}
# extract EUC code (e.g. .*EUC_PATTERN.*)
# but how to do for "EUC ASCII EUC" case ???
my ($pa, $loc, $i);
do {
if ($a =~ /(($re_euc_c|$re_euc_kana|$re_euc_0212)+)/) {
$pa = $1;
$loc = index($pa, $pat);
}
print STDERR "buf = <$a> pa=<$pa> pat=<$pat> loc=$loc\n" if $debug;
return 1 if ($loc % 2) == 0;
$a = substr($a, index($a, $pa) + length($pa) );
} while ($i++ < 16);
0;
}
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001,2002 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Language::Japanese::Utils appeared in fml5 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|