Created
April 1, 2020 09:11
-
-
Save sw1nn/49288dd2b378b2ddc66fd5dd078669b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def input ", \t, , , UTF-8;q=0.8,shift_JIS;q=0.4,a,b") | |
(def rfc-7231-re #"(?x) | |
(?: | |
\s* # OWS | |
(?<charset>\*|[\p{Alnum}!\#\$\%\&'\*\+\-\.\^_\`\|\~]+) | |
(?i: | |
;q= | |
(?<weight>0\.\d{1,3}|1\.0{1,3}) | |
)? | |
)+?,?") | |
(defn parse-rfc7231-accept-charset [s] | |
(->> s | |
(re-seq rfc-7231-re) | |
(keep (fn [m] | |
(when-let [[charset weight] (next m)] | |
(vector charset (when weight (Float/parseFloat weight)))))) | |
(into {}))) | |
(comment | |
(parse-rfc7231-accept-charset input) | |
;; {"UTF-8" 0.8, "shift_JIS" 0.4, "a" nil, "b" nil} | |
(criterium.core/quick-bench | |
(parse-rfc7231-accept-charset input))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment