next node: StringScan,
prev node: BTUnionConv,
up to node: Subsystem String Formatting And Scanning


StringFormat

Formatting strings for output.

Signature of StringFormat

List of Import References :
See BOOL
See BTUnion
See Char
See DENOTATION
See Int
See Nat
See Option
See Real
See Seq
See String

SIGNATURE StringFormat

$Date: 2010-09-30 18:24:17 +0200 (Do, 30. Sep 2010) $ ($Revision: 616 $)

IMPORT  Nat        ONLY nat       
        Int        ONLY int
        Real       ONLY real      
        Char       ONLY char
        String     ONLY string      
        Seq[union] ONLY seq
        BTUnion    ONLY union

Basic Formatting

format(pattern, args) converts arguments as described in pattern to a string. The pattern format is modelled after C and specified below.

FUN format      : denotation ** seq[union] -> string
FUN format      : string ** seq[union] -> string

Script Formatting

The expression format(Pattern <- Val1 & Val2 & ... & ValN) is equivalent to format(Pattern, %(t1(Val1),t2(Vale),...,tN(ValN))), where tI is the according union constructor from BTUnion.

SORT script

FUN <- : denotation ** denotation -> script
FUN <- : denotation ** string -> script
FUN <- : denotation ** real -> script
FUN <- : denotation ** nat -> script
FUN <- : denotation ** int -> script

FUN & : script ** denotation -> script
FUN & : script ** string -> script
FUN & : script ** real -> script
FUN & : script ** nat -> script
FUN & : script ** int -> script

FUN formatS: script -> string

Pattern Specification

The format of pattern is interpreted with the following grammar. Metasymbols are bold, comments are in italics.

format

::= { formatEl }

formatEl

::= % % output single % | % { flag } [ len ] [ . [ prec ] ] type | other char

flag

::= - left justify |
+ always prepend sign |
space prepend space if not negative |
0 justify with zeros |
# alternate format

len

::= * read length from seq[union], must be nat | number

prec

::= * read precision from seq[union], must be nat |
number

number

::= { 0 | .. | 9 } no leading zeros

type

::= n nat | i int | s string | c char | b bool | r real-1 | R real-2 | e real-3 | d denotation

other char

::= print as themselve

% - formats are interpreted thus:

len
specifies minimal field width. Spaces will be prepended until field width is reached. If flag - is given, spaces will be appended. If * is given for len or precision the value is given by the current head of the seq[union], which must be of type nat. The head of the seq[union] must be of the given type. It is removed from the seq and formatted as specified.
output of nats:
precision
prepend zeros until number has precision digits
flag +
prepend + character
flag space
ignored
flag 0
prepend zeros instead of spaces; ignored if flag - is given
flag #
ignored
output of ints:
precision
prepend zeros until number has precision digits
flag +
prepend + character, if number not negative
flag space
prepend space, if number not negative
flag 0
prepend zeros instead of spaces; ignored if flag - is given
flag #
ignored
output of reals:
precision
produce exactly precision digits after the point, default 6 (but trailing zeros are removed unless flag 0 is given)
flag +
prepend + character if number not negative
flag space
prepend space, if number not negative
flag 0
do not remove trailing zeros
flag #
always add decimal point
Three different formats are available for real numbers:
%r real-1 if (abs(number) <= max'Nat and abs(number) > 1e-4) or number = 0 then output [+|-]nnnn.ddddd else [+|-]n.dddddd e[+|-]nnn
%R real-2 output as [+|-]nnnnnnnnnnn.ddddddddd
%e real-3 output as [+|-]n.dddddd[e][+|-]nnnn
Note: trailing zeros are not removed if they are result of rounding e.g. "0.899" with 2 digits precision is always printed as "0.90" Sometimes trailing zeros are not removed, presumably due to inaccuracies of conversion from binary to decimal representation.
output of strings and denotations:
precision
output only the first precision characters
flag +
ignored
flag space
ignored
flag 0
ignored
flag #
ignored
output of characters:
Characters are treated like strings.
output of booleans:
Output the string true or false.
precision
output only the first precision characters of true or false
flag +
ignored
flag space
ignored
flag 0
output true as "1" and false as "0"
flag #
use uppercase (TRUE, FALSE)


next node: StringScan,
prev node: BTUnionConv,
up to node: Subsystem String Formatting And Scanning