Skip to content

Commit 34e3329

Browse files
committed
Improve arity handling
1 parent 252f7fe commit 34e3329

1 file changed

Lines changed: 46 additions & 32 deletions

File tree

src/ring/util/codec.clj

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
(defn percent-encode
3232
"Percent-encode every character in the given string using either the specified
3333
encoding, or UTF-8 by default."
34-
[^String unencoded & [^String encoding]]
35-
(->> (.getBytes unencoded (or encoding "UTF-8"))
36-
(map (partial format "%%%02X"))
37-
(str/join)))
34+
([unencoded]
35+
(percent-encode unencoded "UTF-8"))
36+
([^String unencoded ^String encoding]
37+
(->> (.getBytes unencoded encoding)
38+
(map (partial format "%%%02X"))
39+
(str/join))))
3840

3941
(defn- parse-bytes [encoded-bytes]
4042
(->> (re-seq #"%[A-Za-z0-9]{2}" encoded-bytes)
@@ -45,28 +47,34 @@
4547
(defn percent-decode
4648
"Decode every percent-encoded character in the given string using the
4749
specified encoding, or UTF-8 by default."
48-
[^String encoded & [^String encoding]]
49-
(str/replace encoded
50-
#"(?:%[A-Za-z0-9]{2})+"
51-
(fn [chars]
52-
(-> ^bytes (parse-bytes chars)
53-
(String. (or encoding "UTF-8"))
54-
(fix-string-replace-bug)))))
50+
([encoded]
51+
(percent-decode encoded "UTF-8"))
52+
([^String encoded ^String encoding]
53+
(str/replace encoded
54+
#"(?:%[A-Za-z0-9]{2})+"
55+
(fn [chars]
56+
(-> ^bytes (parse-bytes chars)
57+
(String. encoding)
58+
(fix-string-replace-bug))))))
5559

5660
(defn url-encode
5761
"Returns the url-encoded version of the given string, using either a specified
5862
encoding or UTF-8 by default."
59-
[unencoded & [encoding]]
60-
(str/replace
63+
([unencoded]
64+
(url-encode unencoded "UTF-8"))
65+
([unencoded encoding]
66+
(str/replace
6167
unencoded
6268
#"[^A-Za-z0-9_~.+-]+"
63-
#(double-escape (percent-encode % encoding))))
69+
#(double-escape (percent-encode % encoding)))))
6470

6571
(defn ^String url-decode
6672
"Returns the url-decoded version of the given string, using either a specified
6773
encoding or UTF-8 by default. If the encoding is invalid, nil is returned."
68-
[encoded & [encoding]]
69-
(percent-decode encoded encoding))
74+
([encoded]
75+
(url-decode encoded "UTF-8"))
76+
([encoded encoding]
77+
(percent-decode encoded encoding)))
7078

7179
(defn base64-encode
7280
"Encode an array of bytes into a base64 encoded string."
@@ -106,28 +114,34 @@
106114
"Encode the supplied value into www-form-urlencoded format, often used in
107115
URL query strings and POST request bodies, using the specified encoding.
108116
If the encoding is not specified, it defaults to UTF-8"
109-
[x & [encoding]]
110-
(form-encode* x (or encoding "UTF-8")))
117+
([x]
118+
(form-encode x "UTF-8"))
119+
([x encoding]
120+
(form-encode* x encoding)))
111121

112122
(defn form-decode-str
113123
"Decode the supplied www-form-urlencoded string using the specified encoding,
114124
or UTF-8 by default."
115-
[^String encoded & [encoding]]
116-
(try
117-
(URLDecoder/decode encoded (or encoding "UTF-8"))
118-
(catch Exception _ nil)))
125+
([encoded]
126+
(form-decode-str encoded "UTF-8"))
127+
([^String encoded encoding]
128+
(try
129+
(URLDecoder/decode encoded encoding)
130+
(catch Exception _ nil))))
119131

120132
(defn form-decode
121133
"Decode the supplied www-form-urlencoded string using the specified encoding,
122134
or UTF-8 by default. If the encoded value is a string, a string is returned.
123135
If the encoded value is a map of parameters, a map is returned."
124-
[^String encoded & [encoding]]
125-
(if-not (.contains encoded "=")
126-
(form-decode-str encoded encoding)
127-
(reduce
128-
(fn [m param]
129-
(if-let [[k v] (str/split param #"=" 2)]
130-
(assoc-conj m (form-decode-str k encoding) (form-decode-str v encoding))
131-
m))
132-
{}
133-
(str/split encoded #"&"))))
136+
([encoded]
137+
(form-decode encoded "UTF-8"))
138+
([^String encoded encoding]
139+
(if-not (.contains encoded "=")
140+
(form-decode-str encoded encoding)
141+
(reduce
142+
(fn [m param]
143+
(if-let [[k v] (str/split param #"=" 2)]
144+
(assoc-conj m (form-decode-str k encoding) (form-decode-str v encoding))
145+
m))
146+
{}
147+
(str/split encoded #"&")))))

0 commit comments

Comments
 (0)