diff --git a/CodeGen.pm b/CodeGen.pm index ef38f59e..124c4886 100644 --- a/CodeGen.pm +++ b/CodeGen.pm @@ -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'], 'Kernel.Bind' => [c => 'Void'], diff --git a/setting b/setting index 6911ac75..b42698e5 100644 --- a/setting +++ b/setting @@ -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 { } diff --git a/test.pl b/test.pl index 6e28a548..ff2cdc4e 100644 --- a/test.pl +++ b/test.pl @@ -10,7 +10,7 @@ ($num) say ("1.." ~ $num); } -plan 91; +plan 93; ok 1, "one is true"; ok 2, "two is also true"; @@ -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"; +}