lua: extend patterns to match/gmatch/gsub; gsub with string/function/table repl +6 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 23:13:05 +00:00
parent bd0377b6a3
commit e670e914e7
5 changed files with 102 additions and 60 deletions

View File

@@ -968,11 +968,16 @@
(define
lua-string-match
(fn (&rest args)
(let ((s (first args)) (pat (nth args 1)))
(let ((idx (index-of s pat)))
(cond
((< idx 0) nil)
(else pat))))))
(let ((s (first args)) (pat (nth args 1))
(init (if (> (len args) 2) (nth args 2) 1)))
(let ((start-i0 (cond
((< init 0) (let ((v (+ (len s) init))) (if (< v 0) 0 v)))
((= init 0) 0)
(else (- init 1)))))
(let ((r (lua-pat-find pat s start-i0)))
(cond
((= r nil) nil)
(else (substring s (first r) (nth r 1)))))))))
;; Literal-only string.gmatch: iterator producing each literal match of pat.
(define
@@ -983,14 +988,14 @@
(cond
((> pos (len s)) nil)
(else
(let ((rest-str (if (= pos 0) s (substring s pos (len s)))))
(let ((idx (index-of rest-str pat)))
(cond
((< idx 0) (begin (set! pos (+ (len s) 1)) nil))
(else
(let ((r (lua-pat-find pat s pos)))
(cond
((= r nil) (begin (set! pos (+ (len s) 1)) nil))
(else
(let ((start (first r)) (end (nth r 1)))
(begin
(set! pos (+ pos idx (len pat)))
pat)))))))))))
(set! pos (if (= end start) (+ end 1) end))
(substring s start end))))))))))))
;; Literal-only string.gsub: replace all occurrences of pat with repl (string only for now).
(define
@@ -1006,28 +1011,42 @@
(let ((out "") (pos 0) (count 0) (done false))
(begin
(define
loop
gsub-loop
(fn ()
(when (and (not done) (<= pos (len s)))
(let ((rest-str (if (= pos 0) s (substring s pos (len s)))))
(let ((idx (index-of rest-str pat)))
(cond
((< idx 0)
(begin
(set! out (str out rest-str))
(set! done true)))
((and (>= max-n 0) (>= count max-n))
(begin
(set! out (str out rest-str))
(set! done true)))
(else
(let ((before (substring rest-str 0 idx)))
(begin
(set! out (str out before (if (= (type-of repl) "string") repl (str repl))))
(set! pos (+ pos idx (len pat)))
(set! count (+ count 1))
(loop))))))))))
(loop)
(let ((r (lua-pat-find pat s pos)))
(cond
((= r nil)
(begin
(set! out (str out (substring s pos (len s))))
(set! done true)))
((and (>= max-n 0) (>= count max-n))
(begin
(set! out (str out (substring s pos (len s))))
(set! done true)))
(else
(let ((start (first r)) (end (nth r 1)))
(let ((matched (substring s start end)))
(let ((replacement
(cond
((= (type-of repl) "string") repl)
((or (= (type-of repl) "function") (= (type-of repl) "lambda"))
(let ((rv (lua-call repl matched)))
(cond
((or (= rv nil) (= rv false)) matched)
(else (str rv)))))
((= (type-of repl) "dict")
(let ((v (get repl matched)))
(cond
((= v nil) matched)
(else (str v)))))
(else (str repl)))))
(begin
(set! out (str out (substring s pos start) replacement))
(set! pos (if (= end start) (+ end 1) end))
(set! count (+ count 1))
(gsub-loop)))))))))))
(gsub-loop)
(list (quote lua-multi) out count))))))))
;; Basic string.format: %s %d %f (%%.Nf ignored), %%.