{-# 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace

trimTail :: String -> String
trimTail :: String -> String
trimTail String
"" = String
""
trimTail String
s = forall a. Int -> [a] -> [a]
take (String -> Int
lastNonBlank String
s) String
s
  where lastNonBlank :: String -> Int
lastNonBlank = (forall a. Num a => a -> a -> a
+Int
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl 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 forall a. Num a => a -> a -> a
+ a
1)
                     | Bool
otherwise = (a
n, a
n 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
                                 forall a. IO a -> Q a
runIO (String -> IO String
readFile String
filename) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Q Exp
quoteExp
    }