From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 4F669BB84 for ; Thu, 28 Aug 2008 16:10:15 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmgDAJhOtkjCpx6viGdsb2JhbACSOwEBAQ8gp3mBaQ X-IronPort-AV: E=Sophos;i="4.32,286,1217800800"; d="scan'208";a="14348270" Received: from smtpka.univ-orleans.fr (HELO ka.univ-orleans.fr) ([194.167.30.175]) by mail2-smtp-roc.national.inria.fr with ESMTP; 28 Aug 2008 16:10:15 +0200 Received: from smtps.univ-orleans.fr (localhost [127.0.0.1]) by ka.univ-orleans.fr (Postfix) with ESMTP id EC07512AD57; Thu, 28 Aug 2008 16:10:14 +0200 (CEST) Received: from [192.168.0.12] (ras75-4-82-235-58-110.fbx.proxad.net [82.235.58.110]) by smtps.univ-orleans.fr (Postfix) with ESMTP id 084F936E60; Thu, 28 Aug 2008 16:10:14 +0200 (CEST) Subject: Re: [Caml-list] Run-time evaluation of a Printf format string From: David Teller To: Jan Kybic Cc: caml-list@yquem.inria.fr In-Reply-To: <87hc951cr9.fsf@fel.cvut.cz> References: <87hc951cr9.fsf@fel.cvut.cz> Content-Type: text/plain Date: Thu, 28 Aug 2008 16:10:22 +0200 Message-Id: <1219932623.7195.20.camel@Blefuscu> Mime-Version: 1.0 X-Mailer: Evolution 2.12.1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; run-time:01 printf:01 univ-orleans:01 pervasives:01 re-implement:01 unparsing:01 parser:01 cheers:01 ocaml's:01 viewcvs:01 mli:01 0200,:01 printf:01 sprintf:01 %03:01 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.