Version française
Home     About     Download     Resources     Contact us    
Browse thread
Run-time evaluation of a Printf format string
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Teller <David.Teller@u...>
Subject: Re: [Caml-list] Run-time evaluation of a Printf format string
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.