vindarel / cl-str

Modern, simple and consistent Common Lisp string manipulation library.
https://vindarel.github.io/cl-str/
MIT License
305 stars 37 forks source link

casing functions inconsistencies #91

Closed kilianmh closed 1 year ago

kilianmh commented 1 year ago

The cl-str functions upcase, downcase, capitalize do behave different than the casing functions from cl-change-case. The functions from cl-change-case do NOT:

We could fix it like this:

(defun no-case (s)
  "Return the no-cased version of `s'.
  Calls cl-change-case:no-case and converts a symbol to name if needed.
  Returns nil if `s' is nil."
  (typecase s
    (null
     nil)
    (string
     (cl-change-case:no-case s))
    (symbol
     (cl-change-case:no-case (symbol-name s)))
    (otherwise
     (error 'simple-type-error))))

That way users can choose themselve cl-change-case which soon only accepts strings or cl-str which also accepts nil and other symbols (like the inbuilt casing functions).

vindarel commented 1 year ago

to be clear, the inconsistency is for the functions we import and re-export from cl-change-case:

  #:no-case
  #:camel-case
  #:dot-case
  #:header-case
  #:param-case
  #:pascal-case
  #:path-case
  #:sentence-case
  #:snake-case
  #:swap-case
  #:title-case
  #:constant-case

STR's upcase, downcase, capitalize call the built-in string-, and fix their weird behavior to return the string "NIL" with nil. Otherwise they keep CL's behavior.

So +1 for the fix.

kilianmh commented 1 year ago

Here is a simpler imprementation using cl:string to coerce s into a string

(defun no-case (s)
  "Return the no-cased version of `s'.
  Calls `cl-change-case:no-case' after coercing `s' into a string,
  but returns nil if `s' is nil."
  (when s
    (cl-change-case:no-case (string s))))

Then the behavior (and look) of all casing functions would be (mostly) consistent:

If you like it this way (also the docstring), I will create the PR accordingly.

vindarel commented 1 year ago

+1 LGTM