You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Martin Sebor <se...@roguewave.com> on 2007/01/29 04:01:58 UTC

new formatting directive: %{@} (was: [Fwd: svn commit: r500920 - in /incubator/stdcxx/trunk/tests: self/0.printf.cpp src/printf.cpp])

This change implements a new formatting directive, %{@}, along
with an extension to the %{$<param>OP[<word>|*|@]} directive.

The effect of the %{@} directive in an ordinary format string
is to extract a pointer to a character string from the argument
list and process it using the same argument list before moving
on to the next character in the original format string.

For example, the following are all equivalent:

   rw_printf ("%s\n", "Hello, World!");
   rw_printf ("%{@}", "%s\n", "Hello, World!");
   rw_printf ("%{@}", "%{@}\n", "%s", "Hello, World!");
   rw_printf ("%{@}", "%{@}", "%{@}\n", "%s", "Hello, World!");

The purpose of the directive is to make it easy to parametrize
the formatting in template code. For example:

   template <class T>
   void print (T x, const char *fmt) {
       rw_printf ("the value of x = %{@}\n", fmt, x);
   }

   int main () {
       print (1234, "%i");   // format 1234 as an integer
       print (3.14, "%g");   // format 3.14 as a double
   }


The effect of '@' in %{$<param>OP[<word>|*|@]}, i.e., the effect
of %{$<param>OP@}, is to extract a formatting string from the
argument list, process it, and assign the result (the formatted
string) to <param> according the rules of OP. For example, the
effect of this code

     rw_printf("%{$FOO!:@}, FOO=%{$FOO}\n", "%s", "bar");

is to set the environment variable FOO to bar and print out on
standard output the following line of text:

bar, FOO=bar

The purpose of this enhancement is to make it easy to preformat
a string containing a description of a call to a tested function
along with the values of its arguments, save it in an environment
variable, and then use this preformatted description in
subsequent diagnostic messages.

Martin

-------- Original Message --------
Subject: svn commit: r500920 - in /incubator/stdcxx/trunk/tests: 
self/0.printf.cpp src/printf.cpp
Date: Mon, 29 Jan 2007 01:58:11 -0000
From: sebor@apache.org
Reply-To: stdcxx-dev@incubator.apache.org
To: stdcxx-commits@incubator.apache.org

Author: sebor
Date: Sun Jan 28 17:58:10 2007
New Revision: 500920

URL: http://svn.apache.org/viewvc?view=rev&rev=500920
Log:
2007-01-28  Martin Sebor  <se...@roguewave.com>

	* printf.cpp (_rw_fmtspec): Avoided handling '$' when it's immediately
	followed by a closing curly brace ('}'). Ditto for '@'.
	(_rw_pvasnprintf): New. Implements the guts of rw_vasnprintf without
	NUL-terminating the buffer to make it easier to call it recursively.
	(_rw_vasnprintf_ext): Recognized the new %{@} directive (nested format
	specifier). Called _rw_fmtexpr to handle it.
	(_rw_fmtexpr): Handled the %{@} directive (nested format specifier).
	* test/printf.cpp (test_envvar): Added test cases exercising
	unconditional assignment (the %{$<param>!:<word>} directive).
	(test_nested_format): Exercised the %{@} directive.

Modified:
     incubator/stdcxx/trunk/tests/self/0.printf.cpp
     incubator/stdcxx/trunk/tests/src/printf.cpp