print, aprint, fprint, sprint - print formatted output
include "sys.m";
sys := load Sys Sys->PATH;
aprint: fn(format: string, *): array of byte;
fprint: fn(fd: ref FD, format: string, *): int;
print: fn(format: string, *): int;
sprint: fn(format: string, *): string;
These functions format and print their arguments as
UTF text. Print writes text to the standard output.
Fprint writes to the named output file descriptor. Sprint
places text in a string, which it returns. Aprint is similar but
returns the text in utf (6) representation as an array of bytes.
Print and fprint return the number of bytes transmitted or a
negative value if an error was encountered when writing the output.
Each of these functions converts, formats, and prints its trailing
arguments under control of a format string. The format contains two
types of objects: plain characters, which are simply copied to the output
stream, and conversion specifications, each of which results in fetching of
zero or more arguments. The Limbo compiler recognizes calls to these
functions and checks that the arguments match the format specifications in
number and type.
Each conversion specification has the following format:
- % [flags] verb
The verb is a single character and each flag is a single character
or a (decimal) numeric string. Up to two numeric strings may be used; the
first is called f1, the second f2. They can be separated by
`.', and if one is present, then f1 and f2 are taken to
be zero if missing, otherwise they are considered `omitted'. Either or both
of the numbers may be replaced with the character *, meaning that the
actual number will be obtained from the argument list as an integer. The
flags and numbers are arguments to the verb described below.
- d, o, x,
X
- The numeric verbs d, o, and x format their int
arguments in decimal, octal, and hexadecimal (with hex digits in
lower-case). The flag b is required when the corresponding value is
a Limbo big, not an int. Arguments are taken to be signed,
unless the u flag is given, to force them to be treated as
unsigned. Each interprets the flags - , , , and # to
mean left justified, commas every three digits, and alternative format. If
f2 is not omitted, the number is padded on the left with zeros
until at least f2 digits appear. Then, if alternative format is
specified for x conversion, the number is preceded by 0x.
Finally, if f1 is not omitted, the number is padded on the left (or
right, if left justification is specified) with enough blanks to make the
field at least f1 characters long. The verb X is similar to
x, except that the hexadecimal digits are displayed in upper-case,
and in alternative format, the number is preceded by 0X.
- e, f,
g
- The floating point verbs e, f, and g take a
real argument. Each interprets the flags +, -, and
# to mean always print a sign, left justified, and alternative
format. F1 is the minimum field width and, if the converted value
takes up less than f1 characters, it is padded on the left (or
right, if `left justified') with spaces. F2 is the number of digits
that are converted after the decimal place for e and f
conversions, and f2 is the maximum number of significant digits for
g conversions. The f verb produces output of the form
[-]digits[.digits]. The e conversion
appends an exponent e[-]digits. The g verb
will output the argument in either e or f with the goal of
producing the smallest output. Also, trailing zeros are omitted from the
fraction part of the output, and a trailing decimal point appears only if
it is followed by a digit. When alternative format is specified, the
result will always contain a decimal point, and for g conversions,
trailing zeros are not removed.
- E, G
- These are the same as e and g respectively, but use E
not e to specify an exponent when one appears.
- c
- The c verb converts a single Unicode character from an int
argument to a UTF encoding, justified within a field of f1
characters as described above.
- r
- The r verb takes no arguments; it prints the error string
associated with the most recent system error.
- s
- The s verb copies a string to the output. The number of
characters copied (n) is the minimum of the size of the string and
f2. These n characters are justified within a field of
f1 characters as described above.
- q
- The q verb copies a string to the output as for s,
but quotes the string in the style of sh (1) only if necessary to
avoid ambiguity (for instance if the string contains quotes or spaces). If
the format string, however, includes the specifier # (for example
%#q), the printed string will always be quoted.
/libinterp/runt.c:/^xprint
/os/port/print.c
/lib9/print.c
The x verb does not apply the 0x prefix when
f2 is present. The prefix should probably be 16r anyway.