Permalink
Browse files

For %{..%}, restore Pierre's semantics

(printf {%foo%} bar) will print the string representation of the
format type of both `foo` and `bar`, instead of printing `bar`
(for this purpose one can just use %s). `bar` content is ignored, but
the typer should check that its type is compatible with the one of
`foo`.

This semantics allows to use (printf %{..%}) for testing/debugging the
use of %(...%): put in the brackets what you believe to be the format
type you want to use, and as argument the format you wish to pass, and
you'll get type-checking confidence and the "canonical" representation
of the format string which you can use in the %(...%) -- note that
using the canonical format type is not mandatory.

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14840 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
  • Loading branch information...
1 parent 02c5fb4 commit f0e39fee7dc703c4e6901487f51e0735ef00bee8 @gasche gasche committed May 12, 2014
@@ -1099,11 +1099,10 @@ fun k o acc fmt -> match fmt with
make_printf k o (Acc_char (acc, chr)) rest
| Format_arg (_, sub_fmtty, rest) ->
- if legacy_behavior then
- let ty = string_of_fmtty sub_fmtty in
- (fun _str ->
- make_printf k o (Acc_string (acc, ty)) rest)
- else (fun (_, str) -> make_printf k o (Acc_string (acc, str)) rest)
+ let ty = string_of_fmtty sub_fmtty in
+ (fun str ->
+ ignore str;
+ make_printf k o (Acc_string (acc, ty)) rest)
| Format_subst (_, _, fmtty, rest) ->
(* Call to type_format can't fail (raise Type_mismatch). *)
fun (fmt, _) -> make_printf k o acc
@@ -61,10 +61,9 @@ test (test2 ());;
(* Testing meta format string printing. *)
let test3 () =
-(* >> Now works as expected. *)
-(*sprintf "%{toto %s titi.\n%}" "Bonjour %s." = "%s" &&*)
-sprintf "%{toto %s titi.\n%}" "Bonjour %s." = "Bonjour %s." &&
-sprintf "%{%d%s%}" "kk%dkk%s\n" = "kk%dkk%s\n";;
+ sprintf "%{toto %S titi.\n%}" "Bonjour %S." = "%s" &&
+ sprintf "%{Bonjour %S.%}" "toto %S titi.\n" = "%s"
+;;
test (test3 ());;
(* Testing meta format string arguments. *)
@@ -41,7 +41,7 @@ try
test (sprintf "%+d/%+i" (-42) (-43) = "-42/-43");
test (sprintf "% d/% i" (-42) (-43) = "-42/-43");
(*test (sprintf "%#d/%#i" (-42) (-43) = "-42/-43");*)
- (* >> '#' is incompatilbe with 'd' *)
+ (* >> '#' is incompatible with 'd' *)
test (sprintf "%4d/%5i" (-42) (-43) = " -42/ -43");
test (sprintf "%*d/%*i" 4 (-42) 5 (-43) = " -42/ -43");
(*test (sprintf "%-0+ #4d/%-0+ #5i" (-42) (-43) = "-42 /-43 ");*)
@@ -60,7 +60,7 @@ try
test (sprintf "%4u" 42 = " 42");
test (sprintf "%*u" 4 42 = " 42");
(*test (sprintf "%-0+ #6d" 42 = "+42 ");*)
- (* >> '-' is incompatible with '0', '#' is incompatilbe with 'd' *)
+ (* >> '-' is incompatible with '0', '#' is incompatible with 'd' *)
printf "\nu negative\n%!";
begin match Sys.word_size with
@@ -552,7 +552,7 @@ try
printf "\n{...%%}\n%!";
let f = format_of_string "%4g/%s" in
- test (sprintf "%{%#0F%S%}" f = "%f%s");
+ test (sprintf "%{%.4F%5S%}" f = "%f%s");
printf "\n(...%%)\n%!";
let f = format_of_string "%d/foo/%s" in
@@ -1090,13 +1090,13 @@ let test46, test47 =
Printf.sprintf "%i %(%s%)."
1 "spells one, %s" "in english"),
(fun () ->
- Printf.sprintf "%i ,%{%s%}, %s."
+ Printf.sprintf "%i %{%s%}, %s."
1 "spells one %s" "in english")
;;
test (test46 () = "1 spells one, in english.")
;;
-test (test47 () = "1 ,spells one %s, in english.")
+test (test47 () = "1 %s, in english.")
;;
(* Testing scanning of meta formats. *)

0 comments on commit f0e39fe

Please sign in to comment.