Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Run-time evaluation of a Printf format string

13 views
Skip to first unread message

Jan Kybic

unread,
Aug 28, 2008, 9:38:41 AM8/28/08
to caml...@yquem.inria.fr
Hello,
I would need an equivalent of Printf.sprintf where the
format string is not constant, it is read from the command line.
The motivation is to let the user specify a template for file names,
such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
Pervasives.string_of_format only accepts constant strings.
Or is there some external library useful for this task?

For the moment I have started to implement it myself for the limited
set of format specifications I will need but if there is some more
elegant solution I would be interested to hear about it.

Thanks,

Jan

--
-------------------------------------------------------------------------
Jan Kybic <ky...@fel.cvut.cz> tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic ICQ 200569450
D

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

David Teller

unread,
Aug 28, 2008, 10:10:24 AM8/28/08
to Jan Kybic, caml...@yquem.inria.fr
As you may have seen in Pervasives.ml, format6 and string are actually
the same thing. Which means that you can Obj.magic your way around the
problem (gasp!) -- provided you have already checked manually that the
string actually represents the format you're interested in.

Of course, that's a tad risky. If I were you, I'd rather re-implement a
small unparsing language with a format comparable to printf's and a tiny
parser to go with it.

Does this help?

Cheers,
David

P.S.:
If you're interested, I've put together a little bit of documentation
on format* which I think can't be found in OCaml's doc [1]. Look for
"{7 Format4}" in the comments.

[1]
https://forge.ocamlcore.org/plugins/scmsvn/viewcvs.php/trunk/extlib/IO.mli?rev=23&root=batteries&view=auto

On Thu, 2008-08-28 at 15:37 +0200, Jan Kybic wrote:
> Hello,
> I would need an equivalent of Printf.sprintf where the
> format string is not constant, it is read from the command line.
> The motivation is to let the user specify a template for file names,
> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
> Pervasives.string_of_format only accepts constant strings.
> Or is there some external library useful for this task?
>
> For the moment I have started to implement it myself for the limited
> set of format specifications I will need but if there is some more
> elegant solution I would be interested to hear about it.
>
> Thanks,
>
> Jan
>
--

David Teller-Rajchenbach
Security of Distributed Systems
http://www.univ-orleans.fr/lifo/Members/David.Teller
Angry researcher: French Universities need reforms, but the LRU act brings liquidations.

Edgar Friendly

unread,
Aug 28, 2008, 10:58:32 AM8/28/08
to Jan Kybic, caml...@yquem.inria.fr
Jan Kybic wrote:
> Hello,
> I would need an equivalent of Printf.sprintf where the
> format string is not constant, it is read from the command line.
> The motivation is to let the user specify a template for file names,
> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
> Pervasives.string_of_format only accepts constant strings.
> Or is there some external library useful for this task?
>
> For the moment I have started to implement it myself for the limited
> set of format specifications I will need but if there is some more
> elegant solution I would be interested to hear about it.
>
> Thanks,
>
> Jan
>

What arguments will you pass to your sprintf command? Just a single
integer? Or do you have multiple values that can be inserted into the
template.

For templating like this, you probably want the different %x's to refer
to different values in your program (i.e. %d - count number, %w - width
of image, %h - height of image, etc). For this use, you'll have to
write your own handler of the input "format" strings, and you won't rely
on sprintf to do the work.

E

Dave Benjamin

unread,
Aug 28, 2008, 11:02:17 AM8/28/08
to Jan Kybic, caml...@yquem.inria.fr
Jan Kybic wrote:
> I would need an equivalent of Printf.sprintf where the
> format string is not constant, it is read from the command line.
> The motivation is to let the user specify a template for file names,
> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
> Pervasives.string_of_format only accepts constant strings.
> Or is there some external library useful for this task?

Does it have to be printf-style formatting? If you can switch to a
syntax like $(foo), you can use Buffer.add_substitute:

# Buffer.add_substitute;;
- : Buffer.t -> (string -> string) -> string -> unit = <fun>
# let buffer = Buffer.create 0;;
val buffer : Buffer.t = <abstr>
# let vars = ["who", "world"];;
val vars : (string * string) list = [("who", "world")]
# Buffer.add_substitute buffer
(fun name -> List.assoc name vars)
"hello $(who)";;
- : unit = ()
# Buffer.contents buffer;;
- : string = "hello world"


Dave

Stéphane Glondu

unread,
Aug 28, 2008, 12:01:49 PM8/28/08
to Jan Kybic, caml...@yquem.inria.fr
Jan Kybic wrote:
> I would need an equivalent of Printf.sprintf where the
> format string is not constant, it is read from the command line.
> The motivation is to let the user specify a template for file names,
> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
> Pervasives.string_of_format only accepts constant strings.
> Or is there some external library useful for this task?

What about Scanf.format_from_string?


Cheers,

--
Stéphane

Jan Kybic

unread,
Aug 29, 2008, 3:35:40 AM8/29/08
to caml...@yquem.inria.fr
> Jan Kybic wrote:
>> I would need an equivalent of Printf.sprintf where the
>> format string is not constant, it is read from the command line.
>> The motivation is to let the user specify a template for file names,
>> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
>> Pervasives.string_of_format only accepts constant strings.
>> Or is there some external library useful for this task?
>

> Stéphane Glondu <st...@glondu.net> writes:
> What about Scanf.format_from_string?

> Dave Benjamin <da...@ramenlabs.com> writes:
> Does it have to be printf-style formatting? If you can switch to a
> syntax like $(foo), you can use Buffer.add_substitute:

Thank you for all your suggestions. Here is my summary:

- If I only want to insert a suitably formatted sequence number,
the simplest solution is using Scanf.format_from_string, such as

let format = "img%03d.png" in
Printf.sprintf (Scanf.format_from_string format "%d") num

- If I want to be able to insert several different variables
(e.g. sequence number, image height and width) but do not
have to specify formatting (like padding zeroes), then
Buffer.add_substitute is easy enough to use.

- If I want both to insert several different variables and to
specify the formatting, I will have to implement my own parser and
interpreter of format strings.

Jan

--
-------------------------------------------------------------------------
Jan Kybic <ky...@fel.cvut.cz> tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic ICQ 200569450

_______________________________________________

0 new messages