Toggle case sensitivity for a huge piece of Clojure code

There is a large piece of code (mostly not mine) that performs the following actions with user input (more or less, a space-separated list of commands with some arguments / parameters):

  • Delete all unsupported characters
  • Divide space by vector
  • Recursively apply the first element in the vector to the rest of the vector (the function uses all the arguments it needs and returns the vector without it and its arguments to the loop).

The functions themselves with respect to input have a combination of (case), (cond), (condp), (=) and (cf.) with some funny (key) comparisons mixed in.

Everyone was okay with the fact that all this is very case sensitive until recently. Now some (previously unknown) ancient integration bits acting as users have appeared and have some problems with the case, which I do not control.

Question: Is there a viable way (a shortcut before there is more time to redo all this) to make the case of string comparison insensitive to any area based on some variable?

I considered 3 options:

  • Code fixing (it will be executed someday, at least, but is not viable at the moment).
  • Removing some low-level comparison function (hopefully only one) and re-linking it to the local area (sounds great, but tricks can be difficult and error prone).
  • (, - , , , NEEDS ).

, , ( ), , , , - .

: :

"Command1 ARG1 aRG2 Command3 command9 Arg4 Arg9 aRg5 COMMAND4 arg8"

: "" , , . , , . NB! , .

+4
2
  • , str/lower-case - , .

  • = , cond condp ( ClojureMostly), .

  • , , case, .

0

, case:

(ns lexer.core)

(defn- standardize [thing]
  (assert (string? thing) (str thing " should be a string"))
  (clojure.string/lower-case thing))

(defmacro case-insensitive-case [expr & pairs+default?]
  (let [pairs (partition 2 pairs+default?)
        convert (fn [[const form]]
                  (list (standardize const) form))
        most-of-it `(case (standardize ~expr) ~@(mapcat convert pairs))]
    (if (-> pairs+default? count even?)
      most-of-it
      (concat most-of-it [(last pairs+default?)]))))

,

(macroexpand-1 '(case-insensitive-case (test expression) 
                                       "Blam!" (1 + 1) 
                                       (whatever works)))
=> (clojure.core/case (lexer.core/standardize (test expression)) "blam!" (1 + 1) (whatever works))

assert in standardize , lower-case :

(clojure.string/lower-case 22)
=> "22"
0

Source: https://habr.com/ru/post/1676156/


All Articles