formatting 5.1 → 5.2
raw patch · 3 files changed
+124/−13 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Formatting.Formatters: asInt :: Enum a => Format a
+ Formatting.Formatters: base :: Integral a => Int -> Format a
+ Formatting.Formatters: bin :: Integral a => Format a
+ Formatting.Formatters: char :: Format Char
+ Formatting.Formatters: oct :: Integral a => Format a
+ Formatting.Formatters: prefixBin :: Integral a => Format a
+ Formatting.Formatters: prefixHex :: Integral a => Format a
+ Formatting.Formatters: prefixOct :: Integral a => Format a
+ Formatting.Formatters: shown :: Show a => Format a
+ Formatting.ShortFormatters: b :: Integral a => Format a
+ Formatting.ShortFormatters: c :: Format Char
+ Formatting.ShortFormatters: d :: Integral a => Format a
+ Formatting.ShortFormatters: o :: Integral a => Format a
+ Formatting.ShortFormatters: sh :: Show a => Format a
- Formatting.ShortFormatters: x :: Format Integer
+ Formatting.ShortFormatters: x :: Integral a => Format a
Files
- formatting.cabal +1/−1
- src/Formatting/Formatters.hs +92/−5
- src/Formatting/ShortFormatters.hs +31/−7
formatting.cabal view
@@ -1,5 +1,5 @@ name: formatting-version: 5.1+version: 5.2 synopsis: Combinator-based type-safe formatting (like printf() or FORMAT) description: Combinator-based type-safe formatting (like printf() or FORMAT), modelled from the HoleyMonoids package. license: BSD3
src/Formatting/Formatters.hs view
@@ -19,6 +19,8 @@ text, stext, string,+ shown,+ char, builder, fconst, -- * Numbers@@ -32,11 +34,18 @@ shortest, commas, ords,+ asInt, -- * Padding left, right, -- * Bases+ base,+ bin,+ oct, hex,+ prefixBin,+ prefixOct,+ prefixHex, -- * Buildables build, Buildable@@ -44,6 +53,8 @@ import Formatting.Holey +import Data.Char (chr, ord)+import Numeric (showIntAtBase) import Data.Monoid import qualified Data.Text as S import qualified Data.Text as T@@ -61,11 +72,6 @@ text :: Format Text text = later T.fromLazyText --- | Render an integer using hexadecimal notation. (No leading 0x is--- added.)-hex :: Integral a => Format a-hex = later T.hex- -- | Output a strict text. stext :: Format S.Text stext = later T.fromText@@ -74,6 +80,18 @@ string :: Format String string = later (T.fromText . T.pack) +-- | Output a showable value (instance of 'Show') by turning it into+-- 'Text':+--+-- >>> format ("Value number " % shown % " is " % shown % ".") 42 False+-- "Value number 42 is False."+shown :: Show a => Format a+shown = later (T.fromText . T.pack . show)++-- | Output a character.+char :: Format Char+char = later B.build+ -- | Build a builder. builder :: Format Builder builder = later id@@ -123,6 +141,13 @@ scifmt :: FPFormat -> Maybe Int -> Format Scientific scifmt f i = later (formatScientificBuilder f i) +-- | Shows the Int value of Enum instances using 'fromEnum'.+--+-- >>> format ("Got: " % char % " (" % asInt % ")") 'a' 'a'+-- "Got: a (97)"+asInt :: Enum a => Format a+asInt = later (T.shortest . fromEnum)+ -- | Pad the left hand side of a string until it reaches k characters -- wide, if necessary filling with character c. left :: Buildable a => Int -> Char -> Format a@@ -160,3 +185,65 @@ 3 -> "rd" _ -> "th" where tens = n `mod` 100++-- Bases+base :: Integral a => Int -> Format a+base numBase = later (B.build . atBase numBase)+ +-- | Render an integer using binary notation. (No leading 0b is+-- added.) Defined as @bin = 'base' 2@.+bin :: Integral a => Format a+bin = base 2+{-# INLINE bin #-}++-- | Render an integer using octal notation. (No leading 0o is+-- added.) Defined as @oct = 'base' 8@.+oct :: Integral a => Format a+oct = base 8+{-# INLINE oct #-}++-- | Render an integer using hexadecimal notation. (No leading 0x is+-- added.) Has a specialized implementation. +hex :: Integral a => Format a+hex = later T.hex+{-# INLINE hex #-}++-- | Render an integer using binary notation with a leading 0b.+prefixBin :: Integral a => Format a+prefixBin = "0b" % bin+{-# INLINE prefixBin #-}++-- | Render an integer using octal notation with a leading 0o.+prefixOct :: Integral a => Format a+prefixOct = "0o" % oct+{-# INLINE prefixOct #-}++-- | Render an integer using hexadecimal notation with a leading 0x.+prefixHex :: Integral a => Format a+prefixHex = "0x" % hex+{-# INLINE prefixHex #-}++-- The following code is mostly taken from `Numeric.Lens.' (from+-- `lens') and modified.++-- | Internal function that converts a number to a base base-2 through+-- base-36.+atBase :: Integral a => Int -> a -> String+atBase b _ | b < 2 || b > 36 = error ("base: Invalid base " ++ show b)+atBase b n =+ showSigned' (showIntAtBase (toInteger b) intToDigit') (toInteger n) ""+{-# INLINE atBase #-}++-- | A simpler variant of 'Numeric.showSigned' that only prepends a dash and+-- doesn't know about parentheses+showSigned' :: Real a => (a -> ShowS) -> a -> ShowS+showSigned' f n+ | n < 0 = showChar '-' . f (negate n)+ | otherwise = f n++-- | Like 'Data.Char.intToDigit', but handles up to base-36+intToDigit' :: Int -> Char+intToDigit' i+ | i >= 0 && i < 10 = chr (ord '0' + i)+ | i >= 10 && i < 36 = chr (ord 'a' + i - 10)+ | otherwise = error ("intToDigit': Invalid int " ++ show i)
src/Formatting/ShortFormatters.hs view
@@ -15,8 +15,10 @@ module Formatting.ShortFormatters where +import Formatting.Formatters (bin, int, oct) import Formatting.Holey +import qualified Data.Text.Buildable as B (build) import qualified Data.Text as S import qualified Data.Text as T import Data.Text.Buildable (Buildable)@@ -28,9 +30,22 @@ t :: Format Text t = later T.fromLazyText +-- | Render an integral e.g. 123 -> \"123\", 0 -> \"0\".+d :: Integral a => Format a+d = int++-- | Render an integer using binary notation. (No leading 0b is+-- added.)+b :: Integral a => Format a+b = bin++-- | Render an integer using octal notation. (No leading 0o is added.)+o :: Integral a => Format a+o = oct+ -- | Render an integer using hexadecimal notation. (No leading 0x is -- added.)-x :: Format Integer+x :: Integral a => Format a x = later T.hex -- | Output a strict text.@@ -41,6 +56,15 @@ s :: Format String s = later (T.fromText . T.pack) +-- | Output a showable value (instance of 'Show') by turning it into+-- 'Text'.+sh :: Show a => Format a+sh = later (T.fromText . T.pack . show)++-- | Output a character.+c :: Format Char+c = later B.build+ -- | Render a floating point number using scientific/engineering -- notation (e.g. 2.3e123), with the given number of decimal places. ef :: Real a => Int -> Format a@@ -62,12 +86,12 @@ sf :: Real a => Format a sf = later T.shortest --- | Pad the left hand side of a string until it reaches k characters--- wide, if necessary filling with character c.+-- | Pad the left hand side of a string until it reaches @k@ characters+-- wide, if necessary filling with character @ch@. l :: Buildable a => Int -> Char -> Format a-l i c = later (T.left i c)+l i ch = later (T.left i ch) --- | Pad the right hand side of a string until it reaches k characters--- wide, if necessary filling with character c.+-- | Pad the right hand side of a string until it reaches @k@ characters+-- wide, if necessary filling with character @ch@. r :: Buildable a => Int -> Char -> Format a-r i c = later (T.right i c)+r i ch = later (T.right i ch)