{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
module TextShow.Utils (
i2d
, isInfixDataCon
, isSymVar
, isTupleString
, lengthB
, toString
, toText
, unlinesB
, unwordsB
) where
import Data.Int (Int64)
import Data.Text (Text)
import Data.Text.Lazy (length, toStrict, unpack)
import Data.Text.Lazy.Builder (Builder, singleton, toLazyText)
import GHC.Exts (Char(C#), Int(I#), (+#), chr#, ord#)
import Prelude ()
import Prelude.Compat hiding (length)
#if defined(MIN_VERSION_ghc_boot_th)
import GHC.Lexeme (startsVarSym)
#else
import Data.Char (isSymbol, ord)
#endif
i2d :: Int -> Char
i2d :: Int -> Char
i2d (I# Int#
i#) = Char# -> Char
C# (Int# -> Char#
chr# (Char# -> Int#
ord# Char#
'0'# Int# -> Int# -> Int#
+# Int#
i#))
{-# INLINE i2d #-}
isInfixDataCon :: String -> Bool
isInfixDataCon :: String -> Bool
isInfixDataCon (Char
':':String
_) = Bool
True
isInfixDataCon String
_ = Bool
False
{-# INLINE isInfixDataCon #-}
isSymVar :: String -> Bool
isSymVar :: String -> Bool
isSymVar String
"" = Bool
False
isSymVar (Char
c : String
_) = Char -> Bool
startsVarSym Char
c
#if !defined(MIN_VERSION_ghc_boot_th)
startsVarSym :: Char -> Bool
startsVarSym c = startsVarSymASCII c || (ord c > 0x7f && isSymbol c)
startsVarSymASCII :: Char -> Bool
startsVarSymASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
#endif
isTupleString :: String -> Bool
isTupleString :: String -> Bool
isTupleString (Char
'(':Char
',':String
_) = Bool
True
isTupleString String
_ = Bool
False
{-# INLINE isTupleString #-}
lengthB :: Builder -> Int64
lengthB :: Builder -> Int64
lengthB = Text -> Int64
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText
{-# INLINE lengthB #-}
toString :: Builder -> String
toString :: Builder -> String
toString = Text -> String
unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText
{-# INLINE toString #-}
toText :: Builder -> Text
toText :: Builder -> Text
toText = Text -> Text
toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText
{-# INLINE toText #-}
unlinesB :: [Builder] -> Builder
unlinesB :: [Builder] -> Builder
unlinesB (Builder
b:[Builder]
bs) = Builder
b forall a. Semigroup a => a -> a -> a
<> Char -> Builder
singleton Char
'\n' forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
unlinesB [Builder]
bs
unlinesB [] = forall a. Monoid a => a
mempty
unwordsB :: [Builder] -> Builder
unwordsB :: [Builder] -> Builder
unwordsB (Builder
b:bs :: [Builder]
bs@(Builder
_:[Builder]
_)) = Builder
b forall a. Semigroup a => a -> a -> a
<> Char -> Builder
singleton Char
' ' forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
unwordsB [Builder]
bs
unwordsB [Builder
b] = Builder
b
unwordsB [] = forall a. Monoid a => a
mempty