Have replace-* work with characters. Add a compiler macro for this.#97
Have replace-* work with characters. Add a compiler macro for this.#97K1D77A wants to merge 1 commit intovindarel:masterfrom
Conversation
Add 3 aliases for the replace-* called substitute-* because cl-user:replace is destructive.
|
I was also going to ask why such heavy use of ppcre is used when many functions could be replaced with plain CL? |
|
Hi @K1D77A, thank you for your pull request! I appreciate the suggestions of setting alias functions and adding support for characters in the Here are some comments:
|
(define-compiler-macro substitute-first (&whole form &rest args)
(declare (ignore form))
(destructuring-bind (old new string)
args
(if (and (constantp old)
(characterp old)
(constantp new)
(characterp new))
`(%substitute-first-character ,old ,new ,string)
`(%substitue-first-string ,old ,new ,string))))
(defun %substitute-first-character (old-char new-char string)
(substitute new-char old-char string :test #'char= :count 1))
(defun substitute-first (old new s)
"Substitute the first occurrence of `old` by `new` in `s`. Arguments are not regexs."
(if (and (characterp old)
(characterp new))
(%substitute-first-character old new s)
(%substitute-first-string (string old) (string new) s)))
(defun %substitute-first-string (old new s)
(let* ((ppcre:*allow-quoting* t)
(old (concatenate 'string "\\Q" old))) ;; treat metacharacters as normal.
;; We need the (list new): see !52
(ppcre:regex-replace old s (list new))))Does this still cause infinite inlining for you? |
good catch, but is it really an issue though? The The goal of cl-str was not to make a better standard. It is much simpler to coerce Thanks for sending a PR (and for help in the review). |
|
From
(defun regex-match (regex target)
;; don't do that!
(scan regex target))Accordingly, the |
Add 3 aliases for the replace-* called substitute-* because cl-user:replace is destructive.