summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-10-14 16:27:00 +0200
committerLeah Neukirchen <leah@vuxu.org>2022-10-14 16:27:00 +0200
commitf35a817f5d54dd58c12f5d823e309749e4501c08 (patch)
tree6d1db97f7f59cc7242a90eb37ac1cfbae87bca13
parenta6361db705521cb10c55e334352d5144dc146bb9 (diff)
downloadmew-f35a817f5d54dd58c12f5d823e309749e4501c08.tar.gz
mew-f35a817f5d54dd58c12f5d823e309749e4501c08.tar.xz
mew-f35a817f5d54dd58c12f5d823e309749e4501c08.zip
add gsplit
-rw-r--r--mew.scm21
-rw-r--r--mew.svnwiki8
2 files changed, 28 insertions, 1 deletions
diff --git a/mew.scm b/mew.scm
index b763d67..7e762c7 100644
--- a/mew.scm
+++ b/mew.scm
@@ -1,4 +1,4 @@
-(module mew (at dec def div empty? eof esc fin final for generic-for-each get gfix giterate gmatch inc keys keyvals len loc mod nth op prn puts rep str slurp tbl while until vals -> ->> ~?)
+(module mew (at dec def div empty? eof esc fin final for generic-for-each get gfix giterate gmatch gsplit inc keys keyvals len loc mod nth op prn puts rep str slurp tbl while until vals -> ->> ~?)
   (import-for-syntax matchable)
 
   (import scheme
@@ -289,6 +289,25 @@
                 (eof))))
           (eof)))))
 
+  (def (gsplit pat str . max)
+    (let ((start 0)
+          (n 1)
+          (max (if (null? max) -1 (car max))))
+      (lambda ()
+        (if (or (>= start 0)
+                (and (> max 0) (< n max)))
+          (let ((data (irregex-search pat str start)))
+            (if (and data (or (< max 0) (< n max)))
+              (let ((s (substring str start (irregex-match-start-index data 0))))
+                (set! n (inc n))
+                (set! start (irregex-match-end-index data 0))
+                s)
+              (let ((s (substring str start)))
+                (set! n (inc n))
+                (set! start -1)
+                s)))
+          (eof)))))
+
   (def (slurp io)
     (cond ((not io)          (read-string #f (current-input-port)))
           ((input-port? io)  (read-string #f io))
diff --git a/mew.svnwiki b/mew.svnwiki
index 91bdf79..3372ff3 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -218,6 +218,14 @@ Returns a generator that for each match of the irregex {{<irx>}} in
 the string {{<str>}} either yields the match, or a list of all
 match data strings (if there are any).
 
+<procedure>(gsplit <irx> <str> <max>?)</procedure>
+
+Returns a generator that yields the strings between matches of the
+irregex {{<irx>}} in the string {{<str>}}, at most {{<max>>} times
+(by default, unlimited).
+
+When the pattern {{<irx>}} uses match data, the result is unspecified.
+
 
 == Special syntax