double fast byte formatting

This commit is contained in:
Tommi Reiman 2018-08-02 16:06:34 +03:00
parent 7389838b59
commit 230717ba65
2 changed files with 5 additions and 21 deletions

View file

@ -162,27 +162,13 @@
(defn strip-nils [m] (defn strip-nils [m]
(->> m (remove (comp nil? second)) (into {}))) (->> m (remove (comp nil? second)) (into {})))
;; #?(:clj (def +percents+ (into [] (map #(format "%%%02X" %) (range 0 256)))))
;; Parts (c) https://github.com/lambdaisland/uri/tree/master/src/lambdaisland/uri
;;
#?(:clj #?(:clj (defn byte->percent [byte]
(def hex-digit (nth +percents+ (if (< byte 0) (+ 256 byte) byte))))
{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 #?(:clj (defn percent-encode [^String s]
(defn byte->percent [byte] (->> (.getBytes s "UTF-8") (map byte->percent) (str/join))))
(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))))
;; ;;
;; encoding & decoding ;; encoding & decoding

View file

@ -168,5 +168,3 @@
(url-encode!) (url-encode!)
(form-decode!) (form-decode!)
(form-encode!)) (form-encode!))
(ring.util.codec/form-decode-str "%2B632+905+123+4567")