| 1 | #!/usr/bin/perl | ||||||||||||||
| 2 | #------------------------------------------------------------------------------- | ||||||||||||||
| 3 | # Encode a Unicode string in Perl and decode it in Java | ||||||||||||||
| 4 | # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2017 | ||||||||||||||
| 5 | #------------------------------------------------------------------------------- | ||||||||||||||
| 6 | |||||||||||||||
| 7 | package Encode::Unicode::PerlDecodeJava; | ||||||||||||||
| 8 | 1 | require v5.16.0; | |||||||||||||
| 9 | 1 | ▶ | use warnings FATAL => qw(all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | our $VERSION = '20171127'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | sub encode93($) # Return the encoded version of a string | ||||||||||||||
| 17 | 22 | ▶ | {my ($i) = @_; # String to encode | ||||||||||||
|
Subroutine
encode93: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 18 | 22 | my $s = ''; | |||||||||||||
| 19 | 22 | my $n = length($i); | |||||||||||||
| 20 | 22 | for(split //, $i) # Letters are passed straight through | |||||||||||||
| 21 | 90 | ▶ | {$s .= /[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '\(\)\[\]\{\}<>`!@#\$%^&*_\-+=,;:|.?\/]/ ? $_ : ord($_).'~'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | } | ||||||||||||||
| 23 | 22 | $s =~ s/([0123456789])(~)([^0123456789]|\Z)/$1$3/gsr; # Remove redundant ~ | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | sub decode93($) # Return the decode version of an encoded string | ||||||||||||||
| 27 | 11 | ▶ | {my ($i) = @_; # String to decode | ||||||||||||
|
Subroutine
decode93: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 28 | 11 | my $s = ''; | |||||||||||||
| 29 | 11 | my $n = ''; | |||||||||||||
| 30 | 11 | for(split //, $i) # Letters are passed straight through | |||||||||||||
| 31 | 124 | ▶ | {if ( /[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '\(\)\[\]\{\}<>`!@#\$%^&*_\-+=,;:|.?\/]/) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 32 | 22 | ▶ | {if (length($n)) {$s .= pack('U', $n); $n = ''} # Number terminated by letter not ~ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 22 | $s .= $_ | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | 15 | elsif (/~/i) {$s .= pack('U', $n); $n = ''} # Decompress number | |||||||||||||
| 36 | 87 | else {$n .= $_} | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | 11 | ▶ | if (length($n)) {$s .= pack('U', $n)} # Trailing number | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | $s | ||||||||||||||
| 40 | 11 | } | |||||||||||||
| 41 | |||||||||||||||
| 42 | #------------------------------------------------------------------------------- | ||||||||||||||
| 43 | # Export | ||||||||||||||
| 44 | #------------------------------------------------------------------------------- | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 1 | require Exporter; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 1 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 1 | @ISA = qw(Exporter); | |||||||||||||
| 51 | 1 | @EXPORT = qw(decode93 encode93); | |||||||||||||
| 52 | 1 | @EXPORT_OK = qw(); | |||||||||||||
| 53 | 1 | %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); | |||||||||||||
| 54 | |||||||||||||||
| 55 | # podDocumentation | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =pod | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =encoding utf-8 | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =head1 Name | ||||||||||||||
| 62 | |||||||||||||||
| 63 | Encode::Unicode::PerlDecodeJava - Encode a Unicode string in Perl and decode it in Java | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =head1 Synopsis | ||||||||||||||
| 66 | |||||||||||||||
| 67 | use Encode::Unicode::PerlDecodeJava; | ||||||||||||||
| 68 | |||||||||||||||
| 69 | ok $_ eq decode93(encode93($_)) for(qw(aaa (𝝰𝝱𝝲) aaa𝝰𝝱𝝲aaa yüz)) | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =head1 Description | ||||||||||||||
| 72 | |||||||||||||||
| 73 | |||||||||||||||
| 74 | =head1 Index | ||||||||||||||
| 75 | |||||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 Installation | ||||||||||||||
| 78 | |||||||||||||||
| 79 | This module is written in 100% Pure Perl and, thus, it is easy to read, use, | ||||||||||||||
| 80 | modify and install. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Standard Module::Build process for building and installing modules: | ||||||||||||||
| 83 | |||||||||||||||
| 84 | perl Build.PL | ||||||||||||||
| 85 | ./Build | ||||||||||||||
| 86 | ./Build test | ||||||||||||||
| 87 | ./Build install | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =head1 Author | ||||||||||||||
| 90 | |||||||||||||||
| 91 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||||||||||||||
| 92 | |||||||||||||||
| 93 | L<http://www.appaapps.com|http://www.appaapps.com> | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 Copyright | ||||||||||||||
| 96 | |||||||||||||||
| 97 | Copyright (c) 2016-2017 Philip R Brenan. | ||||||||||||||
| 98 | |||||||||||||||
| 99 | This module is free software. It may be used, redistributed and/or modified | ||||||||||||||
| 100 | under the same terms as Perl itself. | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =cut | ||||||||||||||
| 103 | |||||||||||||||
| 104 | |||||||||||||||
| 105 | # Tests and documentation | ||||||||||||||
| 106 | |||||||||||||||
| 107 | sub test | ||||||||||||||
| 108 | 1 | ▶ | {my $p = __PACKAGE__; | ||||||||||||
|
Subroutine
test: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 109 | 1 | ▶ | return if eval "eof(${p}::DATA)"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 1 | my $s = eval "join('', <${p}::DATA>)"; | |||||||||||||
| 111 | 1 | ▶ | $@ and die $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 1 | ▶ | eval $s; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | 1 | ▶ | $@ and die $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 1 | ▶ | test unless caller; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | 1 | 1; | |||||||||||||
| 119 | __DATA__ | ||||||||||||||