Replace clojure.string/split with a custom splitter
This commit is contained in:
parent
045634fd3c
commit
5fa85400f0
3 changed files with 24 additions and 3 deletions
|
|
@ -31,7 +31,7 @@
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
#?(:clj [clojure.template])
|
#?(:clj [clojure.template])
|
||||||
[honey.sql.protocols :as p]
|
[honey.sql.protocols :as p]
|
||||||
[honey.sql.util :refer [str join]]))
|
[honey.sql.util :refer [str join split-by-separator]]))
|
||||||
|
|
||||||
;; default formatting for known clauses
|
;; default formatting for known clauses
|
||||||
|
|
||||||
|
|
@ -316,7 +316,7 @@
|
||||||
[n %]
|
[n %]
|
||||||
(if aliased
|
(if aliased
|
||||||
[%]
|
[%]
|
||||||
(str/split % #"\."))))
|
(split-by-separator % "."))))
|
||||||
parts (parts-fn col-e)
|
parts (parts-fn col-e)
|
||||||
entity (join "." (map #(cond-> % (not= "*" %) (quote-fn))) parts)]
|
entity (join "." (map #(cond-> % (not= "*" %) (quote-fn))) parts)]
|
||||||
(suspicious-entity-check entity)
|
(suspicious-entity-check entity)
|
||||||
|
|
@ -457,7 +457,7 @@
|
||||||
:default (subs (str x) 1))
|
:default (subs (str x) 1))
|
||||||
(str x))]
|
(str x))]
|
||||||
(cond (str/starts-with? c "%")
|
(cond (str/starts-with? c "%")
|
||||||
(let [[f & args] (str/split (subs c 1) #"\.")]
|
(let [[f & args] (split-by-separator (subs c 1) ".")]
|
||||||
[(str (format-fn-name f) "("
|
[(str (format-fn-name f) "("
|
||||||
(join ", " (map #(format-entity (keyword %) opts)) args)
|
(join ", " (map #(format-entity (keyword %) opts)) args)
|
||||||
")")])
|
")")])
|
||||||
|
|
|
||||||
|
|
@ -75,3 +75,16 @@
|
||||||
|
|
||||||
:default
|
:default
|
||||||
(clojure.string/join separator (transduce xform conj [] coll)))))
|
(clojure.string/join separator (transduce xform conj [] coll)))))
|
||||||
|
|
||||||
|
(defn split-by-separator
|
||||||
|
"More efficient implementation of `clojure.string/split` for cases when a
|
||||||
|
literal string (not regex) is used as a separator, and for cases where the
|
||||||
|
separator is not present in the haystack at all."
|
||||||
|
[s sep]
|
||||||
|
(loop [start 0, res []]
|
||||||
|
(if-let [sep-idx (clojure.string/index-of s sep start)]
|
||||||
|
(recur (inc sep-idx) (conj res (subs s start sep-idx)))
|
||||||
|
(if (= start 0)
|
||||||
|
;; Fastpath - zero separators in s
|
||||||
|
[s]
|
||||||
|
(conj res (subs s start))))))
|
||||||
|
|
|
||||||
|
|
@ -43,3 +43,11 @@
|
||||||
(is (= "1, 2, 3, 4"
|
(is (= "1, 2, 3, 4"
|
||||||
(sut/join ", " (remove nil?) [1 nil 2 nil 3 nil nil nil 4])))
|
(sut/join ", " (remove nil?) [1 nil 2 nil 3 nil nil nil 4])))
|
||||||
(is (= "" (sut/join ", " (remove nil?) [nil nil nil nil]))))
|
(is (= "" (sut/join ", " (remove nil?) [nil nil nil nil]))))
|
||||||
|
|
||||||
|
(deftest split-by-separator-test
|
||||||
|
(is (= [""] (sut/split-by-separator "" ".")))
|
||||||
|
(is (= ["" ""] (sut/split-by-separator "." ".")))
|
||||||
|
(is (= ["hello"] (sut/split-by-separator "hello" ".")))
|
||||||
|
(is (= ["h" "e" "l" "l" "o"] (sut/split-by-separator "h.e.l.l.o" ".")))
|
||||||
|
(is (= ["" "h" "e" "" "" "l" "" "l" "o" ""]
|
||||||
|
(sut/split-by-separator ".h.e...l..l.o." "."))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue