mirror of
https://github.com/metosin/reitit.git
synced 2025-12-20 09:31:11 +00:00
double fast byte formatting
This commit is contained in:
parent
7389838b59
commit
230717ba65
2 changed files with 5 additions and 21 deletions
|
|
@ -162,27 +162,13 @@
|
|||
(defn strip-nils [m]
|
||||
(->> m (remove (comp nil? second)) (into {})))
|
||||
|
||||
;;
|
||||
;; Parts (c) https://github.com/lambdaisland/uri/tree/master/src/lambdaisland/uri
|
||||
;;
|
||||
#?(:clj (def +percents+ (into [] (map #(format "%%%02X" %) (range 0 256)))))
|
||||
|
||||
#?(:clj
|
||||
(def hex-digit
|
||||
{0 "0" 1 "1" 2 "2" 3 "3"
|
||||
4 "4" 5 "5" 6 "6" 7 "7"
|
||||
8 "8" 9 "9" 10 "A" 11 "B"
|
||||
12 "C" 13 "D" 14 "E" 15 "F"}))
|
||||
#?(:clj (defn byte->percent [byte]
|
||||
(nth +percents+ (if (< byte 0) (+ 256 byte) byte))))
|
||||
|
||||
#?(:clj
|
||||
(defn byte->percent [byte]
|
||||
(let [byte (bit-and 0xFF byte)
|
||||
low-nibble (bit-and 0xF byte)
|
||||
high-nibble (bit-shift-right byte 4)]
|
||||
(str "%" (hex-digit high-nibble) (hex-digit low-nibble)))))
|
||||
|
||||
#?(:clj
|
||||
(defn percent-encode [^String unencoded]
|
||||
(->> (.getBytes unencoded "UTF-8") (map byte->percent) (str/join))))
|
||||
#?(:clj (defn percent-encode [^String s]
|
||||
(->> (.getBytes s "UTF-8") (map byte->percent) (str/join))))
|
||||
|
||||
;;
|
||||
;; encoding & decoding
|
||||
|
|
|
|||
|
|
@ -168,5 +168,3 @@
|
|||
(url-encode!)
|
||||
(form-decode!)
|
||||
(form-encode!))
|
||||
|
||||
(ring.util.codec/form-decode-str "%2B632+905+123+4567")
|
||||
|
|
|
|||
Loading…
Reference in a new issue