summary refs log tree commit diff
path: root/mew.scm
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 /mew.scm
parenta6361db705521cb10c55e334352d5144dc146bb9 (diff)
downloadmew-f35a817f5d54dd58c12f5d823e309749e4501c08.tar.gz
mew-f35a817f5d54dd58c12f5d823e309749e4501c08.tar.xz
mew-f35a817f5d54dd58c12f5d823e309749e4501c08.zip
add gsplit
Diffstat (limited to 'mew.scm')
-rw-r--r--mew.scm21
1 files changed, 20 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))