packages feed

css-parser-0.0.2 / src / CssParser / TextMarshal.hs

module CssParser.TextMarshal  ( module CssParser.TextMarshal  , readEither  ) whereimport CssParser.Preludeimport Data.Text.Lazy qualified as Limport Data.Text.Lazy.Builder (toLazyText)import Data.Text.Lazy.Builder.Int (decimal)import Text.Read (readEither)numToText :: Integral a => a -> LTextnumToText = toLazyText . decimalshowHex :: Int -> ShowSshowHex = go (6 :: Int)  where    go 0 _ s = s    go k n rs = go (k - 1) q (intToDigit r : rs)      where        ~(q, r) = quotRem n 16encodeStringChar :: Char -> LTextencodeStringChar c  | c `notElem` ("\"\\\n\t\r\f" :: String) = L.singleton c  | otherwise = cons '\\' (L.pack (showHex (ord c) ""))encodeStringLiteral :: Text -> LTextencodeStringLiteral t =  cons c (snoc (L.concatMap encodeStringChar $ fromStrict t) c)  where    c = '"'readHex :: String -> Either String IntegerreadHex = \case  [a, b, c] -> readEither ['0', 'x', a, a, b, b, c, c]  o -> readEither $ '0':'x':oreadUnquotedUrl :: String -> StringreadUnquotedUrl = dropEnd 1 . drop 4