Skip to content

Commit 7c1b8b5

Browse files
authored
Merge pull request #38 from bsless/faster-form-decode-str
Add short-circuit check to form-decode-str
2 parents 2b98c7c + 84bb670 commit 7c1b8b5

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/ring/util/codec.clj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,21 @@
125125
([x encoding]
126126
(form-encode* x encoding)))
127127

128+
(defn- form-encoded-chars? [^String s]
129+
(or (.contains s "+") (.contains s "%")))
130+
128131
(defn form-decode-str
129132
"Decode the supplied www-form-urlencoded string using the specified encoding,
130133
or UTF-8 by default."
131134
([encoded]
132135
(form-decode-str encoded "UTF-8"))
133-
([^String encoded ^String encoding]
134-
(try
135-
(URLDecoder/decode encoded ^String (or encoding "UTF-8"))
136-
(catch Exception _ nil))))
136+
([^String encoded encoding]
137+
(if (form-encoded-chars? encoded)
138+
(try
139+
(let [^String encoding (or encoding "UTF-8")]
140+
(URLDecoder/decode encoded encoding))
141+
(catch Exception _ nil))
142+
encoded)))
137143

138144
(defn- tokenized [s delim]
139145
(reify clojure.lang.IReduceInit

0 commit comments

Comments
 (0)