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