- cur_arg_=0; dumped_=false;
- // maybe first arg is bound:
- if(bound_.size() != 0)
- {
- while(cur_arg_ < num_args_ && bound_[cur_arg_] ) ++cur_arg_;
- }
- return *this;
-}
-
-template< class Ch, class Tr>
-basic_format<Ch,Tr>& basic_format<Ch,Tr> ::clear_binds()
- // cancel all bindings, and clear()
-{
- bound_.resize(0);
- clear();
- return *this;
-}
-
-template< class Ch, class Tr>
-basic_format<Ch,Tr>& basic_format<Ch,Tr> ::clear_bind(int argN)
- // cancel the binding of ONE argument, and clear()
-{
- if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] )
- {
- if( exceptions() & io::out_of_range_bit )
- boost::throw_exception(io::out_of_range()); // arg not in range.
- else return *this;
- }
- bound_[argN-1]=false;
- clear();
- return *this;
-}
-
-
-
-template< class Ch, class Tr>
-std::basic_string<Ch,Tr> basic_format<Ch,Tr> ::str() const
-{
- dumped_=true;
- if(items_.size()==0)
- return prefix_;
- if( cur_arg_ < num_args_)
- if( exceptions() & io::too_few_args_bit )
- boost::throw_exception(io::too_few_args()); // not enough variables have been supplied !
-
- unsigned long sz = prefix_.size();
- unsigned long i;
- for(i=0; i < items_.size(); ++i)
- sz += items_[i].res_.size() + items_[i].appendix_.size();
- string_t res;
- res.reserve(sz);
-
- res += prefix_;
- for(i=0; i < items_.size(); ++i)
- {
- const format_item_t& item = items_[i];
- res += item.res_;
- if( item.argN_ == format_item_t::argN_tabulation)
- {
- BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
- std::streamsize n = item.state_.width_ - res.size();
- if( n > 0 )
- res.append( n, item.state_.fill_ );
+
+ template<class Ch, class Tr, class Alloc>
+ void basic_format<Ch, Tr, Alloc>::
+ make_or_reuse_data (std::size_t nbitems) {
+#if !defined(BOOST_NO_STD_LOCALE)
+ Ch fill = ( BOOST_USE_FACET(std::ctype<Ch>, getloc()) ). widen(' ');
+#else
+ Ch fill = ' ';
+#endif
+ if(items_.size() == 0)
+ items_.assign( nbitems, format_item_t(fill) );
+ else {
+ if(nbitems>items_.size())
+ items_.resize(nbitems, format_item_t(fill));
+ bound_.resize(0);
+ for(std::size_t i=0; i < nbitems; ++i)
+ items_[i].reset(fill); // strings are resized, instead of reallocated
+ }
+ prefix_.resize(0);
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
+ clear () {
+ // empty the string buffers (except bound arguments)
+ // and make the format object ready for formatting a new set of arguments
+
+ BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
+
+ for(unsigned long i=0; i<items_.size(); ++i) {
+ // clear converted strings only if the corresponding argument is not bound :
+ if( bound_.size()==0 || items_[i].argN_<0 || !bound_[ items_[i].argN_ ] )
+ items_[i].res_.resize(0);
+ }
+ cur_arg_=0; dumped_=false;
+ // maybe first arg is bound:
+ if(bound_.size() != 0) {
+ for(; cur_arg_ < num_args_ && bound_[cur_arg_]; ++cur_arg_)
+ {}
+ }
+ return *this;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
+ clear_binds () {
+ // remove all binds, then clear()
+ bound_.resize(0);
+ clear();
+ return *this;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
+ clear_bind (int argN) {
+ // remove the bind of ONE argument then clear()
+ if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] ) {
+ if( exceptions() & io::out_of_range_bit)
+ boost::throw_exception(io::out_of_range(argN, 1, num_args_+1 ) );
+ else return *this;
+ }
+ bound_[argN-1]=false;
+ clear();
+ return *this;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ int basic_format<Ch,Tr, Alloc>::
+ bound_args() const {
+ if(bound_.size()==0)
+ return 0;
+ int n=0;
+ for(int i=0; i<num_args_ ; ++i)
+ if(bound_[i])
+ ++n;
+ return n;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ int basic_format<Ch,Tr, Alloc>::
+ fed_args() const {
+ if(bound_.size()==0)
+ return cur_arg_;
+ int n=0;
+ for(int i=0; i<cur_arg_ ; ++i)
+ if(!bound_[i])
+ ++n;
+ return n;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ int basic_format<Ch,Tr, Alloc>::
+ cur_arg() const {
+ return cur_arg_+1; }
+
+ template< class Ch, class Tr, class Alloc>
+ int basic_format<Ch,Tr, Alloc>::
+ remaining_args() const {
+ if(bound_.size()==0)
+ return num_args_-cur_arg_;
+ int n=0;
+ for(int i=cur_arg_; i<num_args_ ; ++i)
+ if(!bound_[i])
+ ++n;
+ return n;
+ }
+
+ template< class Ch, class Tr, class Alloc>
+ typename basic_format<Ch, Tr, Alloc>::string_type
+ basic_format<Ch,Tr, Alloc>::
+ str () const {
+ if(items_.size()==0)
+ return prefix_;
+ if( cur_arg_ < num_args_)
+ if( exceptions() & io::too_few_args_bit )
+ // not enough variables supplied
+ boost::throw_exception(io::too_few_args(cur_arg_, num_args_));
+
+ unsigned long i;
+ string_type res;
+ res.reserve(size());
+ res += prefix_;
+ for(i=0; i < items_.size(); ++i) {
+ const format_item_t& item = items_[i];
+ res += item.res_;
+ if( item.argN_ == format_item_t::argN_tabulation) {
+ BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
+ if( static_cast<size_type>(item.fmtstate_.width_) > res.size() )
+ res.append( static_cast<size_type>(item.fmtstate_.width_) - res.size(),
+ item.fmtstate_.fill_ );
+ }
+ res += item.appendix_;
+ }
+ dumped_=true;
+ return res;
+ }
+ template< class Ch, class Tr, class Alloc>
+ typename std::basic_string<Ch, Tr, Alloc>::size_type basic_format<Ch,Tr, Alloc>::
+ size () const {
+#ifdef BOOST_MSVC
+ // If std::min<unsigned> or std::max<unsigned> are already instantiated
+ // at this point then we get a blizzard of warning messages when we call
+ // those templates with std::size_t as arguments. Weird and very annoyning...
+#pragma warning(push)
+#pragma warning(disable:4267)
+#endif
+ BOOST_USING_STD_MAX();
+ size_type sz = prefix_.size();
+ unsigned long i;
+ for(i=0; i < items_.size(); ++i) {
+ const format_item_t& item = items_[i];
+ sz += item.res_.size();
+ if( item.argN_ == format_item_t::argN_tabulation)
+ sz = max BOOST_PREVENT_MACRO_SUBSTITUTION (sz,
+ static_cast<size_type>(item.fmtstate_.width_) );
+ sz += item.appendix_.size();
+ }
+ return sz;
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif