module Data.Text.Manipulate.Internal.Types where
import Control.Monad
import qualified Data.Char as Char
import Data.Monoid
import Data.Text.Lazy.Builder (Builder, singleton)
import GHC.Base
isWordBoundary :: Char -> Bool
isWordBoundary c = Char.isUpper c || isBoundary c
isBoundary :: Char -> Bool
isBoundary = not . Char.isAlphaNum
ordinal :: Integral a => a -> Builder
ordinal (toInteger -> n) = decimal n <> suf
where
suf | x `elem` [11..13] = "th"
| y == 1 = "st"
| y == 2 = "nd"
| y == 3 = "rd"
| otherwise = "th"
(flip mod 100 -> x, flip mod 10 -> y) = join (,) (abs n)
decimal :: Integral a => a -> Builder
decimal i
| i < 0 = singleton '-' <> go (i)
| otherwise = go i
where
go n | n < 10 = digit n
| otherwise = go (n `quot` 10) <> digit (n `rem` 10)
digit :: Integral a => a -> Builder
digit n = singleton $! i2d (fromIntegral n)
i2d :: Int -> Char
i2d (I# i#) = C# (chr# (ord# '0'# +# i#))