Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added all-scans-as-strings #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,49 @@ compile time."
,@rest))
(t form)))

(defun all-scans-as-strings (regex string &key start end)
"Returns two values: 1. A list of all matches, 2. A list of lists for
all of the group matches."
(let ((start (or start 0))
(end (or end (length string)))
(mresult NIL)
(mlast NIL)
(sresult NIL)
(slast NIL))
(labels ((macc (v)
(if (null mresult)
(setf mresult (cons v nil)
mlast mresult)
(setf (cdr mlast) (cons v nil)
mlast (cdr mlast))))
(sacc (v)
(if (null sresult)
(setf sresult (cons v nil)
slast sresult)
(setf (cdr slast) (cons v nil)
slast (cdr slast)))))
(apply #'values
(ppcre:do-scans (ms me
rs re
regex string
(list mresult sresult)
:start start
:end end)
(macc (subseq string ms me))
(sacc (loop for s across rs
for e across re
collecting (subseq string s e))))))))

#-:cormanlisp
(define-compiler-macro all-scans-as-strings (&whole form regex &rest rest)
"Make sure that constant forms are compiled into scanners at
compile time."
(cond ((constantp regex)
`(all-scans-as-strings
(load-time-value (create-scanner ,regex))
,@rest))
(t form)))

(defun split (regex target-string
&key (start 0)
(end (length target-string))
Expand Down
1 change: 1 addition & 0 deletions packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
:count-matches
:all-matches
:all-matches-as-strings
:all-scans-as-strings
:split
:regex-replace
:regex-replace-all
Expand Down