+import Data.List hiding (last)
+import Data.Map (Map)
+import qualified Data.Map as M
+import Data.Monoid.Unicode
+import Data.Ratio
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Time
+import Language.Haskell.TH.Lib
+import Language.Haskell.TH.Syntax
+import Network.URI
+import Prelude hiding (last)
+import Prelude.Unicode
+import System.IO.Unsafe
+
+-- |>>> splitBy (== ':') "ab:c:def"
+-- ["ab", "c", "def"]
+splitBy ∷ (a → Bool) → [a] → [[a]]
+{-# INLINEABLE splitBy #-}
+splitBy isSep src
+ = case break isSep src of
+ (last , [] ) → [last]
+ (first, _sep:rest) → first : splitBy isSep rest
+
+-- |>>> quoteStr "abc"
+-- "\"abc\""
+--
+-- >>> quoteStr "ab\"c"
+-- "\"ab\\\"c\""
+quoteStr ∷ Ascii → AsciiBuilder
+quoteStr str = A.toAsciiBuilder "\"" ⊕
+ go (A.toByteString str) (∅) ⊕
+ A.toAsciiBuilder "\""
+ where
+ go ∷ Strict.ByteString → AsciiBuilder → AsciiBuilder
+ go bs ab
+ = case Strict.break (≡ '"') bs of
+ (x, y)
+ | Strict.null y
+ → ab ⊕ b2ab x
+ | otherwise
+ → go (Strict.tail y)
+ (ab ⊕ b2ab x ⊕ A.toAsciiBuilder "\\\"")
+
+ b2ab ∷ Strict.ByteString → AsciiBuilder
+ b2ab = A.toAsciiBuilder ∘ 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 ≡ '&') (A.toString src)
+ let (key, value) = break (≡ '=') pairStr
+ return ( unescape key
+ , unescape $ case value of
+ ('=':val) → val
+ val → val
+ )
+ where
+ unescape ∷ String → ByteString
+ unescape = Strict.pack ∘ unEscapeString ∘ map plusToSpace
+
+ plusToSpace ∷ Char → Char
+ plusToSpace '+' = ' '
+ plusToSpace c = c