Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[module-management] replace hard tabs with spaces
  • Loading branch information
Paul Cochrane committed Mar 7, 2015
1 parent a1d2c40 commit 6c64b9a
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions categories/module-management/Fletcher.pl
Expand Up @@ -25,44 +25,47 @@
=end pod

my @modules = (
{name => "Foo::Bar", meta => {auth=>'mailto:dave@example.com', ver=>'1.2.0'}},
{name => "Foo::動", meta => {auth=>'mailto:chang@example.com', ver=>'2.1.3'}}
{name => "Foo::Bar", meta => {auth=>'mailto:dave@example.com', ver=>'1.2.0'}},
{name => "Foo::動", meta => {auth=>'mailto:chang@example.com', ver=>'2.1.3'}}
);

say filename(@modules[0]<name>,@modules[0]<meta>);
say filename(@modules[1]<name>,@modules[1]<meta>);

sub filename($module,%meta) {
my $filename = strencode($module);
for %meta.sort {
my $v = .value ~~ m/[\D & <-[.]>]/ ?? fletcher16(.value) !! .value;
$filename ~= "-" ~ .key ~ ":$v";
}
return $filename ~ "--0";
my $filename = strencode($module);
for %meta.sort {
my $v = .value ~~ m/[\D & <-[.]>]/ ?? fletcher16(.value) !! .value;
$filename ~= "-" ~ .key ~ ":$v";
}
return $filename ~ "--0";
}

sub strencode($str) {
return $str.subst(/(<-alpha -[:]>)/,{ charencode($0) },:g);
return $str.subst(/(<-alpha -[:]>)/,{ charencode($0) },:g);
}

sub charencode($char) {
my ($url,$hex) = ('',$char.fmt("%02x"));
my ($url,$hex) = ('',$char.fmt("%02x"));

while $hex.chars {
$url ~= '%' ~ $hex.substr(0,2);
$hex = $hex.substr(2);
}
return $url;
while $hex.chars {
$url ~= '%' ~ $hex.substr(0,2);
$hex = $hex.substr(2);
}
return $url;
}

sub fletcher16($str) {
my ($A,$B) = (0,0);
for map { .ord }, $str.comb -> $val {
if $val > 255 {
$A = ($A + $val div 255) % 255;
$B = ($B + $A) % 255;
}
$A = ($A + $val % 255) % 255;
$B = ($B + $A) % 255;
}
return ($A*256 + $B).fmt("%04x");
my ($A,$B) = (0,0);
for map { .ord }, $str.comb -> $val {
if $val > 255 {
$A = ($A + $val div 255) % 255;
$B = ($B + $A) % 255;
}
$A = ($A + $val % 255) % 255;
$B = ($B + $A) % 255;
}
return ($A*256 + $B).fmt("%04x");
}

# vim: expandtab shiftwidth=4 ft=perl6

0 comments on commit 6c64b9a

Please sign in to comment.