Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special case for 'build' on minBound of Integral types. Fixes #18 #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Data/Text/Buildable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Data.Int (Int8, Int16, Int32, Int64)
import Data.Fixed (Fixed, HasResolution, showFixed)
import Data.Ratio (Ratio, denominator, numerator)
import Data.Text.Format.Functions ((<>))
import Data.Text.Format.Int (decimal, hexadecimal)
import Data.Text.Format.Int (decimal, hexadecimal, integer)
import Data.Text.Format.Types (Hex(..), Shown(..))
import Data.Text.Lazy.Builder
import Data.Time.Calendar (Day, showGregorian)
Expand Down Expand Up @@ -91,7 +91,7 @@ instance Buildable Int64 where
{-# INLINE build #-}

instance Buildable Integer where
build = decimal
build = integer 10
{-# INLINE build #-}

instance (HasResolution a) => Buildable (Fixed a) where
Expand Down
7 changes: 6 additions & 1 deletion Data/Text/Format/Int.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
module Data.Text.Format.Int
(
decimal
, integer
, hexadecimal
, minus
) where
Expand Down Expand Up @@ -39,7 +40,7 @@ import GHC.Integer.GMP.Internals
# define PAIR(a,b) (a,b)
#endif

decimal :: Integral a => a -> Builder
decimal :: (Bounded a, Integral a)=> a -> Builder
{-# SPECIALIZE decimal :: Int -> Builder #-}
{-# SPECIALIZE decimal :: Int8 -> Builder #-}
{-# SPECIALIZE decimal :: Int16 -> Builder #-}
Expand All @@ -52,6 +53,10 @@ decimal :: Integral a => a -> Builder
{-# SPECIALIZE decimal :: Word64 -> Builder #-}
{-# RULES "decimal/Integer" decimal = integer 10 :: Integer -> Builder #-}
decimal i
| i == minBound =
-- special case, since (-i) would not be representable assuming two's
-- compliment:
minus <> integer 10 (negate $ fromIntegral i)
| i < 0 = minus <> go (-i)
| otherwise = go i
where
Expand Down
2 changes: 1 addition & 1 deletion text-format.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: text-format
version: 0.3.1.1
version: 0.3.1.2
license: BSD3
license-file: LICENSE
homepage: https://github.com/bos/text-format
Expand Down