Skip to content

Commit

Permalink
Implement Str.chars and Str.substr
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 20, 2010
1 parent 63ae8f5 commit 69370b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CodeGen.pm
Expand Up @@ -38,6 +38,9 @@ use 5.010;
{ islist => [f => 'Boolean'] },
'CLRImportObject' =>
{ val => [f => 'Object'] },
'String' =>
{ Length => [f => 'Int32'],
Substring => [m => 'String'] },
'Kernel.SlurpyHelper' => [c => 'List<Variable>'],
'Kernel.Bind' => [c => 'Void'],
Expand Down
8 changes: 8 additions & 0 deletions setting
Expand Up @@ -174,6 +174,14 @@ my class Num {
my class Str {
method Str () { self }
method ACCEPTS($t) { self eq $t }
method chars() { Q:CgOp {
(box Num (cast Double (getfield Length (unbox String (@ (l self))))))
} }
method substr($from, $len) { Q:CgOp {
(box Str (rawcall [unbox String (@ (l self))] Substring
[cast Int32 (unbox Double (@ (l $from)))]
[cast Int32 (unbox Double (@ (l $len)))]))
} }
}
my class Blob { }
my class Char { }
Expand Down
7 changes: 6 additions & 1 deletion test.pl
Expand Up @@ -10,7 +10,7 @@ ($num)
say ("1.." ~ $num);
}

plan 91;
plan 93;

ok 1, "one is true";
ok 2, "two is also true";
Expand Down Expand Up @@ -286,3 +286,8 @@ ($num)
ok foo == 42, "constants without sigils work";
ok $bar == 51, "constants with sigils work";
}

{
ok "Hello".substr(1,3) eq "ell", "substr works";
ok "Hello".chars == 5, ".chars works";
}

0 comments on commit 69370b6

Please sign in to comment.