Just in time processing on a character stream: Difference between revisions
Just in time processing on a character stream (view source)
Revision as of 15:39, 30 May 2019
, 4 years ago→{{header|Perl 6}}: Made a bit more robust
SqrtNegInf (talk | contribs) (Added Perl example) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Made a bit more robust) |
||
Line 571:
{{works with|Rakudo|2019.03}}
This is a
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"
▲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"
]
Line 586 ⟶ 589:
srand $key.comb(/<.alnum>/).join.parse-base(36) % 2**63;
my @stream = (flat "\n", ' ' .. '~').roll(*);▼
▲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 $
$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/;
'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: ==";
<pre>== Secret: ==
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
!@#$%^&*()_+}{[].,><\|/?'";:1234567890
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.
!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre>
▲Decoded
▲In my opinion, this task is pretty silly.</pre>
=={{header|Python}}==
|