1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
2005-09-20 10:18:03 -07:00
|
|
|
(* Xavier Leroy and Pierre Weis, projet Cristal, INRIA Rocquencourt *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2001-10-28 06:21:27 -08:00
|
|
|
(** Formatted output functions. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
val fprintf : out_channel -> ('a, out_channel, unit) format -> 'a
|
2001-10-26 15:38:48 -07:00
|
|
|
(** [fprintf outchan format arg1 ... argN] formats the arguments
|
2005-09-26 03:12:01 -07:00
|
|
|
[arg1] to [argN] according to the format string [format], and
|
|
|
|
outputs the resulting string on the channel [outchan].
|
|
|
|
|
2012-01-11 07:22:51 -08:00
|
|
|
The format string is a character string which contains two types of
|
2005-09-26 03:12:01 -07:00
|
|
|
objects: plain characters, which are simply copied to the output
|
|
|
|
channel, and conversion specifications, each of which causes
|
|
|
|
conversion and printing of arguments.
|
|
|
|
|
|
|
|
Conversion specifications have the following form:
|
|
|
|
|
2012-03-14 09:03:15 -07:00
|
|
|
[% [flags] [width] [.precision] type]
|
2005-09-26 03:12:01 -07:00
|
|
|
|
|
|
|
In short, a conversion specification consists in the [%] character,
|
|
|
|
followed by optional modifiers and a type which is made of one or
|
2012-01-11 07:22:51 -08:00
|
|
|
two characters.
|
2005-09-26 03:12:01 -07:00
|
|
|
|
2012-01-11 07:22:51 -08:00
|
|
|
The types and their meanings are:
|
|
|
|
|
|
|
|
- [d], [i]: convert an integer argument to signed decimal.
|
|
|
|
- [u], [n], [l], [L], or [N]: convert an integer argument to
|
|
|
|
unsigned decimal. Warning: [n], [l], [L], and [N] are
|
|
|
|
used for [scanf], and should not be used for [printf].
|
2001-10-26 15:38:48 -07:00
|
|
|
- [x]: convert an integer argument to unsigned hexadecimal,
|
|
|
|
using lowercase letters.
|
|
|
|
- [X]: convert an integer argument to unsigned hexadecimal,
|
|
|
|
using uppercase letters.
|
|
|
|
- [o]: convert an integer argument to unsigned octal.
|
2002-05-12 11:37:49 -07:00
|
|
|
- [s]: insert a string argument.
|
2012-01-11 07:22:51 -08:00
|
|
|
- [S]: convert a string argument to OCaml syntax (double quotes, escapes).
|
2002-05-12 11:37:49 -07:00
|
|
|
- [c]: insert a character argument.
|
2013-04-25 02:11:44 -07:00
|
|
|
- [C]: convert a character argument to OCaml syntax
|
|
|
|
(single quotes, escapes).
|
2001-10-26 15:38:48 -07:00
|
|
|
- [f]: convert a floating-point argument to decimal notation,
|
2002-05-12 11:37:49 -07:00
|
|
|
in the style [dddd.ddd].
|
2011-12-21 07:37:54 -08:00
|
|
|
- [F]: convert a floating-point argument to OCaml syntax ([dddd.]
|
2005-09-20 10:18:03 -07:00
|
|
|
or [dddd.ddd] or [d.ddd e+-dd]).
|
2001-10-26 15:38:48 -07:00
|
|
|
- [e] or [E]: convert a floating-point argument to decimal notation,
|
2002-05-12 11:37:49 -07:00
|
|
|
in the style [d.ddd e+-dd] (mantissa and exponent).
|
2001-10-26 15:38:48 -07:00
|
|
|
- [g] or [G]: convert a floating-point argument to decimal notation,
|
2002-05-12 11:37:49 -07:00
|
|
|
in style [f] or [e], [E] (whichever is more compact).
|
2002-09-05 03:27:34 -07:00
|
|
|
- [B]: convert a boolean argument to the string [true] or [false]
|
2012-01-11 07:22:51 -08:00
|
|
|
- [b]: convert a boolean argument (deprecated; do not use in new
|
|
|
|
programs).
|
2002-07-28 14:46:07 -07:00
|
|
|
- [ld], [li], [lu], [lx], [lX], [lo]: convert an [int32] argument to
|
2001-10-28 06:21:27 -08:00
|
|
|
the format specified by the second letter (decimal, hexadecimal, etc).
|
|
|
|
- [nd], [ni], [nu], [nx], [nX], [no]: convert a [nativeint] argument to
|
|
|
|
the format specified by the second letter.
|
|
|
|
- [Ld], [Li], [Lu], [Lx], [LX], [Lo]: convert an [int64] argument to
|
|
|
|
the format specified by the second letter.
|
2012-01-11 07:22:51 -08:00
|
|
|
- [a]: user-defined printer. Take two arguments and apply the
|
2005-09-26 03:12:01 -07:00
|
|
|
first one to [outchan] (the current output channel) and to the
|
|
|
|
second argument. The first argument must therefore have type
|
2001-10-26 15:38:48 -07:00
|
|
|
[out_channel -> 'b -> unit] and the second ['b].
|
2005-09-26 03:12:01 -07:00
|
|
|
The output produced by the function is inserted in the output of
|
|
|
|
[fprintf] at the current point.
|
2012-01-11 07:22:51 -08:00
|
|
|
- [t]: same as [%a], but take only one argument (with type
|
2001-10-26 15:38:48 -07:00
|
|
|
[out_channel -> unit]) and apply it to [outchan].
|
2013-05-29 09:44:12 -07:00
|
|
|
- [\{ fmt %\}]: convert a format string argument to its type digest.
|
|
|
|
The argument must have the same type as the internal format string
|
|
|
|
[fmt].
|
2012-01-11 07:22:51 -08:00
|
|
|
- [( fmt %)]: format string substitution. Take a format string
|
|
|
|
argument and substitute it to the internal format string [fmt]
|
2005-09-26 03:12:01 -07:00
|
|
|
to print following arguments. The argument must have the same
|
2011-07-20 02:17:07 -07:00
|
|
|
type as the internal format string [fmt].
|
2003-04-28 02:44:21 -07:00
|
|
|
- [!]: take no argument and flush the output.
|
2001-10-26 15:38:48 -07:00
|
|
|
- [%]: take no argument and output one [%] character.
|
2012-01-11 07:22:51 -08:00
|
|
|
- [\@]: take no argument and output one [\@] character.
|
2012-04-11 11:00:32 -07:00
|
|
|
- [,]: take no argument and output nothing: a no-op delimiter for
|
|
|
|
conversion specifications.
|
2001-10-28 06:21:27 -08:00
|
|
|
|
2005-09-26 03:12:01 -07:00
|
|
|
The optional [flags] are:
|
2001-10-28 06:21:27 -08:00
|
|
|
- [-]: left-justify the output (default is right justification).
|
2002-05-25 01:37:33 -07:00
|
|
|
- [0]: for numerical conversions, pad with zeroes instead of spaces.
|
2012-01-11 07:22:51 -08:00
|
|
|
- [+]: for signed numerical conversions, prefix number with a [+]
|
|
|
|
sign if positive.
|
|
|
|
- space: for signed numerical conversions, prefix number with a
|
|
|
|
space if positive.
|
2014-10-15 06:34:58 -07:00
|
|
|
- [#]: request an alternate formatting style for the hexadecimal
|
|
|
|
and octal integer types ([x], [X], [o], [lx], [lX], [lo], [Lx],
|
|
|
|
[LX], [Lo]).
|
2001-10-28 06:21:27 -08:00
|
|
|
|
2005-09-26 03:12:01 -07:00
|
|
|
The optional [width] is an integer indicating the minimal
|
|
|
|
width of the result. For instance, [%6d] prints an integer,
|
|
|
|
prefixing it with spaces to fill at least 6 characters.
|
|
|
|
|
|
|
|
The optional [precision] is a dot [.] followed by an integer
|
|
|
|
indicating how many digits follow the decimal point in the [%f],
|
|
|
|
[%e], and [%E] conversions. For instance, [%.4f] prints a [float] with
|
|
|
|
4 fractional digits.
|
|
|
|
|
|
|
|
The integer in a [width] or [precision] can also be specified as
|
|
|
|
[*], in which case an extra integer argument is taken to specify
|
|
|
|
the corresponding [width] or [precision]. This integer argument
|
2008-01-11 08:13:18 -08:00
|
|
|
precedes immediately the argument to print.
|
|
|
|
For instance, [%.*f] prints a [float] with as many fractional
|
|
|
|
digits as the value of the argument given before the float. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
val printf : ('a, out_channel, unit) format -> 'a
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Same as {!Printf.fprintf}, but output on [stdout]. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
val eprintf : ('a, out_channel, unit) format -> 'a
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Same as {!Printf.fprintf}, but output on [stderr]. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
val sprintf : ('a, unit, string) format -> 'a
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Same as {!Printf.fprintf}, but instead of printing on an output channel,
|
2006-11-06 01:12:53 -08:00
|
|
|
return a string containing the result of formatting the arguments. *)
|
1999-02-25 02:26:38 -08:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Same as {!Printf.fprintf}, but instead of printing on an output channel,
|
|
|
|
append the formatted arguments to the given extensible buffer
|
|
|
|
(see module {!Buffer}). *)
|
1999-02-25 02:26:38 -08:00
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val ifprintf : 'b -> ('a, 'b, 'c, unit) format4 -> 'a
|
2012-04-11 11:00:32 -07:00
|
|
|
(** Same as {!Printf.fprintf}, but does not print anything.
|
|
|
|
Useful to ignore some material when conditionally printing.
|
|
|
|
@since 3.10.0
|
|
|
|
*)
|
|
|
|
|
2006-05-04 05:52:22 -07:00
|
|
|
(** Formatted output functions with continuations. *)
|
2008-09-27 13:50:01 -07:00
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val kfprintf : (out_channel -> 'd) -> out_channel ->
|
|
|
|
('a, out_channel, unit, 'd) format4 -> 'a
|
2005-07-22 05:21:02 -07:00
|
|
|
(** Same as [fprintf], but instead of returning immediately,
|
2010-05-21 11:30:12 -07:00
|
|
|
passes the out channel to its first argument at the end of printing.
|
|
|
|
@since 3.09.0
|
|
|
|
*)
|
2005-07-22 05:21:02 -07:00
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val ikfprintf : ('b -> 'd) -> 'b -> ('a, 'b, 'c, 'd) format4 -> 'a
|
2012-04-11 11:00:32 -07:00
|
|
|
(** Same as [kfprintf] above, but does not print anything.
|
|
|
|
Useful to ignore some material when conditionally printing.
|
|
|
|
@since 4.0
|
|
|
|
*)
|
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val ksprintf : (string -> 'd) -> ('a, unit, string, 'd) format4 -> 'a
|
2005-07-22 05:21:02 -07:00
|
|
|
(** Same as [sprintf] above, but instead of returning the string,
|
2010-05-21 11:30:12 -07:00
|
|
|
passes it to the first argument.
|
|
|
|
@since 3.09.0
|
|
|
|
*)
|
2005-07-22 05:21:02 -07:00
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val kbprintf : (Buffer.t -> 'd) -> Buffer.t ->
|
|
|
|
('a, Buffer.t, unit, 'd) format4 -> 'a
|
2006-05-04 05:52:22 -07:00
|
|
|
(** Same as [bprintf], but instead of returning immediately,
|
2010-05-21 11:30:12 -07:00
|
|
|
passes the buffer to its first argument at the end of printing.
|
|
|
|
@since 3.10.0
|
|
|
|
*)
|
2006-05-04 05:52:22 -07:00
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
(** Deprecated *)
|
|
|
|
|
2015-08-02 08:37:00 -07:00
|
|
|
val kprintf : (string -> 'b) -> ('a, unit, string, 'b) format4 -> 'a
|
2005-07-22 05:21:02 -07:00
|
|
|
(** A deprecated synonym for [ksprintf]. *)
|