-- | Internal functions for timestats
module Debug.TimeStats.Internal
  ( formatIntWithSeparator
  ) where


-- | Formats an int with a thousand separator.
--
-- For instance
--
-- > formatIntWithSeparator '_' 123456789 "a" == "123_456_789a"
--
formatIntWithSeparator :: Char -> Int -> ShowS
formatIntWithSeparator :: Char -> Int -> ShowS
formatIntWithSeparator Char
sep Int
x0 String
s0 =
    if Int
x0 forall a. Ord a => a -> a -> Bool
>= Int
0 then forall {a}. (Show a, Integral a) => a -> ShowS
go Int
x0 String
s0 else Char
'-' forall a. a -> [a] -> [a]
: forall {a}. (Show a, Integral a) => a -> ShowS
go (-Int
x0) String
s0
  where
    -- precondition: x >= 0
    go :: a -> ShowS
go a
x String
s =
      let (a
d, a
m) = forall a. Integral a => a -> a -> (a, a)
divMod a
x a
1000
       in if a
d forall a. Ord a => a -> a -> Bool
<= a
0 then forall a. Show a => a -> ShowS
shows a
m String
s
          else a -> ShowS
go a
d forall a b. (a -> b) -> a -> b
$ Char
sep forall a. a -> [a] -> [a]
: forall {a}. (Ord a, Num a) => a -> String
pad999 a
m forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> ShowS
shows a
m String
s

    -- precondition: 0 <= m && m <= 999
    pad999 :: a -> String
pad999 a
m
      | a
m forall a. Ord a => a -> a -> Bool
< a
10    = String
"00"
      | a
m forall a. Ord a => a -> a -> Bool
< a
100   =  String
"0"
      | Bool
otherwise =   String
""