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

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(+Racket)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 233:
<pre>Silence-Dogood.</pre>
 
=={{header|C++ sharp|C#}}==
Text used to encode:[http://paulo-jorente.de/text/theRaven.txt The Raven - by E.A.Poe]
<lang cpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
 
typedef struct {
int s[4];
}userI;
 
class jit{
public:
void decode( std::string& file, std::vector<userI>& ui ) {
std::ifstream f( file.c_str(), std::ios_base::in );
fileBuffer = std::string( ( std::istreambuf_iterator<char>( f ) ), std::istreambuf_iterator<char>() );
f.close();
for( std::vector<userI>::iterator t = ui.begin(); t != ui.end(); t++ ) {
if( !decode( ( *t ).s ) ) break;
}
std::cout << "\n\n";
}
private:
bool decode( int* ui ) {
int l = 0, t = 0, p = 0, c = 0, a = 0;
for( std::string::iterator i = fileBuffer.begin(); i != fileBuffer.end(); i++ ) {
if( p == ui[0] && l == ui[1] && t == ui[2] && c == ui[3] ) {
if( *i == '!' ) return false;
std::cout << *i; return true;
}
if( *i == '\n' ) { l++; t = c = 0; }
else if( *i == '\t' ) { t++; c = 0; }
else if( *i == '\f' ) { p++; l = t = c = 0; }
else { c++;}
}
return false;
}
std::string fileBuffer;
};
void getUserInput( std::vector<userI>& u ) {
std::string h = "0 18 0 0 0 68 0 1 0 100 0 32 0 114 0 45 0 38 0 26 0 16 0 21 0 17 0 59 0 11 "
"0 29 0 102 0 0 0 10 0 50 0 39 0 42 0 33 0 50 0 46 0 54 0 76 0 47 0 84 2 28";
//std::getline( std::cin, h );
std::stringstream ss( h );
userI a;
int x = 0;
while( std::getline( ss, h, ' ' ) ) {
a.s[x] = atoi( h.c_str() );
if( ++x == 4 ) {
u.push_back( a );
x = 0;
}
}
}
int main( int argc, char* argv[] ) {
std::vector<userI> ui;
getUserInput( ui );
 
jit j;
j.decode( std::string( "theRaven.txt" ), ui );
return 0;
}
</lang>
{{out}}
<pre>
Silence-Dogood.
</pre>
 
=={{header|C#|C sharp}}==
{{trans|D}}
<lang csharp>using System;
Line 385 ⟶ 316:
{{out}}
<pre>Silence-Dogood.</pre>
 
=={{header|C++}}==
Text used to encode:[http://paulo-jorente.de/text/theRaven.txt The Raven - by E.A.Poe]
<lang cpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
 
typedef struct {
int s[4];
}userI;
 
class jit{
public:
void decode( std::string& file, std::vector<userI>& ui ) {
std::ifstream f( file.c_str(), std::ios_base::in );
fileBuffer = std::string( ( std::istreambuf_iterator<char>( f ) ), std::istreambuf_iterator<char>() );
f.close();
for( std::vector<userI>::iterator t = ui.begin(); t != ui.end(); t++ ) {
if( !decode( ( *t ).s ) ) break;
}
std::cout << "\n\n";
}
private:
bool decode( int* ui ) {
int l = 0, t = 0, p = 0, c = 0, a = 0;
for( std::string::iterator i = fileBuffer.begin(); i != fileBuffer.end(); i++ ) {
if( p == ui[0] && l == ui[1] && t == ui[2] && c == ui[3] ) {
if( *i == '!' ) return false;
std::cout << *i; return true;
}
if( *i == '\n' ) { l++; t = c = 0; }
else if( *i == '\t' ) { t++; c = 0; }
else if( *i == '\f' ) { p++; l = t = c = 0; }
else { c++;}
}
return false;
}
std::string fileBuffer;
};
void getUserInput( std::vector<userI>& u ) {
std::string h = "0 18 0 0 0 68 0 1 0 100 0 32 0 114 0 45 0 38 0 26 0 16 0 21 0 17 0 59 0 11 "
"0 29 0 102 0 0 0 10 0 50 0 39 0 42 0 33 0 50 0 46 0 54 0 76 0 47 0 84 2 28";
//std::getline( std::cin, h );
std::stringstream ss( h );
userI a;
int x = 0;
while( std::getline( ss, h, ' ' ) ) {
a.s[x] = atoi( h.c_str() );
if( ++x == 4 ) {
u.push_back( a );
x = 0;
}
}
}
int main( int argc, char* argv[] ) {
std::vector<userI> ui;
getUserInput( ui );
 
jit j;
j.decode( std::string( "theRaven.txt" ), ui );
return 0;
}
</lang>
{{out}}
<pre>
Silence-Dogood.
</pre>
 
=={{header|D}}==
Line 831:
Decoded
The slithey toves did gyre and gimble in the wabe</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2019.03}}
 
This is a something of 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".
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;
 
my @stream = (flat "\n", ' ' .. '~').roll(*);
 
sub jit-encode (Str $str) {
my $i = 0;
my $last = 0;
my $enc = '';
for $str.comb -> $c {
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 $block = 60;
$enc.comb($block).join: "\n"
}
 
sub jit-decode (Str $str is copy) {
$str.=subst("\n", '', :g);
$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 $secret = q:to/END/;
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 jit-decode($enc);</lang>
 
<pre>== Secret: ==
In my opinion, this task is pretty silly.
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890
 
 
== Encoded: ==
26j52d6Ie1Ge4Cd26po1GdsQa3Ms52piQd4Cn3Md2Wcf1GtciQz1GwQb5Si2
WnQlQmQjQv1GmQra2Wt4Cpc1Gysatu26va1Gq52x4Cp1Gv4CeQv1Gb1Gp4Co
6IbaQyAUmd26a7Yw3Mh2Wu26v1GfQsQwbQpn26z1Gi1Gl1GmQb1Gfs26v4Ce
Qy2Wm78xaaa4Cj26x6If3Msqu2Wx2Wku1Gh52ydQh3Mb78rll1GvcQap5Sgy
Qm26s1Gh26clj1Gm1GzA4y26bat1Gdk1Gs1Gpm1GlQs7Ys52dQw1Giv5Se5S
u3Mb1Gucn4Cq26h26pQq2Wh5Sh7Yi8OrpQl26p26i3MqtQiQhQi4Ckb3Mi
 
== Decoded: ==
In my opinion, this task is pretty silly.
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre>
 
=={{header|Phix}}==
Line 1,106 ⟶ 1,008:
Silence-Dogood.
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.03}}
 
This is a something of 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".
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;
 
my @stream = (flat "\n", ' ' .. '~').roll(*);
 
sub jit-encode (Str $str) {
my $i = 0;
my $last = 0;
my $enc = '';
for $str.comb -> $c {
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 $block = 60;
$enc.comb($block).join: "\n"
}
 
sub jit-decode (Str $str is copy) {
$str.=subst("\n", '', :g);
$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 $secret = q:to/END/;
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 jit-decode($enc);</lang>
 
<pre>== Secret: ==
In my opinion, this task is pretty silly.
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890
 
 
== Encoded: ==
26j52d6Ie1Ge4Cd26po1GdsQa3Ms52piQd4Cn3Md2Wcf1GtciQz1GwQb5Si2
WnQlQmQjQv1GmQra2Wt4Cpc1Gysatu26va1Gq52x4Cp1Gv4CeQv1Gb1Gp4Co
6IbaQyAUmd26a7Yw3Mh2Wu26v1GfQsQwbQpn26z1Gi1Gl1GmQb1Gfs26v4Ce
Qy2Wm78xaaa4Cj26x6If3Msqu2Wx2Wku1Gh52ydQh3Mb78rll1GvcQap5Sgy
Qm26s1Gh26clj1Gm1GzA4y26bat1Gdk1Gs1Gpm1GlQs7Ys52dQw1Giv5Se5S
u3Mb1Gucn4Cq26h26pQq2Wh5Sh7Yi8OrpQl26p26i3MqtQiQhQi4Ckb3Mi
 
== Decoded: ==
In my opinion, this task is pretty silly.
 
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.
 
!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre>
 
=={{header|REXX}}==
10,327

edits