| 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 | " " => "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 | ); |
|---|
| 36 | my %bit2whitespaces = map { $whitespaces2bit{$_} => $_ } keys %whitespaces2bit; |
|---|
| 37 | |
|---|
| 38 | sub encode($$;$) { ## no critic |
|---|
| 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 | |
|---|
| 47 | sub decode($$;$) { ## no critic |
|---|
| 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 | |
|---|
| 64 | 1; |
|---|
| 65 | __END__ |
|---|
| 66 | |
|---|
| 67 | =encoding utf8 |
|---|
| 68 | |
|---|
| 69 | =head1 NAME |
|---|
| 70 | |
|---|
| 71 | Acme::Encode::WhiteSpace8 - |
|---|
| 72 | |
|---|
| 73 | =head1 SYNOPSIS |
|---|
| 74 | |
|---|
| 75 | use Acme::Encode::WhiteSpace8; |
|---|
| 76 | |
|---|
| 77 | =head1 DESCRIPTION |
|---|
| 78 | |
|---|
| 79 | Acme::Encode::WhiteSpace8 is |
|---|
| 80 | |
|---|
| 81 | =head1 AUTHOR |
|---|
| 82 | |
|---|
| 83 | Kazuhiro 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 | |
|---|
| 91 | Acme::Encode::WhiteSpace8 is Subversion repository is hosted at L<http://coderepos.org/share/>. |
|---|
| 92 | patches and collaborators are welcome. |
|---|
| 93 | |
|---|
| 94 | =head1 LICENSE |
|---|
| 95 | |
|---|
| 96 | This library is free software; you can redistribute it and/or modify |
|---|
| 97 | it under the same terms as Perl itself. |
|---|
| 98 | |
|---|
| 99 | =cut |
|---|