Permalink
Browse files

Add .split, .index, .rindex, .lines, .words, more features to .comb

  • Loading branch information...
1 parent 41acd2e commit 9f864d48d4565269be8376ccd2c56a43a0d3bc31 @sorear committed Dec 27, 2010
Showing with 105 additions and 11 deletions.
  1. +2 −1 lib/CLRBackend.cs
  2. +2 −0 lib/Kernel.cs
  3. +66 −5 lib/SAFE.setting
  4. +35 −5 test2.pl
View
@@ -3062,7 +3062,8 @@ class NamProcessor {
public CpsOp SubInfoCtor() {
CpsOp[] args = new CpsOp[10];
- args[0] = CpsOp.StringLiteral(sub.unit.name + " " + sub.name);
+ args[0] = CpsOp.StringLiteral(sub.unit.name + " " +
+ (sub.name == "ANON" ? cpb.mb.Name : sub.name));
args[1] = CpsOp.NewIntArray(cpb.cx.lineBuffer.ToArray());
args[2] = CpsOp.DBDLiteral(cpb.mb);
args[3] = (sub.outer != null) ?
View
@@ -386,6 +386,8 @@ public class SubInfo {
Frame thn = Kernel.GetInferiorRoot()
.MakeChild(th, (SubInfo) rbuf[rbase + 1 + names]);
src = Kernel.RunInferior(thn);
+ if (src == null)
+ throw new Exception("Improper null return from sub default for " + PName(rbase));
goto gotit;
}
if ((flags & SIG_F_OPTIONAL) != 0) {
View
@@ -60,6 +60,8 @@ my class Any is Mu {
}
my class Cursor { ... }
+my class Regex { ... }
+my class Num { ... }
my class Cool {
method grep($sm) {
gather self.for(-> $r {
@@ -80,21 +82,80 @@ my class Cool {
};
}
- method comb($rx) {
+ method split($matcher, $limit = 1_000_000_000, :$all?) {
+ my $matchrx = (($matcher ~~ Regex) ?? $matcher !! /$matcher/);
my $str = ~self;
my $C = Cursor.new($str);
+ my @out;
my $i = 0;
+ my $last = 0;
+ my $limctr = $limit;
+ my $M;
+ while ($i <= chars $str) && ($limctr > 1) {
+ $M = first($matchrx($C.cursor($i++)));
+ if $M {
+ push @out, substr($str, $last, ($M.from - $last));
+ push @out, $M if $all;
+ $i = ($last = $M.to);
+ $limctr = ($limctr - 1);
+ }
+ }
+ push @out, substr($str, $last, (chars($str) - $last));
+ @out;
+ }
+
+ method index($substring, $pos = 0) {
+ my $str = ~self;
+ my $fromc = $pos;
+ my $len = chars $substring;
+ my $maxi = chars($str) - $len;
+ while $fromc <= $maxi {
+ if substr($str,$fromc,$len) eq $substring {
+ return $fromc;
+ }
+ $fromc++;
+ }
+ Num; # XXX StrPos
+ }
+
+ method rindex($substring, $from?) {
+ my $str = ~self;
+ my $len = chars $substring;
+ my $fromc = (($from // 1_000_000_000) min (chars($str) - $len));
+ while $fromc >= 0 {
+ if substr($str,$fromc,$len) eq $substring {
+ return $fromc;
+ }
+ $fromc = $fromc - 1;
+ }
+ Num; # XXX StrPos
+ }
+
+ my $char = /./;
+ method comb($matcher = $char, $limit = 1_000_000_000, :$match) {
+ my $str = ~self;
+ my $C = Cursor.new($str);
+ my $i = 0;
+ my $limctr = $limit;
my @out;
- while $i < chars $str {
- my $M = first($rx($C.cursor($i++)));
+ while ($i < chars $str) && $limctr {
+ my $M = first($matcher($C.cursor($i++)));
if $M {
$i max= $M.to;
- push @out, ~$M;
- } else {
+ push @out, ($match ?? $M !! (~$M));
+ $limctr = $limctr - 1;
}
}
@out
}
+
+ method lines($limit = 1_000_000_000) {
+ self.comb(/ ^^ \N* /, $limit);
+ }
+
+ method words($limit = 1_000_000_000) {
+ self.comb(/ \S+ /, $limit);
+ }
method say() { (~self).say }
method chars() { chars(self) }
method substr($x,$y) { substr(self,$x,$y) }
View
@@ -2,8 +2,10 @@
use Test;
use MONKEY_TYPING;
+augment class Cool {
+}
+
{
- # GH-3
my class A { method Numeric { 42 } }
is A.new + 23, 65, '+ calls user-written .Numeric';
}
@@ -40,16 +42,44 @@
}
{
+ my @arr;
+ my $ix = -1;
+ ok !(defined @arr[$ix]), "can index before arrays to get undef";
+}
+
+{
+ is ("foo bar baz".split(/\s/).join('|')), 'foo|bar|baz', 'basic split';
+ is ("foo bar baz".split(' ').join('|')), 'foo|bar|baz', 'split with string';
+ is ("foo bar baz".split(' ', 2).join('|')), 'foo|bar baz',
+ 'split with a limit';
+ is (" foo bar".split(' ').join('|')), '||foo|bar',
+ 'split with leading empty fields';
+ is ("foo bar ".split(' ').join('|')), 'foo|bar||',
+ 'split with trailing empty fields';
+ try { "foo bar".split };
+ ok $!, 'split requires an argument';
+ is ("ax+by*cz".split(/\W/, :all).join('|')), 'ax|+|by|*|cz',
+ 'split :all';
+
+ is "hello world".index('l'), 2, ".index";
+ is "hello world".index('l',5), 9, ".index with restart point";
+ ok (!defined("hello world".index('x'))), ".index off end";
+ is "hello world".rindex('l'), 9, ".rindex";
+ is "hello world".rindex('l', 6), 3, ".rindex with restart point";
+ ok (!defined("hello world".rindex('x'))), ".rindex off end";
+
+ is ("abc".comb.join('|')), 'a|b|c', 'comb with default matcher';
+ is ("abc".comb(/./, 2).join('|')), 'a|b', 'comb with limit';
+ is ("A1 B2 C3".comb(/(\w)(\d)/, :match).[2].[1]), 3, 'comb :match';
+}
+
+{
my $str = '';
$str ~= 1;
INIT $str ~= 2;
$str ~= 3;
INIT $str ~= 4;
is $str, '2413', 'INIT blocks run in correct order';
-
- my @arr;
- my $ix = -1;
- ok !(defined @arr[$ix]), "can index before arrays to get undef";
}
#is $?FILE, 'test.pl', '$?FILE works';

0 comments on commit 9f864d4

Please sign in to comment.