Just in time processing on a character stream: Difference between revisions

→‎{{header|Perl 6}}: Made a bit more robust
(Added Perl example)
(→‎{{header|Perl 6}}: Made a bit more robust)
Line 571:
{{works with|Rakudo|2019.03}}
 
This is a somewhatsomething fragileof a toy encoder / decoder and probably shouldn't be used for anything serious.
 
Default encode/decode key is 'Perl 6' Feed it a pass phrase at the command line to use that instead.
 
Will handle any visible character in the ASCII range as well as space and new-line.
 
<lang perl6>#`[
Set srand to set the encode / decode "key", and.
characters. Need to use the same "key" and same implementation
generate a lazy, reproducible stream of "random"
implementation of Perl 6 to encode / decode. Gain "security" by
characters. Need to use the same key and same
exchanging "keys" by a second channel. Default
implementation of Perl 6 to encode / decode.
"key" is "Perl 6"
Gain "security" by exchanging keys by a second
channel. Default key is 'Perl 6'
]
 
Line 586 ⟶ 589:
srand $key.comb(/<.alnum>/).join.parse-base(36) % 2**63;
 
my @stream = (flat "\n", ' ' .. '~').roll(*);
# random but reproducible infinite stream of characters
my @stream = (flat ' ' .. '~').roll(*);
 
sub jit-encode (Str $str) {
Line 596 ⟶ 598:
my $h;
my $l = '';
++$i until $i > 1 && $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;
}
my $encblock = 60;
$enc.comb($block).join: "\n"
}
 
sub jit-decode (Str $str is copy) {
$str.=subst("\n", '', :g);
$str ~~ m:g/((.*?) (<:Ll>))/;
my $dec = '';
Line 620 ⟶ 624:
}
 
my $secret = q:to/END/;
my $enc = jit-encode('In my opinion, this task is pretty silly.');
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890
END
 
say "== Secret: ==\n$secret";
 
say "\n== Encoded: ==";
say my $enc = jit-encode($secret);
 
say "\n== Decoded: ==";
say "Encoded\n$enc\n\nDecoded\n", jit-decode($enc);</lang>
 
<pre>== Secret: ==
In my opinion, this task is pretty silly.</pre>
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890
 
 
<pre>== Encoded: ==
26j52d6Ie1Ge4Cd26po1GdsQa3Ms52piQd4Cn3Md2Wcf1GtciQz1GwQb5Si2
WnQlQmQjQv1GmQra2Wt4Cpc1Gysatu26va1Gq52x4Cp1Gv4CeQv1Gb1Gp4Co
6IbaQyAUmd26a7Yw3Mh2Wu26v1GfQsQwbQpn26z1Gi1Gl1GmQb1Gfs26v4Ce
Qy2Wm78xaaa4Cj26x6If3Msqu2Wx2Wku1Gh52ydQh3Mb78rll1GvcQap5Sgy
Qm26s1Gh26clj1Gm1GzA4y26bat1Gdk1Gs1Gpm1GlQs7Ys52dQw1Giv5Se5S
u3Mb1Gucn4Cq26h26pQq2Wh5Sh7Yi8OrpQl26p26i3MqtQiQhQi4Ckb3Mi
 
== Decoded: ==
say "Encoded\n$enc\n\nDecoded\n", jit-decode($enc);</lang>
In my opinion, this task is pretty silly.
 
'Twas brillig, and the slithy toves
<pre>Encoded
Did gyre and gimble in the wabe.
26j52dhl2Wp1Gw2WceQj1Go3MyQc1Gd52a1Gb1Ga2Wcq26o4Cwf3MiQsQn2Wglk3MhkQyeQjas3MkQn2Wd26aaQcQj
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre>
Decoded
In my opinion, this task is pretty silly.</pre>
 
=={{header|Python}}==
10,327

edits