    {-# LANGUAGE NamedFieldPuns #-}
    {-# OPTIONS_GHC -fno-warn-missing-fields #-}

    module Data.String.Here.Internal (trim, quoteDependentFile) where

    import Data.Char

    import Language.Haskell.TH.Quote
    import Language.Haskell.TH.Syntax

    trim :: String -> String
    trim :: String -> String
    trim = String -> String
    trimTail (String -> String) -> (String -> String) -> String -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Char -> Bool) -> String -> String
    forall a. (a -> Bool) -> [a] -> [a]
    dropWhile Char -> Bool
    isSpace

    trimTail :: String -> String
    trimTail :: String -> String
    trimTail String
    "" = String
    ""
    trimTail String
    s = Int -> String -> String
    forall a. Int -> [a] -> [a]
    take (String -> Int
    lastNonBlank String
    s) String
    s
      where lastNonBlank :: String -> Int
    lastNonBlank = (Int -> Int -> Int
    forall a. Num a => a -> a -> a
    +Int
    1) (Int -> Int) -> (String -> Int) -> String -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int, Int) -> Int
    forall a b. (a, b) -> a
    fst ((Int, Int) -> Int) -> (String -> (Int, Int)) -> String -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ((Int, Int) -> Char -> (Int, Int))
    -> (Int, Int) -> String -> (Int, Int)
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl (Int, Int) -> Char -> (Int, Int)
    forall {a}. Num a => (a, a) -> Char -> (a, a)
    acc (Int
    0, Int
    0)
            acc :: (a, a) -> Char -> (a, a)
    acc (a
    l, a
    n) Char
    c | Char -> Bool
    isSpace Char
    c = (a
    l, a
    n a -> a -> a
    forall a. Num a => a -> a -> a
    + a
    1)
                         | Bool
    otherwise = (a
    n, a
    n a -> a -> a
    forall a. Num a => a -> a -> a
    + a
    1)

    quoteDependentFile :: QuasiQuoter -> QuasiQuoter
    quoteDependentFile :: QuasiQuoter -> QuasiQuoter
    quoteDependentFile QuasiQuoter {String -> Q Exp
    quoteExp :: QuasiQuoter -> String -> Q Exp
    quoteExp :: String -> Q Exp
    quoteExp} =
      QuasiQuoter
        { quoteExp :: String -> Q Exp
    quoteExp = \String
    filename -> do String -> Q ()
    addDependentFile String
    filename
                                     IO String -> Q String
    forall a. IO a -> Q a
    runIO (String -> IO String
    readFile String
    filename) Q String -> (String -> Q Exp) -> Q Exp
    forall a b. Q a -> (a -> Q b) -> Q b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= String -> Q Exp
    quoteExp
        }