diogoalexandrefranco / cl-strings

A portable, dependency-free set of utilities to manipulate strings in Common Lisp
MIT License
46 stars 7 forks source link

please add replace-first string function #9

Open maruks opened 5 years ago

maruks commented 5 years ago

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

arademaker commented 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.

maruks commented 5 years ago

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.

arademaker commented 5 years ago

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.

ruricolist commented 5 years ago

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