haskus-system-0.6.0.0: Haskus system programming framework

Safe HaskellNone
LanguageHaskell2010

Haskus.Format.Text

Contents

Synopsis

Documentation

module Data.Text

Conversions

textEncodeUtf8 :: Text -> Buffer Source #

Encode Text into Utf8

stringEncodeUtf8 :: String -> Buffer Source #

Encode String into Utf8

Formatting

textFormat :: Params ps => Format -> ps -> Text Source #

Format a text

newtype Only a :: * -> * #

Use this newtype wrapper for your single parameter if you are formatting a string containing exactly one substitution site.

Constructors

Only 

Fields

Instances

Bounded a => Bounded (Only a) 

Methods

minBound :: Only a #

maxBound :: Only a #

Enum a => Enum (Only a) 

Methods

succ :: Only a -> Only a #

pred :: Only a -> Only a #

toEnum :: Int -> Only a #

fromEnum :: Only a -> Int #

enumFrom :: Only a -> [Only a] #

enumFromThen :: Only a -> Only a -> [Only a] #

enumFromTo :: Only a -> Only a -> [Only a] #

enumFromThenTo :: Only a -> Only a -> Only a -> [Only a] #

Eq a => Eq (Only a) 

Methods

(==) :: Only a -> Only a -> Bool #

(/=) :: Only a -> Only a -> Bool #

Floating a => Floating (Only a) 

Methods

pi :: Only a #

exp :: Only a -> Only a #

log :: Only a -> Only a #

sqrt :: Only a -> Only a #

(**) :: Only a -> Only a -> Only a #

logBase :: Only a -> Only a -> Only a #

sin :: Only a -> Only a #

cos :: Only a -> Only a #

tan :: Only a -> Only a #

asin :: Only a -> Only a #

acos :: Only a -> Only a #

atan :: Only a -> Only a #

sinh :: Only a -> Only a #

cosh :: Only a -> Only a #

tanh :: Only a -> Only a #

asinh :: Only a -> Only a #

acosh :: Only a -> Only a #

atanh :: Only a -> Only a #

log1p :: Only a -> Only a #

expm1 :: Only a -> Only a #

log1pexp :: Only a -> Only a #

log1mexp :: Only a -> Only a #

Fractional a => Fractional (Only a) 

Methods

(/) :: Only a -> Only a -> Only a #

recip :: Only a -> Only a #

fromRational :: Rational -> Only a #

Integral a => Integral (Only a) 

Methods

quot :: Only a -> Only a -> Only a #

rem :: Only a -> Only a -> Only a #

div :: Only a -> Only a -> Only a #

mod :: Only a -> Only a -> Only a #

quotRem :: Only a -> Only a -> (Only a, Only a) #

divMod :: Only a -> Only a -> (Only a, Only a) #

toInteger :: Only a -> Integer #

Num a => Num (Only a) 

Methods

(+) :: Only a -> Only a -> Only a #

(-) :: Only a -> Only a -> Only a #

(*) :: Only a -> Only a -> Only a #

negate :: Only a -> Only a #

abs :: Only a -> Only a #

signum :: Only a -> Only a #

fromInteger :: Integer -> Only a #

Ord a => Ord (Only a) 

Methods

compare :: Only a -> Only a -> Ordering #

(<) :: Only a -> Only a -> Bool #

(<=) :: Only a -> Only a -> Bool #

(>) :: Only a -> Only a -> Bool #

(>=) :: Only a -> Only a -> Bool #

max :: Only a -> Only a -> Only a #

min :: Only a -> Only a -> Only a #

Read a => Read (Only a) 
Real a => Real (Only a) 

Methods

toRational :: Only a -> Rational #

RealFloat a => RealFloat (Only a) 
RealFrac a => RealFrac (Only a) 

Methods

properFraction :: Integral b => Only a -> (b, Only a) #

truncate :: Integral b => Only a -> b #

round :: Integral b => Only a -> b #

ceiling :: Integral b => Only a -> b #

floor :: Integral b => Only a -> b #

Show a => Show (Only a) 

Methods

showsPrec :: Int -> Only a -> ShowS #

show :: Only a -> String #

showList :: [Only a] -> ShowS #

Buildable a => Params (Only a) 

Methods

buildParams :: Only a -> [Builder] #

hex :: Integral a => a -> Builder #

Render an integer using hexadecimal notation. (No leading "0x" is added.)

printf :: PrintfType r => String -> r #

Format a variable number of arguments with the C-style formatting string. The return value is either String or (IO a) (which should be (IO '()'), but Haskell's type system makes this hard).

The format string consists of ordinary characters and conversion specifications, which specify how to format one of the arguments to printf in the output string. A format specification is introduced by the % character; this character can be self-escaped into the format string using %%. A format specification ends with a /format character/ that provides the primary information about how to format the value. The rest of the conversion specification is optional. In order, one may have flag characters, a width specifier, a precision specifier, and type-specific modifier characters.

Unlike C printf(3), the formatting of this printf is driven by the argument type; formatting is type specific. The types formatted by printf "out of the box" are:

printf is also extensible to support other types: see below.

A conversion specification begins with the character %, followed by zero or more of the following flags:

   -      left adjust (default is right adjust)
   +      always use a sign (+ or -) for signed conversions
   space  leading space for positive numbers in signed conversions
   0      pad with zeros rather than spaces
   #      use an \"alternate form\": see below

When both flags are given, - overrides 0 and + overrides space. A negative width specifier in a * conversion is treated as positive but implies the left adjust flag.

The "alternate form" for unsigned radix conversions is as in C printf(3):

   %o           prefix with a leading 0 if needed
   %x           prefix with a leading 0x if nonzero
   %X           prefix with a leading 0X if nonzero
   %b           prefix with a leading 0b if nonzero
   %[eEfFgG]    ensure that the number contains a decimal point

Any flags are followed optionally by a field width:

   num    field width
   *      as num, but taken from argument list

The field width is a minimum, not a maximum: it will be expanded as needed to avoid mutilating a value.

Any field width is followed optionally by a precision:

   .num   precision
   .      same as .0
   .*     as num, but taken from argument list

Negative precision is taken as 0. The meaning of the precision depends on the conversion type.

   Integral    minimum number of digits to show
   RealFloat   number of digits after the decimal point
   String      maximum number of characters

The precision for Integral types is accomplished by zero-padding. If both precision and zero-pad are given for an Integral field, the zero-pad is ignored.

Any precision is followed optionally for Integral types by a width modifier; the only use of this modifier being to set the implicit size of the operand for conversion of a negative operand to unsigned:

   hh     Int8
   h      Int16
   l      Int32
   ll     Int64
   L      Int64

The specification ends with a format character:

   c      character               Integral
   d      decimal                 Integral
   o      octal                   Integral
   x      hexadecimal             Integral
   X      hexadecimal             Integral
   b      binary                  Integral
   u      unsigned decimal        Integral
   f      floating point          RealFloat
   F      floating point          RealFloat
   g      general format float    RealFloat
   G      general format float    RealFloat
   e      exponent format float   RealFloat
   E      exponent format float   RealFloat
   s      string                  String
   v      default format          any type

The "%v" specifier is provided for all built-in types, and should be provided for user-defined type formatters as well. It picks a "best" representation for the given type. For the built-in types the "%v" specifier is converted as follows:

   c      Char
   u      other unsigned Integral
   d      other signed Integral
   g      RealFloat
   s      String

Mismatch between the argument types and the format string, as well as any other syntactic or semantic errors in the format string, will cause an exception to be thrown at runtime.

Note that the formatting for RealFloat types is currently a bit different from that of C printf(3), conforming instead to showEFloat, showFFloat and showGFloat (and their alternate versions showFFloatAlt and showGFloatAlt). This is hard to fix: the fixed versions would format in a backward-incompatible way. In any case the Haskell behavior is generally more sensible than the C behavior. A brief summary of some key differences:

  • Haskell printf never uses the default "6-digit" precision used by C printf.
  • Haskell printf treats the "precision" specifier as indicating the number of digits after the decimal point.
  • Haskell printf prints the exponent of e-format numbers without a gratuitous plus sign, and with the minimum possible number of digits.
  • Haskell printf will place a zero after a decimal point when possible.

Examples

  > printf "%d\n" (23::Int)
  23
  > printf "%s %s\n" "Hello" "World"
  Hello World
  > printf "%.2f\n" pi
  3.14

Parsing

textParseHexadecimal :: Integral a => Text -> Either String a Source #

Parse an hexadecimal number FIXME: use a real parser (MegaParsec, etc.)

IO

putStrLn :: Text -> IO () #

Write a string to stdout, followed by a newline.