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

Revision 18389, 1.6 kB (checked in by yappo, 5 years ago)

TAB CR LF SPACE を使った

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    "\t" => "00",
19    "\r" => "01",
20    "\n" => "10",
21    " "  => "11",
22);
23my %bit2whitespaces = map { $whitespaces2bit{$_} => $_ } keys %whitespaces2bit;
24
25sub encode($$;$) {
26    my ($obj, $str, $chk) = @_;
27
28    my $bytes = Encode::encode('utf8', $str);
29    my $bits = unpack('B*', $bytes);
30    $bits =~ s/([01][01])/$bit2whitespaces{$1}/g;
31    $bits;
32}
33
34sub decode($$;$) {
35    my ($obj, $bytes, $chk) = @_;
36    return '' unless $bytes;
37    return $bytes unless $bytes =~ /^[\t\r\n ]{4,}$/;
38
39    my @bits = map {
40        $whitespaces2bit{$_}
41    } split //, $bytes;
42
43    my $ret = '';
44    while (my @byte = splice @bits, 0, 4) {
45        $ret .= pack('B8', join '', @byte);
46    }
47
48    $ret;
49}
50
51
521;
53__END__
54
55=encoding utf8
56
57=head1 NAME
58
59Acme::Encode::WhiteSpace8 -
60
61=head1 SYNOPSIS
62
63  use Acme::Encode::WhiteSpace8;
64
65=head1 DESCRIPTION
66
67Acme::Encode::WhiteSpace8 is
68
69=head1 AUTHOR
70
71Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
72
73=head1 SEE ALSO
74
75=head1 REPOSITORY
76
77  svn co http://svn.coderepos.org/share/lang/perl/Acme-Encode-WhiteSpace8/trunk Acme-Encode-WhiteSpace8
78
79Acme::Encode::WhiteSpace8 is Subversion repository is hosted at L<http://coderepos.org/share/>.
80patches and collaborators are welcome.
81
82=head1 LICENSE
83
84This library is free software; you can redistribute it and/or modify
85it under the same terms as Perl itself.
86
87=cut
Note: See TracBrowser for help on using the browser.