diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-10-27 21:44:05 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-10-27 21:44:05 +0200 |
commit | 6388d0894405e30e463ae1bd719d52ae4b09813d (patch) | |
tree | 5732e865dc5d8bbde15561f0549b70c220d80133 | |
parent | f7b1fca131331f2dacae8aa8f58d7d3d68682cec (diff) | |
download | mew-6388d0894405e30e463ae1bd719d52ae4b09813d.tar.gz mew-6388d0894405e30e463ae1bd719d52ae4b09813d.tar.xz mew-6388d0894405e30e463ae1bd719d52ae4b09813d.zip |
gsplit: split on single-chars when pattern matches nothing
Also fixes issue when you have fewer fields than the maximum.
-rw-r--r-- | mew.scm | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/mew.scm b/mew.scm index 147b538..c5d200c 100644 --- a/mew.scm +++ b/mew.scm @@ -401,21 +401,26 @@ (n 1) (max (optional max -1))) (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)) - (when (= start (len str)) - (set! start -1)) - s) - (let ((s (substring str start))) - (set! n (inc n)) - (set! start -1) - s))) - (eof))))) + (cond ((< start 0) (eof)) + ((<= 0 max n) (let ((s (substring str start))) + (set! start -1) + s)) + (else (let ((data (irregex-search pat str start))) + (if data + (let ((s (substring str start (irregex-match-start-index data 0)))) + (set! n (inc n)) + (if (equal? s "") + (begin + (set! s (substring str start (inc start))) + (set! start (inc start))) + (set! start (irregex-match-end-index data 0))) + (when (= start (len str)) + (set! start -1)) + s) + (begin + (let ((s (substring str start))) + (set! start -1) + s))))))))) (define (slurp io) (cond ((not io) (read-string #f (current-input-port))) |