| 1 | package Acme::Encode::WhiteSpace8; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base qw(Encode::Encoding); |
|---|
| 6 | use utf8; |
|---|
| 7 | |
|---|
| 8 | our $VERSION = '0.01'; |
|---|
| 9 | |
|---|
| 10 | __PACKAGE__->Define('WHITESPACE-8'); |
|---|
| 11 | |
|---|
| 12 | our $OPTIONAL_DIRECT_CHARS = 1; |
|---|
| 13 | my $specials = quotemeta "\'(),-./:?"; |
|---|
| 14 | $OPTIONAL_DIRECT_CHARS |
|---|
| 15 | and $specials .= quotemeta "!\"#$%&*;<=>@[]^_`{|}"; |
|---|
| 16 | |
|---|
| 17 | my %whitespaces2bit = ( |
|---|
| 18 | "\t" => "00", |
|---|
| 19 | "\r" => "01", |
|---|
| 20 | "\n" => "10", |
|---|
| 21 | " " => "11", |
|---|
| 22 | ); |
|---|
| 23 | my %bit2whitespaces = map { $whitespaces2bit{$_} => $_ } keys %whitespaces2bit; |
|---|
| 24 | |
|---|
| 25 | sub 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 | |
|---|
| 34 | sub 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 | |
|---|
| 52 | 1; |
|---|
| 53 | __END__ |
|---|
| 54 | |
|---|
| 55 | =encoding utf8 |
|---|
| 56 | |
|---|
| 57 | =head1 NAME |
|---|
| 58 | |
|---|
| 59 | Acme::Encode::WhiteSpace8 - |
|---|
| 60 | |
|---|
| 61 | =head1 SYNOPSIS |
|---|
| 62 | |
|---|
| 63 | use Acme::Encode::WhiteSpace8; |
|---|
| 64 | |
|---|
| 65 | =head1 DESCRIPTION |
|---|
| 66 | |
|---|
| 67 | Acme::Encode::WhiteSpace8 is |
|---|
| 68 | |
|---|
| 69 | =head1 AUTHOR |
|---|
| 70 | |
|---|
| 71 | Kazuhiro 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 | |
|---|
| 79 | Acme::Encode::WhiteSpace8 is Subversion repository is hosted at L<http://coderepos.org/share/>. |
|---|
| 80 | patches and collaborators are welcome. |
|---|
| 81 | |
|---|
| 82 | =head1 LICENSE |
|---|
| 83 | |
|---|
| 84 | This library is free software; you can redistribute it and/or modify |
|---|
| 85 | it under the same terms as Perl itself. |
|---|
| 86 | |
|---|
| 87 | =cut |
|---|