Boost logo

Boost :

Subject: Re: [boost] Boost::Format add-on
From: Ryan Gallagher (ryan.gallagher_at_[hidden])
Date: 2009-03-18 04:55:48


joel falcou <joel.falcou <at> u-psud.fr> writes:
>
>
> > I'm not really as antsy for the change as Joel may be. But if he
> > doesn't mind I'd be happy to post a patch to trac in the next day
> or so.
>
> I can do the patch, we just have to settle on the method
> name :)

Well, I guess I gave my vote, but I'm open to other suggestions as
well.

I didn't see your message until now and already have a patch using
my suggested names. You can use it if filing a trac item, otherwise
feel free to modify or redo if the names change.

-Ryan

PS -- Another possible accessor is num_items, perhaps useful in
conjunction with modify_item, though I don't really see much of a
use-case for it so left it out.

Index: boost/format/format_class.hpp
===================================================================
--- boost/format/format_class.hpp (revision 51826)
+++ boost/format/format_class.hpp (working copy)
@@ -50,12 +50,22 @@
 #endif
         io::detail::locale_t getloc() const;
 
+ // The total number of arguments expected to be passed to the format
string.
+ int num_expected_args() const
+ { return num_args_; }
+ // The number of arguments currently bound to the format string.
+ int num_bound_args() const
+ { return cur_arg_; }
+ // The number of arguments still required to be passed to the format
string.
+ int num_remaining_args() const
+ { return num_expected_args() - num_bound_args(); }
+
         basic_format& clear(); // empty all converted string buffers
(except bound items)
         basic_format& clear_binds(); // unbind all bound items, and call
clear()
         basic_format& parse(const string_type&); // resets buffers and parse a
new format string
 
         // ** formatted result ** //
- size_type size() const; // sum of the current string pieces sizes
+ size_type size() const; // The number of characters in the
formatted result string.
         string_type str() const; // final string
 
         // ** arguments passing ** //
@@ -127,7 +137,7 @@
         std::vector<format_item_t> items_; // each '%..' directive leads to a
format_item
         std::vector<bool> bound_; // stores which arguments were bound. size()
== 0 || num_args
 
- int style_; // style of format-string : positional or
not, etc
+ int style_; // style of format-string : positional or
not, etc
         int cur_arg_; // keep track of wich argument is current
         int num_args_; // number of expected arguments
         mutable bool dumped_; // true only after call to str() or <<
Index: libs/format/test/format_test3.cpp
===================================================================
--- libs/format/test/format_test3.cpp (revision 51826)
+++ libs/format/test/format_test3.cpp (working copy)
@@ -93,6 +93,31 @@
       BOOST_ERROR("nesting did not work");
     }
 
+ // observers
+ BOOST_CHECK_EQUAL(format("foo").num_expected_args(), 0);
+ BOOST_CHECK_EQUAL(format("foo").num_bound_args(), 0);
+ BOOST_CHECK_EQUAL(format("foo").num_remaining_args(), 0);
+
+ BOOST_CHECK_EQUAL(format("foo%s").num_expected_args(), 1);
+ BOOST_CHECK_EQUAL(format("foo%s").num_bound_args(), 0);
+ BOOST_CHECK_EQUAL(format("foo%s").num_remaining_args(), 1);
+
+ BOOST_CHECK_EQUAL((format("foo%s") % "bar").num_expected_args(), 1);
+ BOOST_CHECK_EQUAL((format("foo%s") % "bar").num_bound_args(), 1);
+ BOOST_CHECK_EQUAL((format("foo%s") % "bar").num_remaining_args(), 0);
+
+ BOOST_CHECK_EQUAL((format("%2%%1%") % "bar" % "foo").num_expected_args(),
2);
+ BOOST_CHECK_EQUAL((format("%2%%1%") % "bar" % "foo").num_bound_args(), 2);
+ BOOST_CHECK_EQUAL((format("%2%%1%") % "bar" % "foo").num_remaining_args(),
0);
+
+ BOOST_CHECK_EQUAL((format("%1$s %2$s %1$s") % "bar").num_expected_args(),
2);
+ BOOST_CHECK_EQUAL((format("%1$s %2$s %1$s") % "bar").num_bound_args(), 1);
+ BOOST_CHECK_EQUAL((format("%1$s %2$s %1$s") % "bar").num_remaining_args(),
1);
+
+ BOOST_CHECK_EQUAL((format("%1%, %2%, %|40t|%3%\n") % "foo" %
"bar").num_expected_args(), 3);
+ BOOST_CHECK_EQUAL((format("%1%, %2%, %|40t|%3%\n") % "foo" %
"bar").num_bound_args(), 2);
+ BOOST_CHECK_EQUAL((format("%1%, %2%, %|40t|%3%\n") % "foo" %
"bar").num_remaining_args(), 1);
+
     // testcase for bug reported at
     // http://lists.boost.org/boost-users/2006/05/19723.php
     format f("%40t%1%");
 


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk