Update contrib.
1 // ----------------------------------------------------------------------------
2 // parsing.hpp : implementation of the parsing member functions
3 // ( parse, parse_printf_directive)
4 // ----------------------------------------------------------------------------
6 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
7 // subject to the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 // see http://www.boost.org/libs/format for library home page
12 // ----------------------------------------------------------------------------
14 #ifndef BOOST_FORMAT_PARSING_HPP
15 #define BOOST_FORMAT_PARSING_HPP
18 #include <boost/format/format_class.hpp>
19 #include <boost/throw_exception.hpp>
20 #include <boost/assert.hpp>
27 #if defined(BOOST_NO_STD_LOCALE)
28 // streams will be used for narrow / widen. but these methods are not const
30 T& const_or_not(const T& x) {
31 return const_cast<T&> (x);
35 const T& const_or_not(const T& x) {
40 template<class Ch, class Facet> inline
41 char wrap_narrow(const Facet& fac, Ch c, char deflt) {
42 return const_or_not(fac).narrow(c, deflt);
45 template<class Ch, class Facet> inline
46 bool wrap_isdigit(const Facet& fac, Ch c) {
47 #if ! defined( BOOST_NO_LOCALE_ISDIGIT )
48 return fac.is(std::ctype<Ch>::digit, c);
55 template<class Iter, class Facet>
56 Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) {
58 for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ;
63 // Input : [start, last) iterators range and a
64 // a Facet to use its widen/narrow member function
65 // Effects : read sequence and convert digits into integral n, of type Res
67 template<class Res, class Iter, class Facet>
68 Iter str2int (const Iter & start, const Iter & last, Res & res,
74 for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) {
75 char cur_ch = wrap_narrow(fac, *it, 0); // cant fail.
77 res += cur_ch - '0'; // 22.2.1.1.2.13 of the C++ standard
82 // skip printf's "asterisk-fields" directives in the format-string buf
83 // Input : char string, with starting index *pos_p
84 // a Facet merely to use its widen/narrow member function
85 // Effects : advance *pos_p by skipping printf's asterisk fields.
87 template<class Iter, class Facet>
88 Iter skip_asterisk(Iter start, Iter last, const Facet& fac)
92 start = wrap_scan_notdigit(fac, start, last);
93 if(start!=last && *start== const_or_not(fac).widen( '$') )
99 // auxiliary func called by parse_printf_directive
100 // for centralising error handling
101 // it either throws if user sets the corresponding flag, or does nothing.
102 inline void maybe_throw_exception(unsigned char exceptions,
103 std::size_t pos, std::size_t size)
105 if(exceptions & io::bad_format_string_bit)
106 boost::throw_exception(io::bad_format_string(pos, size) );
110 // Input: the position of a printf-directive in the format-string
111 // a basic_ios& merely to use its widen/narrow member function
112 // a bitset'exceptions' telling whether to throw exceptions on errors.
114 // true if parse succeeded (ignore some errors if exceptions disabled)
115 // false if it failed so bad that the directive should be printed verbatim
117 // start is incremented so that *start is the first char after
119 // *fpar is set with the parameters read in the directive
120 template<class Ch, class Tr, class Alloc, class Iter, class Facet>
121 bool parse_printf_directive(Iter & start, const Iter& last,
122 detail::format_item<Ch, Tr, Alloc> * fpar,
124 std::size_t offset, unsigned char exceptions)
126 typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
128 fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive
129 bool precision_set = false;
130 bool in_brackets=false;
132 std::size_t fstring_size = last-start0+offset;
133 if(*start== const_or_not(fac).widen( '|')) {
135 if( ++start >= last ) {
136 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
141 // the flag '0' would be picked as a digit for argument order, but here it's a flag :
142 if(*start== const_or_not(fac).widen( '0'))
145 // handle argument order (%2$d) or possibly width specification: %2d
146 if(wrap_isdigit(fac, *start)) {
148 start = str2int(start, last, n, fac);
149 if( start >= last ) {
150 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
154 // %N% case : this is already the end of the directive
155 if( *start == const_or_not(fac).widen( '%') ) {
159 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
160 // but don't return. maybe "%" was used in lieu of '$', so we go on.
165 if ( *start== const_or_not(fac).widen( '$') ) {
170 // non-positionnal directive
171 fpar->fmtstate_.width_ = n;
172 fpar->argN_ = format_item_t::argN_no_posit;
173 goto parse_precision;
179 while ( start != last) { // as long as char is one of + - = _ # 0 l h or ' '
181 switch ( wrap_narrow(fac, *start, 0)) {
182 case '\'' : break; // no effect yet. (painful to implement)
184 case 'h': // short/long modifier : for printf-comaptibility (no action needed)
187 fpar->fmtstate_.flags_ |= std::ios_base::left;
190 fpar->pad_scheme_ |= format_item_t::centered;
193 fpar->fmtstate_.flags_ |= std::ios_base::internal;
196 fpar->pad_scheme_ |= format_item_t::spacepad;
199 fpar->fmtstate_.flags_ |= std::ios_base::showpos;
202 fpar->pad_scheme_ |= format_item_t::zeropad;
203 // need to know alignment before really setting flags,
204 // so just add 'zeropad' flag for now, it will be processed later.
207 fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
216 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
221 // first skip 'asterisk fields' : *, or *N$
222 if(*start == const_or_not(fac).widen( '*') )
223 start = skip_asterisk(start, last, fac);
224 if(start!=last && wrap_isdigit(fac, *start))
225 start = str2int(start, last, fpar->fmtstate_.width_, fac);
229 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
232 // handle precision spec
233 if (*start== const_or_not(fac).widen( '.')) {
235 if(start != last && *start == const_or_not(fac).widen( '*') )
236 start = skip_asterisk(start, last, fac);
237 if(start != last && wrap_isdigit(fac, *start)) {
238 start = str2int(start, last, fpar->fmtstate_.precision_, fac);
239 precision_set = true;
242 fpar->fmtstate_.precision_ =0;
245 // handle formatting-type flags :
246 while( start != last && ( *start== const_or_not(fac).widen( 'l')
247 || *start== const_or_not(fac).widen( 'L')
248 || *start== const_or_not(fac).widen( 'h')) )
251 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
255 if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
259 switch ( wrap_narrow(fac, *start, 0) ) {
261 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
262 case 'p': // pointer => set hex.
264 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
265 fpar->fmtstate_.flags_ |= std::ios_base::hex;
269 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
270 fpar->fmtstate_.flags_ |= std::ios_base::oct;
274 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
276 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
277 fpar->fmtstate_.flags_ |= std::ios_base::scientific;
279 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
280 fpar->fmtstate_.flags_ |= std::ios_base::dec;
284 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
285 fpar->fmtstate_.flags_ |= std::ios_base::fixed;
289 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
290 fpar->fmtstate_.flags_ |= std::ios_base::dec;
296 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
298 fpar->fmtstate_.fill_ = *start;
299 fpar->pad_scheme_ |= format_item_t::tabulation;
300 fpar->argN_ = format_item_t::argN_tabulation;
303 fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
304 fpar->pad_scheme_ |= format_item_t::tabulation;
305 fpar->argN_ = format_item_t::argN_tabulation;
309 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
311 case 'g': // 'g' conversion is default for floats.
312 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
313 fpar->fmtstate_.flags_ |= std::ios_base::dec;
315 // CLEAR all floatield flags, so stream will CHOOSE
316 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
325 if(precision_set) // handle truncation manually, with own parameter.
326 fpar->truncate_ = fpar->fmtstate_.precision_;
327 fpar->fmtstate_.precision_ = 6; // default stream precision.
330 fpar->argN_ = format_item_t::argN_ignored;
333 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
338 if( start != last && *start== const_or_not(fac).widen( '|') ) {
342 else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
346 // -end parse_printf_directive()
348 template<class String, class Facet>
349 int upper_bound_from_fstring(const String& buf,
350 const typename String::value_type arg_mark,
352 unsigned char exceptions)
354 // quick-parsing of the format-string to count arguments mark (arg_mark, '%')
355 // returns : upper bound on the number of format items in the format strings
356 using namespace boost::io;
357 typename String::size_type i1=0;
359 while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
360 if( i1+1 >= buf.size() ) {
361 if(exceptions & bad_format_string_bit)
362 boost::throw_exception(bad_format_string(i1, buf.size() )); // must not end in ".. %"
363 else break; // stop there, ignore last '%'
365 if(buf[i1+1] == buf[i1] ) {// escaped "%%"
370 // in case of %N% directives, dont count it double (wastes allocations..) :
371 i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
372 if( i1 < buf.size() && buf[i1] == arg_mark )
378 template<class String> inline
379 void append_string(String& dst, const String& src,
380 const typename String::size_type beg,
381 const typename String::size_type end) {
382 #if !defined(BOOST_NO_STRING_APPEND)
383 dst.append(src.begin()+beg, src.begin()+end);
385 dst += src.substr(beg, end-beg);
389 } // detail namespace
394 // -----------------------------------------------
395 // format :: parse(..)
397 template<class Ch, class Tr, class Alloc>
398 basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
399 parse (const string_type& buf) {
400 // parse the format-string
402 #if !defined(BOOST_NO_STD_LOCALE)
403 const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
405 io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
406 //has widen and narrow even on compilers without locale
409 const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
410 bool ordered_args=true;
413 // A: find upper_bound on num_items and allocates arrays
414 int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
415 make_or_reuse_data(num_items);
417 // B: Now the real parsing of the format string :
419 typename string_type::size_type i0=0, i1=0;
420 typename string_type::const_iterator it;
421 bool special_things=false;
423 while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
424 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
425 if( buf[i1+1] == buf[i1] ) { // escaped mark, '%%'
426 io::detail::append_string(piece, buf, i0, i1+1);
430 BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
433 io::detail::append_string(piece, buf, i0, i1);
436 bool parse_ok = io::detail::parse_printf_directive(
437 it, buf.end(), &items_[cur_item], fac, i1, exceptions());
438 i1 = it - buf.begin();
439 if( ! parse_ok ) // the directive will be printed verbatim
442 items_[cur_item].compute_states(); // process complex options, like zeropad, into params
444 int argN=items_[cur_item].argN_;
445 if(argN == format_item_t::argN_ignored)
447 if(argN ==format_item_t::argN_no_posit)
449 else if(argN == format_item_t::argN_tabulation) special_things=true;
450 else if(argN > max_argN) max_argN = argN;
454 BOOST_ASSERT(cur_item == num_items);
456 // store the final piece of string
458 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
459 io::detail::append_string(piece, buf, i0, buf.size());
463 if(max_argN >= 0 ) { // dont mix positional with non-positionnal directives
464 if(exceptions() & io::bad_format_string_bit)
465 boost::throw_exception(io::bad_format_string(max_argN, 0));
466 // else do nothing. => positionnal arguments are processed as non-positionnal
468 // set things like it would have been with positional directives :
469 int non_ordered_items = 0;
470 for(int i=0; i< num_items; ++i)
471 if(items_[i].argN_ == format_item_t::argN_no_posit) {
472 items_[i].argN_ = non_ordered_items;
475 max_argN = non_ordered_items-1;
478 // C: set some member data :
479 items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
481 if(special_things) style_ |= special_needs;
482 num_args_ = max_argN + 1;
483 if(ordered_args) style_ |= ordered;
484 else style_ &= ~ordered;
491 #endif // BOOST_FORMAT_PARSING_HPP