Open maruks opened 5 years ago
What is the argument for having such a function in the library? I mean, I am not the author of the library but I am just curious about how to handle the possible infinity many interesting functions that could be added to the library.
This is basic functionality which is often included in base library. For example, java , python and many other languages have replace_first. Lisp devs are missing out.
Authors of cl-strings could also add :max-replace parameter to replace-all.
Good argument, in that case, we can go further and enumerate all missing functions in the current version regarding any other standard library available for other languages.
If there is already a function that does string replacement, I think a :count
argument would be in line with the rest of CL.
(defun replace-first (string part replacement &key (test #'char=)) "Returns a new string in which the first occurence of the part is replaced with replacement." (with-output-to-string (out) (let ((part-length (length part)) (pos (search part string :test test))) (if pos (progn (write-string string out :end pos) (write-string replacement out) (write-string string out :start (+ pos part-length))) (write-string string out)))))