Skip to content

Instantly share code, notes, and snippets.

@ugexe
Last active July 6, 2016 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/baa168a641894a0731595c812724f76d to your computer and use it in GitHub Desktop.
Save ugexe/baa168a641894a0731595c812724f76d to your computer and use it in GitHub Desktop.
# always fails first time after `rm -rf lib/.precomp` with a segfault
# but always passes all subsequent runs
# removed --> Seq and return an array
multi sub decode-base64(Str $str, :$pad, :@alpha, |c) {
my $encodings = chars64with(@alpha);
my @values = $str.comb(/@$encodings/).rotor(4, :partial).map: -> $chunk {
state %lookup = $encodings.kv.hash.antipairs;
my $n = [+] $chunk.map: { (%lookup{$_} || 0) +< ((state $m = 24) -= 6) }
my $res = (16, 8, 0).map: { $n +> $_ +& 255 }
#slip($res.grep(* > 0));
slip($res.head( 3 - ( 4 - $chunk.elems ) ) );
}
return @values;
}
# gives correct results, but segfaults randomly (also Internal error: invalid thread ID 65 in GC work pass)
multi sub decode-base64(Str $str, :$pad, :@alpha, |c --> Seq) {
my $encodings = chars64with(@alpha);
my @result = $str.comb(/@$encodings/).rotor(4, :partial).map: -> $chunk {
state %lookup = $encodings.kv.hash.antipairs;
my $n = [+] $chunk.map: { (%lookup{$_} || 0) +< ((state $m = 24) -= 6) }
my $res = (16, 8, 0).map: { $n +> $_ +& 255 }
#slip($res.grep(* > 0));
slip($res.head( 3 - ( 4 - $chunk.elems ) ) );
}
@result.Seq;
}
# always fails, losing items at index 1 and 2
multi sub decode-base64(Str $str, :$pad, :@alpha, |c --> Seq) {
my $encodings = chars64with(@alpha);
$str.comb(/@$encodings/).rotor(4, :partial).map: -> $chunk {
state %lookup = $encodings.kv.hash.antipairs;
my $n = [+] $chunk.map: { (%lookup{$_} || 0) +< ((state $m = 24) -= 6) }
my $res = (16, 8, 0).map: { $n +> $_ +& 255 }
#slip($res.grep(* > 0));
slip($res.head( 3 - ( 4 - $chunk.elems ) ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment