on master help sign in
Order by specificity for items with the same q

Closes #118

commitecd53e3febedb5c6d911a56886e80d111aafceef authorStephen Paul Weber <singpolyma@singpolyma.net> dateTue 9 Oct 2012, 18:36 (14 years ago) parent51b81d3
1 file changed+7−2
wai-extra/Network/Wai/Parse.hs+7−2
wai-extra/Network/Wai/Parse.hs+7−2
@@ -58,11 +58,16 @@ breakDiscard w s =
5858parseHttpAccept :: S.ByteString -> [S.ByteString]
5959parseHttpAccept = map fst
6060 . sortBy (rcompare `on` snd)
61− . map grabQ
61+ . map (addSpecificity . grabQ)
6262 . S.split 44 -- comma
6363 where
64− rcompare :: Double -> Double -> Ordering
64+ rcompare :: (Double,Int) -> (Double,Int) -> Ordering
6565 rcompare = flip compare
66+ addSpecificity (s, q) =
67+ -- Prefer higher-specificity types
68+ let semicolons = S.count 0x3B s
69+ stars = S.count 0x2A s
70+ in (s, (q, semicolons - stars))
6671 grabQ s =
6772 -- Stripping all spaces may be too harsh.
6873 -- Maybe just strip either side of semicolon?