Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add &hash, 1-arg .substr, .subst with Str lhs, allow assigning .kv to…
… hashes
  • Loading branch information
sorear committed May 27, 2011
1 parent 9e75e8d commit b084a1e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/CORE.setting
Expand Up @@ -100,9 +100,10 @@ my class Cool {
push @out, substr($str, $last, (chars($str) - $last));
@out;
}
method subst($matcher, $replacement, :g(:$global)) {
method subst($matcher_, $replacement, :g(:$global)) {
my $str = ~self;
my $C = Cursor.new($str);
my $matcher = $matcher_ ~~ Regex ?? $matcher_ !! /$matcher_/;
my $i = 0;
my $to = 0;
my $limctr = $global ?? Inf !! 1;
Expand Down Expand Up @@ -186,7 +187,7 @@ my class Cool {
my $s = ~self;
substr($s, 0, chars($s) - 1)
}
method substr($x,$y) { substr(self,$x,$y) }
method substr($x, $y = chars(self)-$x) { substr(self,$x,$y) }
method lc() { Q:CgOp { (box Str (str_tolower (obj_getstr {self}))) }}
method uc() { Q:CgOp { (box Str (str_toupper (obj_getstr {self}))) }}
method flip() { Q:CgOp { (box Str (str_flip (obj_getstr {self}))) }}
Expand Down Expand Up @@ -273,7 +274,7 @@ my class Str is Cool {
)
} }
method Numeric() { Q:CgOp { (box Num (str_tonum (obj_getstr {self}))) } }
method substr($from, $len) { substr(self, $from, $len) }
method substr($from, $len = chars(self)-$from) { substr(self, $from, $len) }
# XXX .trans
method perl() { defined(self) ?? '"' ~ self ~ '"' !! self.typename }
}
Expand Down Expand Up @@ -561,6 +562,7 @@ my class IterCursor {
}
sub flat(*@x) { @x }
sub hash(\|@x) { %(@x.unwrap-single) }
my class Whatever {
method ACCEPTS(Mu $x) { defined(self) || $x.^isa(Whatever) }
Expand Down Expand Up @@ -817,8 +819,13 @@ my class Hash {
(vvarlist_push (l iter) {$in})
(whileloop 0 0 (iter_hasflat (l iter))
(letn elt (@ (vvarlist_shift (l iter)))
(varhash_setindex (obj_getstr (getslot key var (l elt)))
(l into) (nsw (@ (getslot value var (l elt)))))))
(ternary (obj_isa (l elt) (class_ref mo Pair))
(varhash_setindex (obj_getstr (getslot key var (l elt)))
(l into) (nsw (@ (getslot value var (l elt)))))
(ternary (iter_hasflat (l iter))
(varhash_setindex (obj_getstr (ns (l elt))) (l into)
(nsw (@ (vvarlist_shift (l iter)))))
(sink (die "Unmatched key in Hash.LISTSTORE"))))))
(setbox (l sobj) (l into))
(newrwlistvar (l sobj)))
};
Expand Down
10 changes: 10 additions & 0 deletions test2.pl
Expand Up @@ -17,6 +17,16 @@
my $str = '';
for 1,2,3,4 -> $x, $y { $str ~= "$x|$y," }
is $str, "1|2,3|4,", 'multivariable for works';

is "moo".subst('o','a',:g), "maa", '.subst can take Str';
is 'Hello'.substr(1), 'ello', '.substr can take 1 arguaent';
is hash((a => 1)).perl, '{"a" => 1}.hash', '&hash works (1)';
is hash((a => 1, b => 2)).<b>, 2, '&hash works (2)';
is hash({a => 1}).perl, '{"a" => 1}.hash', '&hash works (3)';

my %hash = "foo", 5;
is %hash<foo>, 5, "Hash.LISTSTORE can take keys and values separately";
dies_ok { %hash = "pie" }, "keys must be matched";
}

{
Expand Down

0 comments on commit b084a1e

Please sign in to comment.