From e58a3305b0b839b710781676f4730c4ecef3b926 Mon Sep 17 00:00:00 2001 From: Joe Littlejohn Date: Fri, 16 Nov 2018 15:29:59 +0000 Subject: [PATCH] Avoid matching boundaries when pattern is empty When options include {whole-words? true} this change fixes an edge-case when the strs are empty. The pattern should match nothing. --- src/cljc/frak.cljc | 6 +++--- test/frak_test.clj | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cljc/frak.cljc b/src/cljc/frak.cljc index 1c23302..1b83b11 100644 --- a/src/cljc/frak.cljc +++ b/src/cljc/frak.cljc @@ -122,7 +122,7 @@ (re-char-set chars false)) ([chars optional?] (when-let [chars (and (seq chars) (map escape chars))] - (str + (str (if (= 1 (count chars)) (first chars) (str \[ (apply str chars) \])) @@ -167,7 +167,7 @@ (let [groups (-> (juxt :terminal? :children) (group-by children) (dissoc nil)) - subpatterns + subpatterns (mapv (fn [[_ v]] (let [chars (map :char v) @@ -227,7 +227,7 @@ remove-unecessary-grouping))] (if (get* opts :exact?) (str "^" pattern "$") - (if (get* opts :whole-words?) + (if (and (get* opts :whole-words?) (seq strs)) (str "\\b" pattern) pattern))))) diff --git a/test/frak_test.clj b/test/frak_test.clj index 5b43306..4f5138a 100644 --- a/test/frak_test.clj +++ b/test/frak_test.clj @@ -56,8 +56,8 @@ #"ba\[[trz]{3}\]" (string-pattern ["bat" "bar" "baz"] nil)))) - (is (= "b(?:i[pt]|at)" - (string-pattern ["bat" "bip" "bit"] nil))) + (let [p (string-pattern ["bat" "bip" "bit"] nil)] + (is (or (= "b(?:at|i[tp])" p) (= "b(?:i[pt]|at)" p)) p)) (is (= "foo\\??" (string-pattern ["foo" "foo?"]))) @@ -73,6 +73,10 @@ ["aching" "achingly"])) (deftest pattern-whole-words + (is (= "" + (string-pattern [] {:whole-words? false}) + (string-pattern [] {:whole-words? true}))) + (is (= ["k pop"] (re-seq (pattern ["pop" "k pop"]) "uk pop"))) -- 2.25.1