Skip to content

Instantly share code, notes, and snippets.

@sw1nn
Created April 1, 2020 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sw1nn/49288dd2b378b2ddc66fd5dd078669b7 to your computer and use it in GitHub Desktop.
Save sw1nn/49288dd2b378b2ddc66fd5dd078669b7 to your computer and use it in GitHub Desktop.
(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