The attached split.diff file is just for demonstration, not a patch
submittal.

I made a method on Str called "match" that returns a List of all matches:

# returns all matches on a regex
.sub 'match' :method :multi(_, 'Sub')
    .param pmc regex
    .local pmc match
    .local pmc tmpstr
    .local pmc retv

    retv = new 'List'
    match = regex.'ACCEPTS'(self)

    loop:
      $S0 = match.'text'()
      if $S0 == '' goto done
      retv.'push'($S0)
      match.'next'()
      goto loop

    done:
      .return(retv)
.end

However, running the following code:
say "ab1cd12ef123gh".match(/\d+/).perl;

We get:
["1", "12", "1", "2", "123", "12", "1", "23", "2", "3"]

As you can see match is returning what seems to be both greedy *and*
non-greedy matches (and everything in between). Is this correct?
Index: src/classes/Str.pir
===================================================================
--- src/classes/Str.pir	(revision 31220)
+++ src/classes/Str.pir	(working copy)
@@ -76,6 +76,78 @@
     .return(retv)
 .end
 
+# returns all matches on a regex
+.sub 'match' :method :multi(_, 'Sub')
+    .param pmc regex
+    .local pmc match
+    .local pmc tmpstr
+    .local pmc retv
+
+    retv = new 'List'
+    match = regex.'ACCEPTS'(self)
+
+    loop:
+      $S0 = match.'text'()
+      if $S0 == '' goto done
+      retv.'push'($S0)
+      match.'next'()
+      goto loop
+
+    done:
+      .return(retv)
+.end
+
+# split a string on a regex
+.sub 'split' :method :multi(_, 'Sub')
+    .param pmc regex 
+    .local pmc match
+    .local pmc tmpstr
+    .local pmc retv
+    .local int start_pos
+    .local int end_pos
+
+    retv = new 'List'
+
+    match = regex.'ACCEPTS'(self)
+    unless match goto done
+
+    start_pos = 0
+    end_pos = match.'from'()
+
+  loop:
+    print "start_pos = "
+    print start_pos
+    print "; end_pos = "
+    say end_pos
+    $S7 = match.'text'()
+    say $S7
+
+    tmpstr = new 'Perl6Str'
+    $S0 = substr self, start_pos, end_pos
+    tmpstr = $S0
+    retv.'push'(tmpstr)
+
+    start_pos = match.'to'()
+
+    match.'next'()
+
+    end_pos = match.'from'()
+    end_pos -= start_pos
+
+    $S1 = match.'text'()
+    if $S1 == '' goto last
+    goto loop
+
+  last:
+    tmpstr = new 'Perl6Str'
+    $S0 = substr self, start_pos, end_pos
+    tmpstr = $S0
+    retv.'push'(tmpstr)
+
+  done:
+    .return(retv)
+.end
+
 .sub lc :method
     .local string tmps
     .local pmc retv

Attachment: split.p6
Description: Binary data

Reply via email to