Permalink
Browse files

Massive edits

--HG--
rename : Data/Text/Format/Param.hs => Data/Text/Buildable.hs
  • Loading branch information...
bos committed May 20, 2011
1 parent 5ef090f commit 323e3119bd9dabac9099e7b480937888d1929426
View
@@ -0,0 +1,148 @@
+{-# LANGUAGE FlexibleInstances #-}
+
+module Data.Text.Buildable
+ (
+ Buildable(..)
+ ) where
+
+import Data.Int (Int8, Int16, Int32, Int64)
+import Data.Ratio (Ratio, denominator, numerator)
+import Data.Text.Format.Functions ((<>))
+import Data.Text.Format.Int (integral)
+import Data.Text.Format.RealFloat (showFloat)
+import Data.Text.Format.RealFloat.Fast (fshowFloat)
+import Data.Text.Format.Types (Fast(..), Shown(..))
+import Data.Text.Lazy.Builder
+import Data.Time.Calendar (Day, showGregorian)
+import Data.Time.Clock (DiffTime, NominalDiffTime, UTCTime, UniversalTime)
+import Data.Time.Clock (getModJulianDate)
+import Data.Time.LocalTime (LocalTime, TimeOfDay, TimeZone, ZonedTime)
+import Data.Word (Word, Word8, Word16, Word32, Word64)
+import qualified Data.Text as ST
+import qualified Data.Text.Lazy as LT
+
+class Buildable p where
+ build :: p -> Builder
+
+instance Buildable Builder where
+ build = id
+
+instance Buildable LT.Text where
+ build = fromLazyText
+ {-# INLINE build #-}
+
+instance Buildable ST.Text where
+ build = fromText
+ {-# INLINE build #-}
+
+instance Buildable Char where
+ build = singleton
+ {-# INLINE build #-}
+
+instance Buildable [Char] where
+ build = fromString
+ {-# INLINE build #-}
+
+instance Buildable Int8 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Int16 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Int32 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Int where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Int64 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Integer where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Word8 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Word16 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Word32 where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Word where
+ build = integral
+ {-# INLINE build #-}
+
+instance Buildable Word64 where
+ build = integral
+ {-# INLINE build #-}
+
+instance (Integral a, Buildable a) => Buildable (Ratio a) where
+ {-# SPECIALIZE instance Buildable (Ratio Integer) #-}
+ build a = build (numerator a) <> singleton '/' <> build (denominator a)
+
+instance Buildable Float where
+ build = showFloat
+ {-# INLINE build #-}
+
+instance Buildable Double where
+ build = showFloat
+ {-# INLINE build #-}
+
+instance Buildable (Fast Float) where
+ build = fshowFloat . fromFast
+ {-# INLINE build #-}
+
+instance Buildable (Fast Double) where
+ build = fshowFloat . fromFast
+ {-# INLINE build #-}
+
+instance Buildable DiffTime where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable NominalDiffTime where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable UTCTime where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable UniversalTime where
+ build = build . Shown . getModJulianDate
+ {-# INLINE build #-}
+
+instance Buildable Day where
+ build = fromString . showGregorian
+ {-# INLINE build #-}
+
+instance (Show a) => Buildable (Shown a) where
+ build = fromString . show . shown
+ {-# INLINE build #-}
+
+instance Buildable TimeOfDay where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable TimeZone where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable LocalTime where
+ build = build . Shown
+ {-# INLINE build #-}
+
+instance Buildable ZonedTime where
+ build = build . Shown
+ {-# INLINE build #-}
View
@@ -1,23 +1,50 @@
{-# LANGUAGE OverloadedStrings #-}
module Data.Text.Format
- where
+ (
+ Fast(..)
+ , Only(..)
+ , format
+ , build
+ , print
+ , hprint
+ , left
+ , right
+ ) where
-import Data.Monoid
-import qualified Data.Text.Lazy as LT
-import qualified Data.Text as ST
+import qualified Data.Text.Buildable as B
+import Data.Text.Format.Params (Params(..))
+import Data.Text.Format.Functions ((<>))
+import Data.Text.Format.Types (Fast(..), Only(..))
import Data.Text.Lazy.Builder
-import Data.Text.Format.Param
-import Data.Text.Format.Params
+import Prelude hiding (print)
+import System.IO (Handle)
+import qualified Data.Text as ST
+import qualified Data.Text.Lazy as LT
+import qualified Data.Text.Lazy.IO as LT
build :: Params ps => ST.Text -> ps -> Builder
-build fmt ps
- | null xs && not ("{}" `ST.isInfixOf` fmt) = fromText fmt
- | otherwise = zipParams (map fromText . ST.splitOn "{}" $ fmt) xs
- where xs = buildParams ps
- zipParams (f:fs) (y:ys) = f `mappend` y `mappend` zipParams fs ys
- zipParams [f] [] = f
- zipParams _ _ = error "oops"
+build fmt ps = zipParams (map fromText . ST.splitOn "{}" $ fmt) xs
+ where zipParams (f:fs) (y:ys) = f <> y <> zipParams fs ys
+ zipParams [f] [] = f
+ zipParams _ _ = error . LT.unpack $ format
+ "Data.Text.Format.build: {} sites, but {} parameters"
+ (ST.count "{}" fmt, length xs)
+ xs = buildParams ps
format :: Params ps => ST.Text -> ps -> LT.Text
format fmt ps = toLazyText $ build fmt ps
+
+print :: Params ps => ST.Text -> ps -> IO ()
+print fmt ps = LT.putStr . toLazyText $ build fmt ps
+
+hprint :: Params ps => Handle -> ST.Text -> ps -> IO ()
+hprint h fmt ps = LT.hPutStr h . toLazyText $ build fmt ps
+
+left :: B.Buildable a => Int -> Char -> a -> Builder
+left k c =
+ fromLazyText . LT.justifyLeft (fromIntegral k) c . toLazyText . B.build
+
+right :: B.Buildable a => Int -> Char -> a -> Builder
+right k c =
+ fromLazyText . LT.justifyRight (fromIntegral k) c . toLazyText . B.build
@@ -0,0 +1,22 @@
+{-# LANGUAGE MagicHash #-}
+
+module Data.Text.Format.Functions
+ (
+ (<>)
+ , i2d
+ ) where
+
+import Data.Monoid (mappend)
+import Data.Text.Lazy.Builder (Builder)
+import GHC.Base
+
+-- | Unsafe conversion for decimal digits.
+{-# INLINE i2d #-}
+i2d :: Int -> Char
+i2d (I# i#) = C# (chr# (ord# '0'# +# i#))
+
+(<>) :: Builder -> Builder -> Builder
+(<>) = mappend
+{-# INLINE (<>) #-}
+
+infixr 4 <>
View
@@ -16,9 +16,9 @@ module Data.Text.Format.Int
, minus
) where
-import Data.Char (chr)
import Data.Int (Int8, Int16, Int32, Int64)
-import Data.Monoid (mappend, mempty)
+import Data.Monoid (mempty)
+import Data.Text.Format.Functions ((<>), i2d)
import Data.Text.Lazy.Builder
import Data.Word (Word, Word8, Word16, Word32, Word64)
import GHC.Base (quotInt, remInt)
@@ -52,14 +52,14 @@ integral :: Integral a => a -> Builder
{-# SPECIALIZE integral :: Word64 -> Builder #-}
{-# RULES "integral/Integer" integral = integer :: Integer -> Builder #-}
integral i
- | i < 0 = minus `mappend` go (-i)
+ | i < 0 = minus <> go (-i)
| otherwise = go i
where
go n | n < 10 = digit n
- | otherwise = go (n `quot` 10) `mappend` digit (n `rem` 10)
+ | otherwise = go (n `quot` 10) <> digit (n `rem` 10)
digit :: Integral a => a -> Builder
-digit n = singleton $! chr (fromIntegral n + 48)
+digit n = singleton $! i2d (fromIntegral n + 48)
{-# INLINE digit #-}
minus :: Builder
@@ -72,7 +72,7 @@ int = integral
integer :: Integer -> Builder
integer (S# i#) = int (I# i#)
integer i
- | i < 0 = minus `mappend` go (-i)
+ | i < 0 = minus <> go (-i)
| otherwise = go i
where
go n | n < maxInt = int (fromInteger n)
@@ -105,15 +105,15 @@ T maxInt maxDigits =
putH :: [Integer] -> Builder
putH (n:ns) = case n `quotRemInteger` maxInt of
PAIR(x,y)
- | q > 0 -> int q `mappend` pblock r `mappend` putB ns
- | otherwise -> int r `mappend` putB ns
+ | q > 0 -> int q <> pblock r <> putB ns
+ | otherwise -> int r <> putB ns
where q = fromInteger x
r = fromInteger y
putH _ = error "putH: the impossible happened"
putB :: [Integer] -> Builder
putB (n:ns) = case n `quotRemInteger` maxInt of
- PAIR(x,y) -> pblock q `mappend` pblock r `mappend` putB ns
+ PAIR(x,y) -> pblock q <> pblock r <> putB ns
where q = fromInteger x
r = fromInteger y
putB _ = mempty
@@ -123,6 +123,6 @@ pblock = go maxDigits
where
go !d !n
| d == 1 = digit n
- | otherwise = go (d-1) q `mappend` digit r
+ | otherwise = go (d-1) q <> digit r
where q = n `quotInt` 10
r = n `remInt` 10
View
@@ -1,72 +0,0 @@
-{-# LANGUAGE FlexibleInstances #-}
-
-module Data.Text.Format.Param
- (
- Param(..)
- ) where
-
-import Data.Text.Lazy.Builder
-import Data.Text.Format.Int
-import qualified Data.Text.Lazy as LT
-import Data.Int (Int8, Int16, Int32, Int64)
-import Data.Word (Word, Word8, Word16, Word32, Word64)
-import qualified Data.Text as ST
-
-class Param p where
- buildParam :: p -> Builder
-
-instance Param LT.Text where
- buildParam = fromLazyText
-
-instance Param ST.Text where
- buildParam = fromText
-
-instance Param Char where
- buildParam = singleton
-
-instance Param [Char] where
- buildParam = fromText . ST.pack
-
-instance Param Int8 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Int16 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Int32 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Int where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Int64 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Integer where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Word8 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Word16 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Word32 where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Word where
- buildParam = integral
- {-# INLINE buildParam #-}
-
-instance Param Word64 where
- buildParam = integral
- {-# INLINE buildParam #-}
Oops, something went wrong.

0 comments on commit 323e311

Please sign in to comment.