scanf-0.1.0.0: Easy and type-safe format strings for parsing and printing

Safe HaskellNone
LanguageHaskell2010

Text.Scanf

Contents

Description

Dead simple and type-safe scanf/printf.

Synopsis

Main definitions

scanf :: Format t -> String -> Maybe t Source #

Parse a string according to a format string.

scanf [fmt|Hello %s|] "Hello world!" :: Maybe (String :+ ())
  = ("world!" :+ ())

printf :: Format t -> t -> String Source #

Print a string according to a format string.

printf [fmt|Hello %s|] ("everyone!" :+ ())
  = "Hello everyone!" 

data a :+ b infixr 1 Source #

A pretty pair type to build lists with values of different types. Remember to close lists with ().

3 :+ "14" :+ () :: Int :+ String :+ ()

Constructors

a :+ b infixr 1 

Instances

(Eq b, Eq a) => Eq ((:+) a b) Source # 

Methods

(==) :: (a :+ b) -> (a :+ b) -> Bool #

(/=) :: (a :+ b) -> (a :+ b) -> Bool #

(Ord b, Ord a) => Ord ((:+) a b) Source # 

Methods

compare :: (a :+ b) -> (a :+ b) -> Ordering #

(<) :: (a :+ b) -> (a :+ b) -> Bool #

(<=) :: (a :+ b) -> (a :+ b) -> Bool #

(>) :: (a :+ b) -> (a :+ b) -> Bool #

(>=) :: (a :+ b) -> (a :+ b) -> Bool #

max :: (a :+ b) -> (a :+ b) -> a :+ b #

min :: (a :+ b) -> (a :+ b) -> a :+ b #

(Show b, Show a) => Show ((:+) a b) Source # 

Methods

showsPrec :: Int -> (a :+ b) -> ShowS #

show :: (a :+ b) -> String #

showList :: [a :+ b] -> ShowS #

fmt :: QuasiQuoter Source #

Parse a typed Format string.

The following conversion strings are supported:

  • %d: signed integer (Int)
  • %l: signed integer (Integer, unbounded)
  • %f: floating point (Double)
  • %s: string of non-space characters (String)
  • %c: single character (Char)
  • %%: parse/print a literal percent character

N.B.: in scanf, spaces in the format string match any number of whitespace character until the next nonspace character.

[fmt|%d lazy %s and %d strict %s|]
  :: Format (Int :+ String :+ Int :+ String :+ ())

fmt_ :: (Format () -> Format t) -> Format t Source #

Construct a format string. This is an alternative to fmt that doesn't rely on Template Haskell.

The components of a format string are composed using (.) (function composition) and (%) (wrapper for constant strings).

fmt_ (int . " lazy " % string . " and " % int . " strict " % string)
  :: Format (Int :+ String :+ Int :+ String :+ ())

data Format t Source #

Typed scanf/printf format strings. They can be built using the fmt quasiquote or with the fmt_ function and format combinators.

Instances

Show (Format t) Source # 

Methods

showsPrec :: Int -> Format t -> ShowS #

show :: Format t -> String #

showList :: [Format t] -> ShowS #

Format constructors

(%) :: String -> (Format t -> Format q) -> Format t -> Format q infixr 9 Source #

Append a constant string to a format string component.

N.B.: in scanf, spaces in the format string match any number of whitespace character until the next nonspace character.

integer :: Format t -> Format (Integer :+ t) Source #

Format an Integer.

int :: Format t -> Format (Int :+ t) Source #

Format an Int.

double :: Format t -> Format (Double :+ t) Source #

Format a Double.

string :: Format t -> Format (String :+ t) Source #

Format a String.

char :: Format t -> Format (Char :+ t) Source #

Format a Char.