Skip to content

Commit

Permalink
DEV: Add cljs version of re-seq-matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
or committed Jun 18, 2022
1 parent bedbaba commit be381cd
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,33 @@
(some-> zloc z/up ns-form?)
(-> zloc z/sexpr first ns-reference-symbols)))

(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (re-matcher re s)]
(letfn [(next-match []
(when (.find m)
{:value (subvec v (.start m) (.end m))
:start (.start m)
:end (.end m)}))]
(take-while some? (repeatedly next-match)))))
#?(:cljs
(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (js/RegExp. re.source "g")]
(letfn [(next-match []
(when-let [result (.exec m s)]
(let [start result.index
end m.lastIndex]
{:value (subvec v start end)
:start start
:end end})))]
(take-while some? (repeatedly next-match)))))

:clj
(defn- re-seq-matcher [re charmap coll]
{:pre (every? charmap coll)}
(let [s (apply str (map charmap coll))
v (vec coll)
m (re-matcher re s)]
(letfn [(next-match []
(when (.find m)
{:value (subvec v (.start m) (.end m))
:start (.start m)
:end (.end m)}))]
(take-while some? (repeatedly next-match))))))

(defn- find-elements-with-comments [nodes]
(re-seq-matcher #"(CNS*)*E(S*C)?"
Expand Down

0 comments on commit be381cd

Please sign in to comment.