2010/11/23

最初だけ "" で後は "/" のシリーズを作る

Common Lisp の SERIES にある latch の使い道が分からなかったけど、初めて使い道を見付けた。

(subseries (latch (series "") :after 1 :post "/") 0 5)

本当はこんな関数を書くのに使った。

(defun path-to-regexp (path)
(let (bindings)
(values
(with-output-to-string (out)
(iterate ((x (scan (ppcre:split "/" path)))
(y (latch (series #'values) :after 1 :post (^ write-string "/" out))))
(funcall y)
(if (q:string-start-p x ":")
(let ((var (subseq x 1)))
(write-string "([^/]+)" out)
(push (intern (string-upcase var)) bindings))
(write-string (ppcre:quote-meta-chars x) out))))
(nreverse bindings))))
;; (path-to-regexp "a/:a-id/b/:id")
;; => "a/([^/]+)/b/([^/]+)"
;; (A-ID ID)

0 件のコメント: