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>
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);
52 (void) fac; // remove "unused parameter" warning
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 // skip printf's "asterisk-fields" directives in the format-string buf
86 // Input : char string, with starting index *pos_p
87 // a Facet merely to use its widen/narrow member function
88 // Effects : advance *pos_p by skipping printf's asterisk fields.
90 template<class Iter, class Facet>
91 Iter skip_asterisk(Iter start, Iter last, const Facet& fac)
95 start = wrap_scan_notdigit(fac, start, last);
96 if(start!=last && *start== const_or_not(fac).widen( '$') )
102 // auxiliary func called by parse_printf_directive
103 // for centralising error handling
104 // it either throws if user sets the corresponding flag, or does nothing.
105 inline void maybe_throw_exception(unsigned char exceptions,
106 std::size_t pos, std::size_t size)
108 if(exceptions & io::bad_format_string_bit)
109 boost::throw_exception(io::bad_format_string(pos, size) );
113 // Input: the position of a printf-directive in the format-string
114 // a basic_ios& merely to use its widen/narrow member function
115 // a bitset'exceptions' telling whether to throw exceptions on errors.
117 // true if parse succeeded (ignore some errors if exceptions disabled)
118 // false if it failed so bad that the directive should be printed verbatim
120 // start is incremented so that *start is the first char after
122 // *fpar is set with the parameters read in the directive
123 template<class Ch, class Tr, class Alloc, class Iter, class Facet>
124 bool parse_printf_directive(Iter & start, const Iter& last,
125 detail::format_item<Ch, Tr, Alloc> * fpar,
127 std::size_t offset, unsigned char exceptions)
129 typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
131 fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive
132 bool precision_set = false;
133 bool in_brackets=false;
135 std::size_t fstring_size = last-start0+offset;
137 if(start>= last) { // empty directive : this is a trailing %
138 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
142 if(*start== const_or_not(fac).widen( '|')) {
144 if( ++start >= last ) {
145 maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
150 // the flag '0' would be picked as a digit for argument order, but here it's a flag :
151 if(*start== const_or_not(fac).widen( '0'))
154 // handle argument order (%2$d) or possibly width specification: %2d
155 if(wrap_isdigit(fac, *start)) {
157 start = str2int(start, last, n, fac);
158 if( start >= last ) {
159 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
163 // %N% case : this is already the end of the directive
164 if( *start == const_or_not(fac).widen( '%') ) {
168 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
169 // but don't return. maybe "%" was used in lieu of '$', so we go on.
174 if ( *start== const_or_not(fac).widen( '$') ) {
179 // non-positionnal directive
180 fpar->fmtstate_.width_ = n;
181 fpar->argN_ = format_item_t::argN_no_posit;
182 goto parse_precision;
188 while ( start != last) { // as long as char is one of + - = _ # 0 l h or ' '
190 switch ( wrap_narrow(fac, *start, 0)) {
191 case '\'' : break; // no effect yet. (painful to implement)
193 case 'h': // short/long modifier : for printf-comaptibility (no action needed)
196 fpar->fmtstate_.flags_ |= std::ios_base::left;
199 fpar->pad_scheme_ |= format_item_t::centered;
202 fpar->fmtstate_.flags_ |= std::ios_base::internal;
205 fpar->pad_scheme_ |= format_item_t::spacepad;
208 fpar->fmtstate_.flags_ |= std::ios_base::showpos;
211 fpar->pad_scheme_ |= format_item_t::zeropad;
212 // need to know alignment before really setting flags,
213 // so just add 'zeropad' flag for now, it will be processed later.
216 fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
225 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
230 // first skip 'asterisk fields' : *, or *N$
231 if(*start == const_or_not(fac).widen( '*') )
232 start = skip_asterisk(start, last, fac);
233 if(start!=last && wrap_isdigit(fac, *start))
234 start = str2int(start, last, fpar->fmtstate_.width_, fac);
238 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
241 // handle precision spec
242 if (*start== const_or_not(fac).widen( '.')) {
244 if(start != last && *start == const_or_not(fac).widen( '*') )
245 start = skip_asterisk(start, last, fac);
246 if(start != last && wrap_isdigit(fac, *start)) {
247 start = str2int(start, last, fpar->fmtstate_.precision_, fac);
248 precision_set = true;
251 fpar->fmtstate_.precision_ =0;
254 // handle formatting-type flags :
255 while( start != last && ( *start== const_or_not(fac).widen( 'l')
256 || *start== const_or_not(fac).widen( 'L')
257 || *start== const_or_not(fac).widen( 'h')) )
260 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
264 if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
268 switch ( wrap_narrow(fac, *start, 0) ) {
270 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
272 case 'p': // pointer => set hex.
274 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
275 fpar->fmtstate_.flags_ |= std::ios_base::hex;
279 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
280 fpar->fmtstate_.flags_ |= std::ios_base::oct;
284 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
287 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
288 fpar->fmtstate_.flags_ |= std::ios_base::scientific;
290 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
291 fpar->fmtstate_.flags_ |= std::ios_base::dec;
295 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
296 fpar->fmtstate_.flags_ |= std::ios_base::fixed;
301 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
302 fpar->fmtstate_.flags_ |= std::ios_base::dec;
308 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
310 fpar->fmtstate_.fill_ = *start;
311 fpar->pad_scheme_ |= format_item_t::tabulation;
312 fpar->argN_ = format_item_t::argN_tabulation;
315 fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
316 fpar->pad_scheme_ |= format_item_t::tabulation;
317 fpar->argN_ = format_item_t::argN_tabulation;
321 fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
323 case 'g': // 'g' conversion is default for floats.
324 fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
325 fpar->fmtstate_.flags_ |= std::ios_base::dec;
327 // CLEAR all floatield flags, so stream will CHOOSE
328 fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
337 if(precision_set) // handle truncation manually, with own parameter.
338 fpar->truncate_ = fpar->fmtstate_.precision_;
339 fpar->fmtstate_.precision_ = 6; // default stream precision.
342 fpar->argN_ = format_item_t::argN_ignored;
345 maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
350 if( start != last && *start== const_or_not(fac).widen( '|') ) {
354 else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
358 // -end parse_printf_directive()
360 template<class String, class Facet>
361 int upper_bound_from_fstring(const String& buf,
362 const typename String::value_type arg_mark,
364 unsigned char exceptions)
366 // quick-parsing of the format-string to count arguments mark (arg_mark, '%')
367 // returns : upper bound on the number of format items in the format strings
368 using namespace boost::io;
369 typename String::size_type i1=0;
371 while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
372 if( i1+1 >= buf.size() ) {
373 if(exceptions & bad_format_string_bit)
374 boost::throw_exception(bad_format_string(i1, buf.size() )); // must not end in ".. %"
380 if(buf[i1+1] == buf[i1] ) {// escaped "%%"
385 // in case of %N% directives, dont count it double (wastes allocations..) :
386 i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
387 if( i1 < buf.size() && buf[i1] == arg_mark )
393 template<class String> inline
394 void append_string(String& dst, const String& src,
395 const typename String::size_type beg,
396 const typename String::size_type end) {
397 dst.append(src.begin()+beg, src.begin()+end);
400 } // detail namespace
405 // -----------------------------------------------
406 // format :: parse(..)
408 template<class Ch, class Tr, class Alloc>
409 basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
410 parse (const string_type& buf) {
411 // parse the format-string
413 #if !defined(BOOST_NO_STD_LOCALE)
414 const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
416 io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
417 //has widen and narrow even on compilers without locale
420 const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
421 bool ordered_args=true;
424 // A: find upper_bound on num_items and allocates arrays
425 int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
426 make_or_reuse_data(num_items);
428 // B: Now the real parsing of the format string :
430 typename string_type::size_type i0=0, i1=0;
431 typename string_type::const_iterator it;
432 bool special_things=false;
434 while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
435 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
436 if( buf[i1+1] == buf[i1] ) { // escaped mark, '%%'
437 io::detail::append_string(piece, buf, i0, i1+1);
441 BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
444 io::detail::append_string(piece, buf, i0, i1);
449 bool parse_ok = io::detail::parse_printf_directive(
450 it, buf.end(), &items_[cur_item], fac, i1, exceptions());
451 i1 = it - buf.begin();
452 if( ! parse_ok ) // the directive will be printed verbatim
455 items_[cur_item].compute_states(); // process complex options, like zeropad, into params
457 int argN=items_[cur_item].argN_;
458 if(argN == format_item_t::argN_ignored)
460 if(argN ==format_item_t::argN_no_posit)
462 else if(argN == format_item_t::argN_tabulation) special_things=true;
463 else if(argN > max_argN) max_argN = argN;
467 BOOST_ASSERT(cur_item == num_items);
469 // store the final piece of string
471 string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
472 io::detail::append_string(piece, buf, i0, buf.size());
476 if(max_argN >= 0 ) { // dont mix positional with non-positionnal directives
477 if(exceptions() & io::bad_format_string_bit)
478 boost::throw_exception(
479 io::bad_format_string(static_cast<std::size_t>(max_argN), 0));
480 // else do nothing. => positionnal arguments are processed as non-positionnal
482 // set things like it would have been with positional directives :
483 int non_ordered_items = 0;
484 for(int i=0; i< num_items; ++i)
485 if(items_[i].argN_ == format_item_t::argN_no_posit) {
486 items_[i].argN_ = non_ordered_items;
489 max_argN = non_ordered_items-1;
492 // C: set some member data :
493 items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
495 if(special_things) style_ |= special_needs;
496 num_args_ = max_argN + 1;
497 if(ordered_args) style_ |= ordered;
498 else style_ &= ~ordered;
505 #endif // BOOST_FORMAT_PARSING_HPP