Just in time processing on a character stream: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
|||
Line 503: | Line 503: | ||
Silence-Dogood. |
Silence-Dogood. |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2019.03}} |
|||
This is a somewhat fragile toy encoder / decoder and shouldn't be used for anything serious. |
|||
<lang perl6>#`[ |
|||
Set srand to set the encode / decode "key", and |
|||
generate a lazy, reproducible stream of "random" |
|||
characters. Need to use the same key and same |
|||
implementation of Perl 6 to encode / decode. |
|||
Gain "security" by exchanging keys by a second |
|||
channel. Default key is 'Perl 6' |
|||
] |
|||
unit sub MAIN ($key = 'Perl 6'); |
|||
srand $key.comb(/<.alnum>/).join.parse-base(36) % 2**63; |
|||
# random but reproducible infinite stream of characters |
|||
my @stream = (flat ' ' .. '~').roll(*); |
|||
sub jit-encode (Str $str) { |
|||
my $i = 0; |
|||
my $last = 0; |
|||
my $enc = ''; |
|||
for $str.comb -> $c { |
|||
my $h; |
|||
my $l = ''; |
|||
++$i until $c eq @stream[$i]; |
|||
my $o = $i - $last; |
|||
$l = $o % 26; |
|||
$h = $o - $l if $o > 26; |
|||
$l += 10; |
|||
$enc ~= ($h ?? $h.base(36).uc !! '') ~ ($l.base(36).lc); |
|||
$last = $i; |
|||
} |
|||
$enc |
|||
} |
|||
sub jit-decode (Str $str) { |
|||
$str ~~ m:g/((.*?) (<:Ll>))/; |
|||
my $dec = ''; |
|||
my $i = 0; |
|||
for $/.List -> $l { |
|||
my $o = ($l[0][1].Str.parse-base(36) - 10 // 0) + |
|||
($l[0][0].Str.parse-base(36) // 0); |
|||
$i += $o; |
|||
$dec ~= @stream[$i]; |
|||
} |
|||
$dec |
|||
} |
|||
my $enc = jit-encode('In my opinion, this task is pretty silly.'); |
|||
say "Encoded\n$enc\n\nDecoded\n", jit-decode($enc);</lang> |
|||
<pre>Encoded |
|||
26j52dhl2Wp1Gw2WceQj1Go3MyQc1Gd52a1Gb1Ga2Wcq26o4Cwf3MiQsQn2Wglk3MhkQyeQjas3MkQn2Wd26aaQcQj |
|||
Decoded |
|||
In my opinion, this task is pretty silly.</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |