summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Weis <Pierre.Weis@inria.fr>2006-11-06 09:12:53 +0000
committerPierre Weis <Pierre.Weis@inria.fr>2006-11-06 09:12:53 +0000
commit044ac2610c4050373d996f588f977f0692779b9f (patch)
tree0203480c5c0d0cff95c8629f591d714a35aaa851
parent84b420f2fe51049d8512b75451e29f2c91215890 (diff)
New function ifprintf that does not print. (See request #0004031.)
Ifprintf is a one-liner using implementation tools from Printf, whereas it is not possible to write it in pure Caml. Useful to conditionally ignore printing. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@7717 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r--stdlib/format.ml1
-rw-r--r--stdlib/format.mli4
-rw-r--r--stdlib/printf.ml2
-rw-r--r--stdlib/printf.mli7
-rw-r--r--stdlib/sys.ml2
5 files changed, 13 insertions, 3 deletions
diff --git a/stdlib/format.ml b/stdlib/format.ml
index 2eb8f6760..f5deb0674 100644
--- a/stdlib/format.ml
+++ b/stdlib/format.ml
@@ -1193,6 +1193,7 @@ let mkprintf to_s get_out =
**************************************************************)
let kfprintf k ppf = mkprintf false (fun _ -> ppf) k;;
+let ifprintf ppf = Printf.kapr (fun _ -> Obj.magic ignore);;
let fprintf ppf = kfprintf ignore ppf;;
let printf fmt = fprintf std_formatter fmt;;
diff --git a/stdlib/format.mli b/stdlib/format.mli
index 04d47080e..c39788495 100644
--- a/stdlib/format.mli
+++ b/stdlib/format.mli
@@ -656,6 +656,10 @@ val kfprintf : (formatter -> 'a) -> formatter ->
(** Same as [fprintf] above, but instead of returning immediately,
passes the formatter to its first argument at the end of printing. *)
+val ifprintf : formatter -> ('a, formatter, unit) format -> 'a;;
+(** Same as [fprintf] above, but does not print anything.
+ Useful to ignore some material when conditionally printing. *)
+
val ksprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b;;
(** Same as [sprintf] above, but instead of returning the string,
passes it to the first argument. *)
diff --git a/stdlib/printf.ml b/stdlib/printf.ml
index 677ad6323..1fbc0c145 100644
--- a/stdlib/printf.ml
+++ b/stdlib/printf.ml
@@ -523,6 +523,8 @@ let mkprintf to_s get_out outc outs flush k fmt =
let kfprintf k oc =
mkprintf false (fun _ -> oc) output_char output_string flush k;;
+let ifprintf oc = kapr (fun _ -> Obj.magic ignore);;
+
let fprintf oc = kfprintf ignore oc;;
let printf fmt = fprintf stdout fmt;;
let eprintf fmt = fprintf stderr fmt;;
diff --git a/stdlib/printf.mli b/stdlib/printf.mli
index 5995a6ba1..b28a8f6df 100644
--- a/stdlib/printf.mli
+++ b/stdlib/printf.mli
@@ -113,10 +113,13 @@ val printf : ('a, out_channel, unit) format -> 'a
val eprintf : ('a, out_channel, unit) format -> 'a
(** Same as {!Printf.fprintf}, but output on [stderr]. *)
+val ifprintf : 'a -> ('b, 'a, unit) format -> 'b
+(** Same as {!Printf.fprintf}, but does not print anything.
+ Useful to ignore some material when conditionally printing. *)
+
val sprintf : ('a, unit, string) format -> 'a
(** Same as {!Printf.fprintf}, but instead of printing on an output channel,
- return a string containing the result of formatting
- the arguments. *)
+ return a string containing the result of formatting the arguments. *)
val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
(** Same as {!Printf.fprintf}, but instead of printing on an output channel,
diff --git a/stdlib/sys.ml b/stdlib/sys.ml
index 50845d9f1..da1b69e89 100644
--- a/stdlib/sys.ml
+++ b/stdlib/sys.ml
@@ -78,4 +78,4 @@ let catch_break on =
(* OCaml version string, must be in the format described in sys.mli. *)
-let ocaml_version = "3.10+dev14 (2006-10-27)";;
+let ocaml_version = "3.10+dev15 (2006-11-05)";;