taking the example from
http://rot8000.com/info as testcase
FYI:
# $chinese = "籝籱籮 籫籾籽籵籮类 籭籲籭 籲籽簪"
use strict;
use warnings;
use utf8;
use open ":std", ":encoding(UTF-8)";
use Test::More;
# rot8000 v1.005;
my %rot;
init_rot();
my $english = "The butler did it!";
my $chinese = "籝籱籮 籫籾籽౲
+1;籮类 籭籲籭 籲籽簪"
+;
# fix PerlMonks' Uni<code>Mess
$chinese =~ s/&#(\d+);/chr($1)/ge;
is(
rot8000($chinese),
$english,
"chin2engl -> $english"
);
is(
rot8000($english),
$chinese,
"engl2chin -> $chinese"
);
my $random = join "", map { chr int rand 2**16-1 } 1..30;
is(
rot8000(rot8000($random)),
$random,
"Identity"
);
done_testing;
sub rot8000 {
my ($in) = @_;
my $out;
for my $char (split //,$in) {
$out .= $rot{$char} // $char;
}
return $out;
}
sub init_rot{
my @toggles = reverse (0, 33,127,161,5760,5761,8192,8203,8232,8234
+,8239,8240,8287,8288,12288,12289,55296,57344);
my @allowed = map chr, 0.. 2**16-1;
while ( my ($stop,$start) = splice @toggles, 0, 2 ) {
#say "$start-$stop";
splice @allowed, $start, $stop-$start;
}
my @allowed_low = splice @allowed, 0, (@allowed/2);
@rot{@allowed_low} = @allowed;
@rot{@allowed} = @allowed_low;
}