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/format/exceptions.hpp>
20 #include <boost/throw_exception.hpp>
21 #include <boost/assert.hpp>
22 #include <boost/config.hpp>
23 #include <boost/core/ignore_unused.hpp>
29 #if defined(BOOST_NO_STD_LOCALE)
30 // streams will be used for narrow / widen. but these methods are not const
32 T& const_or_not(const T& x) {
33 return const_cast<T&> (x);
37 const T& const_or_not(const T& x) {
42 template<class Ch, class Facet> inline
43 char wrap_narrow(const Facet& fac, Ch c, char deflt) {
44 return const_or_not(fac).narrow(c, deflt);
47 template<class Ch, class Facet> inline
48 bool wrap_isdigit(const Facet& fac, Ch c) {
49 #if ! defined( BOOST_NO_LOCALE_ISDIGIT )
50 return fac.is(std::ctype<Ch>::digit, c);
54 return isdigit(c) != 0;
58 template<class Iter, class Facet>
59 Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) {
61 for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ;
66 // Input : [start, last) iterators range and a
67 // a Facet to use its widen/narrow member function
68 // Effects : read sequence and convert digits into integral n, of type Res
70 template<class Res, class Iter, class Facet>
71 Iter str2int (const Iter & start, const Iter & last, Res & res,
77 for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) {
78 char cur_ch = wrap_narrow(fac, *it, 0); // cant fail.
80 res += cur_ch - '0'; // 22.2.1.1.2.13 of the C++ standard
85 // auxiliary func called by parse_printf_directive
86 // for centralising error handling
87 // it either throws if user sets the corresponding flag, or does nothing.
88 inline void maybe_throw_exception(unsigned char exceptions,
89 std::size_t pos, std::size_t size)
91 if(exceptions & io::bad_format_string_bit)
92 boost::throw_exception(io::bad_format_string(pos, size) );
96 // Input: the position of a printf-directive in the format-string
97 // a basic_ios& merely to use its widen/narrow member function
98 // a bitset'exceptions' telling whether to throw exceptions on errors.
100 // true if parse succeeded (ignore some errors if exceptions disabled)
101 // false if it failed so bad that the directive should be printed verbatim
103 // start is incremented so that *start is the first char after
105 // *fpar is set with the parameters read in the directive
106 template<class Ch, class Tr, class Alloc, class Iter, class Facet>
107 bool parse_printf_directive(Iter & start, const Iter& last,
108 detail::format_item<Ch, Tr, Alloc> * fpar,
110 std::size_t offset, unsigned char exceptions)
112 typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
114 fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive
115 bool precision_set = false;
116 bool in_brackets=false;
118 std::size_t fstring_size = last-start0+offset;
121 if(start>= last) { // empty directive : this is a trailing %
122 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
126 if(*start== const_or_not(fac).widen( '|')) {
128 if( ++start >= last ) {
129 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
134 // the flag '0' would be picked as a digit for argument order, but here it's a flag :
135 if(*start== const_or_not(fac).widen( '0'))
138 // handle argument order (%2$d) or possibly width specification: %2d
139 if(wrap_isdigit(fac, *start)) {
141 start = str2int(start, last, n, fac);
142 if( start >= last ) {
143 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
147 // %N% case : this is already the end of the directive
148 if( *start == const_or_not(fac).widen( '%') ) {
152 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
156 if ( *start== const_or_not(fac).widen( '$') ) {
161 // non-positional directive
162 fpar->fmtstate_.width_ = n;
163 fpar->argN_ = format_item_t::argN_no_posit;
164 goto parse_precision;
170 while (start != last) { // as long as char is one of + - = _ # 0 or ' '
171 switch ( wrap_narrow(fac, *start, 0)) {
173 break; // no effect yet. (painful to implement)
175 fpar->fmtstate_.flags_ |= std::ios_base::left;
178 fpar->pad_scheme_ |= format_item_t::centered;
181 fpar->fmtstate_.flags_ |= std::ios_base::internal;
184 fpar->pad_scheme_ |= format_item_t::spacepad;
187 fpar->fmtstate_.flags_ |= std::ios_base::showpos;
190 fpar->pad_scheme_ |= format_item_t::zeropad;
191 // need to know alignment before really setting flags,
192 // so just add 'zeropad' flag for now, it will be processed later.
195 fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
204 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
208 // first skip 'asterisk fields' : * or num (length)
210 if(*start == const_or_not(fac).widen( '*') )
212 else if(start!=last && wrap_isdigit(fac, *start))
213 start = str2int(start, last, fpar->fmtstate_.width_, fac);
217 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
220 // handle precision spec
221 if (*start== const_or_not(fac).widen( '.')) {
223 if(start != last && *start == const_or_not(fac).widen( '*') )
225 else if(start != last && wrap_isdigit(fac, *start)) {
226 start = str2int(start, last, fpar->fmtstate_.precision_, fac);
227 precision_set = true;
230 fpar->fmtstate_.precision_ =0;
233 // argument type modifiers
234 while (start != last) {
235 switch (wrap_narrow(fac, *start, 0)) {
241 // boost::format ignores argument type modifiers as it relies on
242 // the type of the argument fed into it by operator %
245 // Note that the ptrdiff_t argument type 't' from C++11 is not honored
246 // because it was already in use as the tabulation specifier in boost::format
249 // Microsoft extensions:
250 // https://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx
259 maybe_throw_exception(exceptions, start - start0 + offset, fstring_size);
266 maybe_throw_exception(exceptions, start - start0 + offset, fstring_size);
273 maybe_throw_exception(exceptions, start - start0 + offset, fstring_size);
280 maybe_throw_exception(exceptions, start - start0 + offset, fstring_size);
286 if (mssiz && mssiz == 'I') {
289 goto parse_conversion_specification;
292 } // loop on argument type modifiers to pick up 'hh', 'll', and the more complex microsoft ones
294 parse_conversion_specification:
295 if (start >= last || mssiz) {
296 maybe_throw_exception(exceptions, start - start0 + offset, fstring_size);
300 if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
305 // The default flags are "dec" and "skipws"
306 // so if changing the base, need to unset basefield first
308 switch (wrap_narrow(fac, *start, 0))
312 fpar->fmtstate_.flags_ |= std::ios_base::boolalpha;
319 // Defaults are sufficient
324 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
327 case 'p': // pointer => set hex.
328 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
329 fpar->fmtstate_.flags_ |= std::ios_base::hex;
334 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
335 fpar->fmtstate_.flags_ |= std::ios_base::oct;
340 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
343 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
344 fpar->fmtstate_.flags_ |= std::ios_base::fixed;
345 fpar->fmtstate_.flags_ |= std::ios_base::scientific;
348 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
351 fpar->fmtstate_.flags_ |= std::ios_base::scientific;
354 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
357 fpar->fmtstate_.flags_ |= std::ios_base::fixed;
360 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
363 // default flags are correct here
366 // Tabulation (a boost::format extension)
370 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
373 fpar->fmtstate_.fill_ = *start;
375 fpar->pad_scheme_ |= format_item_t::tabulation;
376 fpar->argN_ = format_item_t::argN_tabulation;
379 fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
380 fpar->pad_scheme_ |= format_item_t::tabulation;
381 fpar->argN_ = format_item_t::argN_tabulation;
393 if(precision_set) // handle truncation manually, with own parameter.
394 fpar->truncate_ = fpar->fmtstate_.precision_;
395 fpar->fmtstate_.precision_ = 6; // default stream precision.
398 // %n is insecure and ignored by boost::format
400 fpar->argN_ = format_item_t::argN_ignored;
404 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
409 if( start != last && *start== const_or_not(fac).widen( '|') ) {
413 else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
417 // -end parse_printf_directive()
419 template<class String, class Facet>
420 int upper_bound_from_fstring(const String& buf,
421 const typename String::value_type arg_mark,
423 unsigned char exceptions)
425 // quick-parsing of the format-string to count arguments mark (arg_mark, '%')
426 // returns : upper bound on the number of format items in the format strings
427 using namespace boost::io;
428 typename String::size_type i1=0;
430 while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
431 if( i1+1 >= buf.size() ) {
432 if(exceptions & bad_format_string_bit)
433 boost::throw_exception(bad_format_string(i1, buf.size() )); // must not end in ".. %"
439 if(buf[i1+1] == buf[i1] ) {// escaped "%%"
444 // in case of %N% directives, dont count it double (wastes allocations..) :
445 i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
446 if( i1 < buf.size() && buf[i1] == arg_mark )
452 template<class String> inline
453 void append_string(String& dst, const String& src,
454 const typename String::size_type beg,
455 const typename String::size_type end) {
456 dst.append(src.begin()+beg, src.begin()+end);
459 } // detail namespace
464 // -----------------------------------------------
465 // format :: parse(..)
467 template<class Ch, class Tr, class Alloc>
468 basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
469 parse (const string_type& buf) {
470 // parse the format-string
472 #if !defined(BOOST_NO_STD_LOCALE)
473 const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
475 io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
476 //has widen and narrow even on compilers without locale
479 const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
480 bool ordered_args=true;
483 // A: find upper_bound on num_items and allocates arrays
484 int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
485 make_or_reuse_data(num_items);
487 // B: Now the real parsing of the format string :
489 typename string_type::size_type i0=0, i1=0;
490 typename string_type::const_iterator it;
491 bool special_things=false;
493 while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
494 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
495 if( buf[i1+1] == buf[i1] ) { // escaped mark, '%%'
496 io::detail::append_string(piece, buf, i0, i1+1);
500 BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
503 io::detail::append_string(piece, buf, i0, i1);
508 bool parse_ok = io::detail::parse_printf_directive(
509 it, buf.end(), &items_[cur_item], fac, i1, exceptions());
510 i1 = it - buf.begin();
511 if( ! parse_ok ) // the directive will be printed verbatim
514 items_[cur_item].compute_states(); // process complex options, like zeropad, into params
516 int argN=items_[cur_item].argN_;
517 if(argN == format_item_t::argN_ignored)
519 if(argN ==format_item_t::argN_no_posit)
521 else if(argN == format_item_t::argN_tabulation) special_things=true;
522 else if(argN > max_argN) max_argN = argN;
526 BOOST_ASSERT(cur_item == num_items);
528 // store the final piece of string
530 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
531 io::detail::append_string(piece, buf, i0, buf.size());
535 if(max_argN >= 0 ) { // dont mix positional with non-positionnal directives
536 if(exceptions() & io::bad_format_string_bit)
537 boost::throw_exception(
538 io::bad_format_string(static_cast<std::size_t>(max_argN), 0));
539 // else do nothing. => positionnal arguments are processed as non-positionnal
541 // set things like it would have been with positional directives :
542 int non_ordered_items = 0;
543 for(int i=0; i< num_items; ++i)
544 if(items_[i].argN_ == format_item_t::argN_no_posit) {
545 items_[i].argN_ = non_ordered_items;
548 max_argN = non_ordered_items-1;
551 // C: set some member data :
552 items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
554 if(special_things) style_ |= special_needs;
555 num_args_ = max_argN + 1;
556 if(ordered_args) style_ |= ordered;
557 else style_ &= ~ordered;
564 #endif // BOOST_FORMAT_PARSING_HPP