lua: strip capture parens in patterns so (%a+) matches (captures not returned yet)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -828,6 +828,31 @@
|
||||
|
||||
;; Top-level find: return (start-index-0based . end-index) or nil on no match.
|
||||
;; If pat starts with ^, anchor to init. Otherwise scan.
|
||||
(define
|
||||
lua-pat-strip-captures
|
||||
(fn (pat)
|
||||
(let ((out "") (i 0))
|
||||
(begin
|
||||
(define
|
||||
sc-loop
|
||||
(fn ()
|
||||
(when (< i (len pat))
|
||||
(let ((c (char-at pat i)))
|
||||
(cond
|
||||
((= c "%")
|
||||
(begin
|
||||
(set! out (str out c))
|
||||
(when (< (+ i 1) (len pat))
|
||||
(set! out (str out (char-at pat (+ i 1)))))
|
||||
(set! i (+ i 2))
|
||||
(sc-loop)))
|
||||
((or (= c "(") (= c ")"))
|
||||
(begin (set! i (+ i 1)) (sc-loop)))
|
||||
(else
|
||||
(begin (set! out (str out c)) (set! i (+ i 1)) (sc-loop))))))))
|
||||
(sc-loop)
|
||||
out))))
|
||||
|
||||
(define
|
||||
lua-pat-find
|
||||
(fn (pat s init)
|
||||
@@ -959,7 +984,7 @@
|
||||
(+ start-i0 idx 1)
|
||||
(+ start-i0 idx (len pat))))))))
|
||||
(else
|
||||
(let ((r (lua-pat-find pat s start-i0)))
|
||||
(let ((r (lua-pat-find (lua-pat-strip-captures pat) s start-i0)))
|
||||
(cond
|
||||
((= r nil) nil)
|
||||
(else (list (quote lua-multi) (+ (first r) 1) (nth r 1)))))))))))
|
||||
@@ -974,7 +999,7 @@
|
||||
((< 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)))
|
||||
(let ((r (lua-pat-find (lua-pat-strip-captures pat) s start-i0)))
|
||||
(cond
|
||||
((= r nil) nil)
|
||||
(else (substring s (first r) (nth r 1)))))))))
|
||||
@@ -988,7 +1013,7 @@
|
||||
(cond
|
||||
((> pos (len s)) nil)
|
||||
(else
|
||||
(let ((r (lua-pat-find pat s pos)))
|
||||
(let ((r (lua-pat-find (lua-pat-strip-captures pat) s pos)))
|
||||
(cond
|
||||
((= r nil) (begin (set! pos (+ (len s) 1)) nil))
|
||||
(else
|
||||
@@ -1014,7 +1039,7 @@
|
||||
gsub-loop
|
||||
(fn ()
|
||||
(when (and (not done) (<= pos (len s)))
|
||||
(let ((r (lua-pat-find pat s pos)))
|
||||
(let ((r (lua-pat-find (lua-pat-strip-captures pat) s pos)))
|
||||
(cond
|
||||
((= r nil)
|
||||
(begin
|
||||
|
||||
Reference in New Issue
Block a user