diff --git a/src/stencil/merger.clj b/src/stencil/merger.clj index 68e2baa4..4d7fd6ad 100644 --- a/src/stencil/merger.clj +++ b/src/stencil/merger.clj @@ -35,35 +35,28 @@ (defn- ->action-inside-parser [chars-and-tokens-to-append] (let [expected-close-tag-chars (volatile! (seq close-tag)) buffer-nonclose-chars-only (new java.util.ArrayList) - buffer-all-chars (new java.util.ArrayList) - buffer-tokens-only (new java.util.ArrayList) buffer-all-read (new java.util.ArrayList)] (fn self ([] - (when (or (seq buffer-all-chars) (seq buffer-nonclose-chars-only) (seq buffer-tokens-only)) + (when (seq buffer-all-read) (assert false "Not-empty buffer!")) ) ;; TODO: throw exception if any of the buffers is not empty. ([token] (.add buffer-all-read token) (if (= token (first @expected-close-tag-chars)) - (if (= 1 (count @expected-close-tag-chars)) + (when-not (vswap! expected-close-tag-chars next) ;; we have read the whole close token (let [action (map-action-token {:action (apply str buffer-nonclose-chars-only)})] (if (:action action) - (->action-parser (concat [action] (remove char? chars-and-tokens-to-append) (vec buffer-tokens-only))) - (->action-parser (concat (vec chars-and-tokens-to-append) (vec buffer-all-read))))) - ;; we have read one char of the close - (do (vswap! expected-close-tag-chars next) - (.add buffer-all-chars token) - self)) - (if (char? token) - (do (.add buffer-all-chars token) - (doto buffer-nonclose-chars-only - (.clear) (.addAll buffer-all-chars)) - (vreset! expected-close-tag-chars (seq close-tag)) - self) - (do (.add buffer-tokens-only token) - self))))))) + (->action-parser (concat [action] + (remove char? chars-and-tokens-to-append) + (remove char? buffer-all-read))) + (->action-parser (concat (vec chars-and-tokens-to-append) (vec buffer-all-read)))))) + (when (char? token) + (doto buffer-nonclose-chars-only + (.clear) (.addAll (filter char? buffer-all-read))) + (vreset! expected-close-tag-chars (seq close-tag)) + self)))))) ;; returns either a collection of elements or nil (defn ->action-parser [prepend] diff --git a/src/stencil/util.clj b/src/stencil/util.clj index 4d5634d9..8ea25404 100644 --- a/src/stencil/util.clj +++ b/src/stencil/util.clj @@ -52,9 +52,6 @@ (defn assoc-if-val [m k v] (if (some? v) (assoc m k v) m)) -(defn suffixes [xs] (take-while seq (iterate next xs))) -(defn prefixes [xs] (take-while seq (iterate butlast xs))) - (defmacro fail [msg obj] (assert (string? msg)) (assert (map? obj)) @@ -66,8 +63,6 @@ (number? x) (int x) :else (fail "Unexpected type of input" {:type (:type x) :input x}))) -(defn subs-last [^String s ^long n] (.substring s (- (.length s) n))) - (defn parsing-exception [expression message] (ParsingException/fromMessage (str expression) (str message))) diff --git a/test/stencil/util_test.clj b/test/stencil/util_test.clj index 853b59af..c95d910a 100644 --- a/test/stencil/util_test.clj +++ b/test/stencil/util_test.clj @@ -55,18 +55,6 @@ (testing "Difference clojure core" (is (not (zip/branch? (xml-zip 42)))))))) -(deftest test-suffixes - (is (= [] (suffixes nil))) - (is (= [] (suffixes []))) - (is (= [[1]] (suffixes [1]))) - (is (= [[1 2 3] [2 3] [3]] (suffixes [1 2 3])))) - -(deftest test-prefixes - (is (= [] (prefixes nil))) - (is (= [] (prefixes []))) - (is (= [[1]] (prefixes [1]))) - (is (= [[1 2 3] [1 2] [1]] (prefixes [1 2 3])))) - (deftest test-->int (is (= nil (->int nil))) (is (= 23 (->int 23))) @@ -97,14 +85,6 @@ (deftest fail-test (is (thrown? clojure.lang.ExceptionInfo (fail "test error" {})))) -(deftest prefixes-test - (is (= [] (prefixes []) (prefixes nil))) - (is (= [[1 2 3] [1 2] [1]] (prefixes [1 2 3])))) - -(deftest suffixes-test - (is (= [] (suffixes []) (suffixes nil))) - (is (= [[1 2 3] [2 3] [3]] (suffixes [1 2 3])))) - (deftest whitespace?-test (is (= true (whitespace? \space))) (is (= true (whitespace? \tab)))