cl-strings icon indicating copy to clipboard operation
cl-strings copied to clipboard

please add replace-first string function

Open maruks opened this issue 6 years ago • 4 comments

(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)))))

maruks avatar May 07 '19 13:05 maruks

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.

arademaker avatar May 07 '19 14:05 arademaker

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.

maruks avatar May 07 '19 14:05 maruks

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.

arademaker avatar May 07 '19 14:05 arademaker

If there is already a function that does string replacement, I think a :count argument would be in line with the rest of CL.

ruricolist avatar May 08 '19 02:05 ruricolist