+-- |>>> quoteStr "abc"
+-- "\"abc\""
+--
+-- >>> quoteStr "ab\"c"
+-- "\"ab\\\"c\""
+quoteStr ∷ Ascii → AsciiBuilder
+quoteStr str = cs ("\"" ∷ Ascii) ⊕
+ go (cs str) (∅) ⊕
+ cs ("\"" ∷ Ascii)
+ where
+ go ∷ ByteString → AsciiBuilder → AsciiBuilder
+ go bs ab
+ = case BS.break (≡ '"') bs of
+ (x, y)
+ | BS.null y
+ → ab ⊕ b2ab x
+ | otherwise
+ → go (BS.tail y)
+ (ab ⊕ b2ab x ⊕ cs ("\\\"" ∷ Ascii))
+
+ b2ab ∷ ByteString → AsciiBuilder
+ b2ab = cs ∘ A.unsafeFromByteString
+
+-- |>>> parseWWWFormURLEncoded "aaa=bbb&ccc=ddd"
+-- [("aaa", "bbb"), ("ccc", "ddd")]
+parseWWWFormURLEncoded ∷ Ascii → [(ByteString, ByteString)]
+parseWWWFormURLEncoded src
+ -- THINKME: We could gain some performance by using attoparsec
+ -- here.
+ | src ≡ "" = []
+ | otherwise = do pairStr ← splitBy (\ c → c ≡ ';' ∨ c ≡ '&') (cs src)
+ let (key, value) = break (≡ '=') pairStr
+ return ( unescape key
+ , unescape $ case value of
+ ('=':val) → val
+ val → val
+ )
+ where
+ unescape ∷ String → ByteString
+ unescape = BS.pack ∘ unEscapeString ∘ (plusToSpace <$>)
+
+ plusToSpace ∷ Char → Char
+ plusToSpace '+' = ' '
+ plusToSpace c = c