Created
March 29, 2020 18:01
-
-
Save malcolmsparks/6a9aab2465d2d7ea841cb9187a6d17fd to your computer and use it in GitHub Desktop.
Parsing the RFC 7231 Accept-Charset header with reap
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
;; Accept-Charset = *( "," OWS ) ( ( charset / "*" ) [ weight ] ) *( OWS | |
;; "," [ OWS ( ( charset / "*" ) [ weight ] ) ] ) | |
(let [parser | |
(let [charset-with-weight | |
(p/sequence-group | |
(p/alternatives | |
(p/pattern-parser | |
(re-pattern charset)) | |
(p/pattern-parser | |
(re-pattern (re/re-str \*)))) | |
(p/optionally (weight)))] | |
(p/cons | |
(p/first | |
(p/sequence-group | |
(p/ignore | |
(p/pattern-parser | |
(re-pattern (re/re-compose "(?:%s)*" (re/re-concat \, OWS))))) | |
charset-with-weight)) | |
(p/zero-or-more | |
(p/first | |
(p/sequence-group | |
(p/ignore | |
(p/pattern-parser | |
(re-pattern (re/re-compose "%s%s" OWS ",")))) | |
(p/optionally | |
(p/first | |
(p/sequence-group | |
(p/ignore | |
(p/pattern-parser | |
(re-pattern OWS))) | |
charset-with-weight))))))))] | |
(criterium.core/quick-bench | |
(let [matcher (re/input ", \t, , , UTF-8;q=0.8,shift_JIS;q=0.4,a,b")] | |
(parser matcher)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the latest Accept header