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>
28 #if defined(BOOST_NO_STD_LOCALE)
29 // streams will be used for narrow / widen. but these methods are not const
31 T& const_or_not(const T& x) {
32 return const_cast<T&> (x);
36 const T& const_or_not(const T& x) {
41 template<class Ch, class Facet> inline
42 char wrap_narrow(const Facet& fac, Ch c, char deflt) {
43 return const_or_not(fac).narrow(c, deflt);
46 template<class Ch, class Facet> inline
47 bool wrap_isdigit(const Facet& fac, Ch c) {
48 #if ! defined( BOOST_NO_LOCALE_ISDIGIT )
49 return fac.is(std::ctype<Ch>::digit, c);
56 template<class Iter, class Facet>
57 Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) {
59 for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ;
64 // Input : [start, last) iterators range and a
65 // a Facet to use its widen/narrow member function
66 // Effects : read sequence and convert digits into integral n, of type Res
68 template<class Res, class Iter, class Facet>
69 Iter str2int (const Iter & start, const Iter & last, Res & res,
75 for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) {
76 char cur_ch = wrap_narrow(fac, *it, 0); // cant fail.
78 res += cur_ch - '0'; // 22.2.1.1.2.13 of the C++ standard
83 // skip printf's "asterisk-fields" directives in the format-string buf
84 // Input : char string, with starting index *pos_p
85 // a Facet merely to use its widen/narrow member function
86 // Effects : advance *pos_p by skipping printf's asterisk fields.
88 template<class Iter, class Facet>
89 Iter skip_asterisk(Iter start, Iter last, const Facet& fac)
93 start = wrap_scan_notdigit(fac, start, last);
94 if(start!=last && *start== const_or_not(fac).widen( '$') )
100 // auxiliary func called by parse_printf_directive
101 // for centralising error handling
102 // it either throws if user sets the corresponding flag, or does nothing.
103 inline void maybe_throw_exception(unsigned char exceptions,
104 std::size_t pos, std::size_t size)
106 if(exceptions & io::bad_format_string_bit)
107 boost::throw_exception(io::bad_format_string(pos, size) );
111 // Input: the position of a printf-directive in the format-string
112 // a basic_ios& merely to use its widen/narrow member function
113 // a bitset'exceptions' telling whether to throw exceptions on errors.
115 // true if parse succeeded (ignore some errors if exceptions disabled)
116 // false if it failed so bad that the directive should be printed verbatim
118 // start is incremented so that *start is the first char after
120 // *fpar is set with the parameters read in the directive
121 template<class Ch, class Tr, class Alloc, class Iter, class Facet>
122 bool parse_printf_directive(Iter & start, const Iter& last,
123 detail::format_item<Ch, Tr, Alloc> * fpar,
125 std::size_t offset, unsigned char exceptions)
127 typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
129 fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive
130 bool precision_set = false;
131 bool in_brackets=false;
133 std::size_t fstring_size = last-start0+offset;
135 if(start>= last) { // empty directive : this is a trailing %
136 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
140 if(*start== const_or_not(fac).widen( '|')) {
142 if( ++start >= last ) {
143 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
148 // the flag '0' would be picked as a digit for argument order, but here it's a flag :
149 if(*start== const_or_not(fac).widen( '0'))
152 // handle argument order (%2$d) or possibly width specification: %2d
153 if(wrap_isdigit(fac, *start)) {
155 start = str2int(start, last, n, fac);
156 if( start >= last ) {
157 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
161 // %N% case : this is already the end of the directive
162 if( *start == const_or_not(fac).widen( '%') ) {
166 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
167 // but don't return. maybe "%" was used in lieu of '$', so we go on.
172 if ( *start== const_or_not(fac).widen( '$') ) {
177 // non-positionnal directive
178 fpar->fmtstate_.width_ = n;
179 fpar->argN_ = format_item_t::argN_no_posit;
180 goto parse_precision;
186 while ( start != last) { // as long as char is one of + - = _ # 0 l h or ' '
188 switch ( wrap_narrow(fac, *start, 0)) {
189 case '\'' : break; // no effect yet. (painful to implement)
191 case 'h': // short/long modifier : for printf-comaptibility (no action needed)
194 fpar->fmtstate_.flags_ |= std::ios_base::left;
197 fpar->pad_scheme_ |= format_item_t::centered;
200 fpar->fmtstate_.flags_ |= std::ios_base::internal;
203 fpar->pad_scheme_ |= format_item_t::spacepad;
206 fpar->fmtstate_.flags_ |= std::ios_base::showpos;
209 fpar->pad_scheme_ |= format_item_t::zeropad;
210 // need to know alignment before really setting flags,
211 // so just add 'zeropad' flag for now, it will be processed later.
214 fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
223 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
228 // first skip 'asterisk fields' : *, or *N$
229 if(*start == const_or_not(fac).widen( '*') )
230 start = skip_asterisk(start, last, fac);
231 if(start!=last && wrap_isdigit(fac, *start))
232 start = str2int(start, last, fpar->fmtstate_.width_, fac);
236 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
239 // handle precision spec
240 if (*start== const_or_not(fac).widen( '.')) {
242 if(start != last && *start == const_or_not(fac).widen( '*') )
243 start = skip_asterisk(start, last, fac);
244 if(start != last && wrap_isdigit(fac, *start)) {
245 start = str2int(start, last, fpar->fmtstate_.precision_, fac);
246 precision_set = true;
249 fpar->fmtstate_.precision_ =0;
252 // handle formatting-type flags :
253 while( start != last && ( *start== const_or_not(fac).widen( 'l')
254 || *start== const_or_not(fac).widen( 'L')
255 || *start== const_or_not(fac).widen( 'h')) )
258 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
262 if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
266 switch ( wrap_narrow(fac, *start, 0) ) {
268 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
269 case 'p': // pointer => set hex.
271 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
272 fpar->fmtstate_.flags_ |= std::ios_base::hex;
276 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
277 fpar->fmtstate_.flags_ |= std::ios_base::oct;
281 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
283 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
284 fpar->fmtstate_.flags_ |= std::ios_base::scientific;
286 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
287 fpar->fmtstate_.flags_ |= std::ios_base::dec;
291 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
292 fpar->fmtstate_.flags_ |= std::ios_base::fixed;
296 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
297 fpar->fmtstate_.flags_ |= std::ios_base::dec;
303 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
305 fpar->fmtstate_.fill_ = *start;
306 fpar->pad_scheme_ |= format_item_t::tabulation;
307 fpar->argN_ = format_item_t::argN_tabulation;
310 fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
311 fpar->pad_scheme_ |= format_item_t::tabulation;
312 fpar->argN_ = format_item_t::argN_tabulation;
316 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
318 case 'g': // 'g' conversion is default for floats.
319 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
320 fpar->fmtstate_.flags_ |= std::ios_base::dec;
322 // CLEAR all floatield flags, so stream will CHOOSE
323 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
332 if(precision_set) // handle truncation manually, with own parameter.
333 fpar->truncate_ = fpar->fmtstate_.precision_;
334 fpar->fmtstate_.precision_ = 6; // default stream precision.
337 fpar->argN_ = format_item_t::argN_ignored;
340 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
345 if( start != last && *start== const_or_not(fac).widen( '|') ) {
349 else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
353 // -end parse_printf_directive()
355 template<class String, class Facet>
356 int upper_bound_from_fstring(const String& buf,
357 const typename String::value_type arg_mark,
359 unsigned char exceptions)
361 // quick-parsing of the format-string to count arguments mark (arg_mark, '%')
362 // returns : upper bound on the number of format items in the format strings
363 using namespace boost::io;
364 typename String::size_type i1=0;
366 while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
367 if( i1+1 >= buf.size() ) {
368 if(exceptions & bad_format_string_bit)
369 boost::throw_exception(bad_format_string(i1, buf.size() )); // must not end in ".. %"
375 if(buf[i1+1] == buf[i1] ) {// escaped "%%"
380 // in case of %N% directives, dont count it double (wastes allocations..) :
381 i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
382 if( i1 < buf.size() && buf[i1] == arg_mark )
388 template<class String> inline
389 void append_string(String& dst, const String& src,
390 const typename String::size_type beg,
391 const typename String::size_type end) {
392 #if !defined(BOOST_NO_STRING_APPEND)
393 dst.append(src.begin()+beg, src.begin()+end);
395 dst += src.substr(beg, end-beg);
399 } // detail namespace
404 // -----------------------------------------------
405 // format :: parse(..)
407 template<class Ch, class Tr, class Alloc>
408 basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
409 parse (const string_type& buf) {
410 // parse the format-string
412 #if !defined(BOOST_NO_STD_LOCALE)
413 const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
415 io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
416 //has widen and narrow even on compilers without locale
419 const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
420 bool ordered_args=true;
423 // A: find upper_bound on num_items and allocates arrays
424 int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
425 make_or_reuse_data(num_items);
427 // B: Now the real parsing of the format string :
429 typename string_type::size_type i0=0, i1=0;
430 typename string_type::const_iterator it;
431 bool special_things=false;
433 while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
434 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
435 if( buf[i1+1] == buf[i1] ) { // escaped mark, '%%'
436 io::detail::append_string(piece, buf, i0, i1+1);
440 BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
443 io::detail::append_string(piece, buf, i0, i1);
448 bool parse_ok = io::detail::parse_printf_directive(
449 it, buf.end(), &items_[cur_item], fac, i1, exceptions());
450 i1 = it - buf.begin();
451 if( ! parse_ok ) // the directive will be printed verbatim
454 items_[cur_item].compute_states(); // process complex options, like zeropad, into params
456 int argN=items_[cur_item].argN_;
457 if(argN == format_item_t::argN_ignored)
459 if(argN ==format_item_t::argN_no_posit)
461 else if(argN == format_item_t::argN_tabulation) special_things=true;
462 else if(argN > max_argN) max_argN = argN;
466 BOOST_ASSERT(cur_item == num_items);
468 // store the final piece of string
470 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
471 io::detail::append_string(piece, buf, i0, buf.size());
475 if(max_argN >= 0 ) { // dont mix positional with non-positionnal directives
476 if(exceptions() & io::bad_format_string_bit)
477 boost::throw_exception(io::bad_format_string(max_argN, 0));
478 // else do nothing. => positionnal arguments are processed as non-positionnal
480 // set things like it would have been with positional directives :
481 int non_ordered_items = 0;
482 for(int i=0; i< num_items; ++i)
483 if(items_[i].argN_ == format_item_t::argN_no_posit) {
484 items_[i].argN_ = non_ordered_items;
487 max_argN = non_ordered_items-1;
490 // C: set some member data :
491 items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
493 if(special_things) style_ |= special_needs;
494 num_args_ = max_argN + 1;
495 if(ordered_args) style_ |= ordered;
496 else style_ &= ~ordered;
503 #endif // BOOST_FORMAT_PARSING_HPP