root/lang/perl/Acme-Encode-WhiteSpace8/trunk/lib/Acme/Encode/WhiteSpace8.pm @ 18391

Revision 18391, 2.0 kB (checked in by yappo, 5 years ago)

サイズがでかくならんようにした

Line 
1package Acme::Encode::WhiteSpace8;
2
3use strict;
4use warnings;
5use base qw(Encode::Encoding);
6use utf8;
7
8our $VERSION = '0.01';
9
10__PACKAGE__->Define('WHITESPACE-8');
11
12our $OPTIONAL_DIRECT_CHARS = 1;
13my $specials = quotemeta "\'(),-./:?";
14$OPTIONAL_DIRECT_CHARS
15    and $specials .= quotemeta "!\"#$%&*;<=>@[]^_`{|}";
16
17my %whitespaces2bit = (
18    " "    => "0000",
19    "\001" => "0001",
20    "\002" => "0010",
21    "\003" => "0011",
22    "\004" => "0100",
23    "\005" => "0101",
24    "\006" => "0110",
25    "\007" => "0111",
26
27    "\010" => "1000",
28    "\011" => "1001",
29    "\013" => "1010",
30    "\014" => "1011",
31    "\016" => "1100",
32    "\017" => "1101",
33    "\020" => "1110",
34    "\021" => "1111",
35);
36my %bit2whitespaces = map { $whitespaces2bit{$_} => $_ } keys %whitespaces2bit;
37
38sub encode($$;$) {
39    my ($obj, $str, $chk) = @_;
40
41    my $bytes = Encode::encode('utf8', $str);
42    my $bits = unpack('B*', $bytes);
43    $bits =~ s/([01][01][01][01])/$bit2whitespaces{$1}/g;
44    $bits;
45}
46
47sub decode($$;$) {
48    my ($obj, $bytes, $chk) = @_;
49    return '' unless $bytes;
50    return $bytes unless $bytes =~ /^[ \001\002\003\004\005\006\007\010\011\013\014\016\017\020\021]{2,}$/;
51
52    my @bits = map {
53        $whitespaces2bit{$_}
54    } split //, $bytes;
55
56    my $ret = '';
57    while (my @byte = splice @bits, 0, 2) {
58        $ret .= pack('B8', join '', @byte);
59    }
60    $ret;
61}
62
63
641;
65__END__
66
67=encoding utf8
68
69=head1 NAME
70
71Acme::Encode::WhiteSpace8 -
72
73=head1 SYNOPSIS
74
75  use Acme::Encode::WhiteSpace8;
76
77=head1 DESCRIPTION
78
79Acme::Encode::WhiteSpace8 is
80
81=head1 AUTHOR
82
83Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
84
85=head1 SEE ALSO
86
87=head1 REPOSITORY
88
89  svn co http://svn.coderepos.org/share/lang/perl/Acme-Encode-WhiteSpace8/trunk Acme-Encode-WhiteSpace8
90
91Acme::Encode::WhiteSpace8 is Subversion repository is hosted at L<http://coderepos.org/share/>.
92patches and collaborators are welcome.
93
94=head1 LICENSE
95
96This library is free software; you can redistribute it and/or modify
97it under the same terms as Perl itself.
98
99=cut
Note: See TracBrowser for help on using the browser.