Skip to content

Commit

Permalink
translated &unpack from PIR to Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Masak committed Mar 17, 2009
1 parent 1ea27f2 commit 72f522d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 71 deletions.
72 changes: 1 addition & 71 deletions src/builtins/any-str.pir
Expand Up @@ -23,7 +23,7 @@ the size of that file down and to emphasize their generic,
.namespace []
.sub 'onload' :anon :init :load
$P0 = get_hll_namespace ['Any']
'!EXPORT'('capitalize,chomp,chars,:d,:e,:f,index,lc,rindex,ord,substr,trim,uc,unpack', 'from'=>$P0)
'!EXPORT'('capitalize,chomp,chars,:d,:e,:f,index,lc,rindex,ord,substr,trim,uc', 'from'=>$P0)
.end
Expand Down Expand Up @@ -1006,76 +1006,6 @@ full "uppercase".
.return(retv)
.end

=item unpack

our List multi Str::unpack ( Str $template, Str $packval )

Takes a string and expands it out into a list of values.

=cut

.namespace['Any']
.sub 'unpack' :multi(_, _)
.param string template
.param string packval
.local pmc retv
.local int len

retv = new 'List'

len = length template
if len == 0 goto done

.local int pos
.local int packpos
pos = 0
packpos = 0

next_directive:
$S0 = substr template, pos, 1
if $S0 == 'A' goto ascii
if $S0 == 'x' goto skip
if $S0 == ' ' goto space
goto fail

ascii:
pos += 1
if pos == len goto fail
$S0 = substr template, pos, 1
$I0 = ord $S0
$I0 -= 48
$S1 = substr packval, packpos, $I0
retv.'push'($S1)
packpos += $I0
pos += 1
if pos == len goto done
goto next_directive

skip:
pos += 1
if pos == len goto fail
$S0 = substr template, pos, 1
$I0 = ord $S0
$I0 -= 48
$S1 = substr packval, packpos, $I0
packpos += $I0
pos += 1
if pos == len goto done
goto next_directive

space:
pos += 1
if pos == len goto done
goto next_directive

done:
.return(retv)

fail:
$P0 = new 'Failure'
.return ($P0)
.end

# Local Variables:
# mode: pir
# fill-column: 100
Expand Down
14 changes: 14 additions & 0 deletions src/setting/Any-str.pm
Expand Up @@ -81,4 +81,18 @@ sub split($delimiter, $target) {
$target.split($delimiter);
}

sub unpack($template, $target) {
$template.trans(/\s+/ => '') ~~ / ((<[Ax]>)(\d+))* /
or return (); # unknown syntax
my $pos = 0;
return gather for $0.values -> $chunk {
my ($operation, $count) = $chunk.[0, 1];
given $chunk.[0] {
when 'A' { take $target.substr($pos, $count); }
when 'x' { } # just skip
}
$pos += $count;
}
}

# vim: ft=perl6

0 comments on commit 72f522d

Please sign in to comment.