    module Data.String.Interpolate.Internal.Util where

    import           Data.Char
    import           Data.Maybe
    import qualified Numeric as N

    import           Data.String.Interpolate.Compat

    toString :: Show a => a -> String
    toString :: forall a. Show a => a -> String
    toString a
    a = let s :: String
    s = a -> String
    forall a. Show a => a -> String
    show a
    a in String -> Maybe String -> String
    forall a. a -> Maybe a -> a
    fromMaybe String
    s (String -> Maybe String
    forall a. Read a => String -> Maybe a
    readMaybe String
    s)
    {-# NOINLINE toString #-}
    {-# RULES "toString/String" toString = id #-}
    {-# RULES "toString/Int" toString = show :: Int -> String #-}
    {-# RULES "toString/Integer" toString = show :: Integer -> String #-}
    {-# RULES "toString/Float" toString = show :: Float -> String #-}
    {-# RULES "toString/Double" toString = show :: Double -> String #-}

    -- Haskell 2010 character unescaping, see:
    -- http://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-200002.6
    unescape :: String -> String
    unescape :: String -> String
    unescape = String -> String
    go
      where
        go :: String -> String
    go String
    input = case String
    input of
          String
    "" -> String
    ""
          Char
    '\\' : Char
    'x' : Char
    x : String
    xs | Char -> Bool
    isHexDigit Char
    x -> case (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isHexDigit String
    xs of
            (String
    ys, String
    zs) -> (Int -> Char
    chr (Int -> Char) -> (String -> Int) -> String -> Char
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Int
    readHex (String -> Char) -> String -> Char
    forall a b. (a -> b) -> a -> b
    $ Char
    xChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ys) Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    zs
          Char
    '\\' : Char
    'o' : Char
    x : String
    xs | Char -> Bool
    isOctDigit Char
    x -> case (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isOctDigit String
    xs of
            (String
    ys, String
    zs) -> (Int -> Char
    chr (Int -> Char) -> (String -> Int) -> String -> Char
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Int
    readOct (String -> Char) -> String -> Char
    forall a b. (a -> b) -> a -> b
    $ Char
    xChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ys) Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    zs
          Char
    '\\' : Char
    x : String
    xs | Char -> Bool
    isDigit Char
    x -> case (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    xs of
            (String
    ys, String
    zs) -> (Int -> Char
    chr (Int -> Char) -> (String -> Int) -> String -> Char
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Int
    forall a. Read a => String -> a
    read (String -> Char) -> String -> Char
    forall a b. (a -> b) -> a -> b
    $ Char
    xChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ys) Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    zs
          Char
    '\\' : String
    input_ -> case String
    input_ of
            Char
    '\\' : String
    xs -> Char
    '\\' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'a' : String
    xs -> Char
    '\a' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'b' : String
    xs -> Char
    '\b' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'f' : String
    xs -> Char
    '\f' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'n' : String
    xs -> Char
    '\n' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'r' : String
    xs -> Char
    '\r' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    't' : String
    xs -> Char
    '\t' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'v' : String
    xs -> Char
    '\v' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '&' : String
    xs -> String -> String
    go String
    xs
            Char
    'N':Char
    'U':Char
    'L' : String
    xs -> Char
    '\NUL' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'O':Char
    'H' : String
    xs -> Char
    '\SOH' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'T':Char
    'X' : String
    xs -> Char
    '\STX' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'T':Char
    'X' : String
    xs -> Char
    '\ETX' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'O':Char
    'T' : String
    xs -> Char
    '\EOT' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'N':Char
    'Q' : String
    xs -> Char
    '\ENQ' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'A':Char
    'C':Char
    'K' : String
    xs -> Char
    '\ACK' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'B':Char
    'E':Char
    'L' : String
    xs -> Char
    '\BEL' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'B':Char
    'S' : String
    xs -> Char
    '\BS' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'H':Char
    'T' : String
    xs -> Char
    '\HT' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'L':Char
    'F' : String
    xs -> Char
    '\LF' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'V':Char
    'T' : String
    xs -> Char
    '\VT' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'F':Char
    'F' : String
    xs -> Char
    '\FF' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'C':Char
    'R' : String
    xs -> Char
    '\CR' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'O' : String
    xs -> Char
    '\SO' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'I' : String
    xs -> Char
    '\SI' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'L':Char
    'E' : String
    xs -> Char
    '\DLE' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'C':Char
    '1' : String
    xs -> Char
    '\DC1' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'C':Char
    '2' : String
    xs -> Char
    '\DC2' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'C':Char
    '3' : String
    xs -> Char
    '\DC3' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'C':Char
    '4' : String
    xs -> Char
    '\DC4' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'N':Char
    'A':Char
    'K' : String
    xs -> Char
    '\NAK' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'Y':Char
    'N' : String
    xs -> Char
    '\SYN' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'T':Char
    'B' : String
    xs -> Char
    '\ETB' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'C':Char
    'A':Char
    'N' : String
    xs -> Char
    '\CAN' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'M' : String
    xs -> Char
    '\EM' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'U':Char
    'B' : String
    xs -> Char
    '\SUB' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'E':Char
    'S':Char
    'C' : String
    xs -> Char
    '\ESC' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'F':Char
    'S' : String
    xs -> Char
    '\FS' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'G':Char
    'S' : String
    xs -> Char
    '\GS' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'R':Char
    'S' : String
    xs -> Char
    '\RS' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'U':Char
    'S' : String
    xs -> Char
    '\US' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'S':Char
    'P' : String
    xs -> Char
    '\SP' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    'D':Char
    'E':Char
    'L' : String
    xs -> Char
    '\DEL' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    '@' : String
    xs -> Char
    '\^@' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'A' : String
    xs -> Char
    '\^A' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'B' : String
    xs -> Char
    '\^B' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'C' : String
    xs -> Char
    '\^C' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'D' : String
    xs -> Char
    '\^D' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'E' : String
    xs -> Char
    '\^E' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'F' : String
    xs -> Char
    '\^F' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'G' : String
    xs -> Char
    '\^G' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'H' : String
    xs -> Char
    '\^H' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'I' : String
    xs -> Char
    '\^I' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'J' : String
    xs -> Char
    '\^J' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'K' : String
    xs -> Char
    '\^K' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'L' : String
    xs -> Char
    '\^L' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'M' : String
    xs -> Char
    '\^M' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'N' : String
    xs -> Char
    '\^N' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'O' : String
    xs -> Char
    '\^O' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'P' : String
    xs -> Char
    '\^P' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'Q' : String
    xs -> Char
    '\^Q' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'R' : String
    xs -> Char
    '\^R' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'S' : String
    xs -> Char
    '\^S' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'T' : String
    xs -> Char
    '\^T' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'U' : String
    xs -> Char
    '\^U' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'V' : String
    xs -> Char
    '\^V' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'W' : String
    xs -> Char
    '\^W' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'X' : String
    xs -> Char
    '\^X' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'Y' : String
    xs -> Char
    '\^Y' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    'Z' : String
    xs -> Char
    '\^Z' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    '[' : String
    xs -> Char
    '\^[' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    '\\' : String
    xs -> Char
    '\^\' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    ']' : String
    xs -> Char
    '\^]' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    '^' : String
    xs -> Char
    '\^^' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            Char
    '^':Char
    '_' : String
    xs -> Char
    '\^_' Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs
            String
    xs -> String -> String
    go String
    xs
          Char
    x:String
    xs -> Char
    x Char -> String -> String
    forall a. a -> [a] -> [a]
    : String -> String
    go String
    xs

        readHex :: String -> Int
        readHex :: String -> Int
    readHex String
    xs = case ReadS Int
    forall a. (Eq a, Num a) => ReadS a
    N.readHex String
    xs of
          [(Int
    n, String
    "")] -> Int
    n
          [(Int, String)]
    _ -> String -> Int
    forall a. HasCallStack => String -> a
    error String
    "Data.String.Interpolate.Util.readHex: no parse"

        readOct :: String -> Int
        readOct :: String -> Int
    readOct String
    xs = case ReadS Int
    forall a. (Eq a, Num a) => ReadS a
    N.readOct String
    xs of
          [(Int
    n, String
    "")] -> Int
    n
          [(Int, String)]
    _ -> String -> Int
    forall a. HasCallStack => String -> a
    error String
    "Data.String.Interpolate.Util.readHex: no parse"