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
);
51 (void) fac
; // remove "unused parameter" warning
57 template<class Iter
, class Facet
>
58 Iter
wrap_scan_notdigit(const Facet
& fac
, Iter beg
, Iter end
) {
60 for( ; beg
!=end
&& wrap_isdigit(fac
, *beg
); ++beg
) ;
65 // Input : [start, last) iterators range and a
66 // a Facet to use its widen/narrow member function
67 // Effects : read sequence and convert digits into integral n, of type Res
69 template<class Res
, class Iter
, class Facet
>
70 Iter
str2int (const Iter
& start
, const Iter
& last
, Res
& res
,
76 for(it
=start
; it
!= last
&& wrap_isdigit(fac
, *it
); ++it
) {
77 char cur_ch
= wrap_narrow(fac
, *it
, 0); // cant fail.
79 res
+= cur_ch
- '0'; // 22.2.1.1.2.13 of the C++ standard
84 // skip printf's "asterisk-fields" directives in the format-string buf
85 // Input : char string, with starting index *pos_p
86 // a Facet merely to use its widen/narrow member function
87 // Effects : advance *pos_p by skipping printf's asterisk fields.
89 template<class Iter
, class Facet
>
90 Iter
skip_asterisk(Iter start
, Iter last
, const Facet
& fac
)
94 start
= wrap_scan_notdigit(fac
, start
, last
);
95 if(start
!=last
&& *start
== const_or_not(fac
).widen( '$') )
101 // auxiliary func called by parse_printf_directive
102 // for centralising error handling
103 // it either throws if user sets the corresponding flag, or does nothing.
104 inline void maybe_throw_exception(unsigned char exceptions
,
105 std::size_t pos
, std::size_t size
)
107 if(exceptions
& io::bad_format_string_bit
)
108 boost::throw_exception(io::bad_format_string(pos
, size
) );
112 // Input: the position of a printf-directive in the format-string
113 // a basic_ios& merely to use its widen/narrow member function
114 // a bitset'exceptions' telling whether to throw exceptions on errors.
116 // true if parse succeeded (ignore some errors if exceptions disabled)
117 // false if it failed so bad that the directive should be printed verbatim
119 // start is incremented so that *start is the first char after
121 // *fpar is set with the parameters read in the directive
122 template<class Ch
, class Tr
, class Alloc
, class Iter
, class Facet
>
123 bool parse_printf_directive(Iter
& start
, const Iter
& last
,
124 detail::format_item
<Ch
, Tr
, Alloc
> * fpar
,
126 std::size_t offset
, unsigned char exceptions
)
128 typedef typename basic_format
<Ch
, Tr
, Alloc
>::format_item_t format_item_t
;
130 fpar
->argN_
= format_item_t::argN_no_posit
; // if no positional-directive
131 bool precision_set
= false;
132 bool in_brackets
=false;
134 std::size_t fstring_size
= last
-start0
+offset
;
136 if(start
>= last
) { // empty directive : this is a trailing %
137 maybe_throw_exception(exceptions
, start
-start0
+ offset
, fstring_size
);
141 if(*start
== const_or_not(fac
).widen( '|')) {
143 if( ++start
>= last
) {
144 maybe_throw_exception(exceptions
, start
-start0
+ offset
, fstring_size
);
149 // the flag '0' would be picked as a digit for argument order, but here it's a flag :
150 if(*start
== const_or_not(fac
).widen( '0'))
153 // handle argument order (%2$d) or possibly width specification: %2d
154 if(wrap_isdigit(fac
, *start
)) {
156 start
= str2int(start
, last
, n
, fac
);
157 if( start
>= last
) {
158 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
162 // %N% case : this is already the end of the directive
163 if( *start
== const_or_not(fac
).widen( '%') ) {
167 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
168 // but don't return. maybe "%" was used in lieu of '$', so we go on.
173 if ( *start
== const_or_not(fac
).widen( '$') ) {
178 // non-positionnal directive
179 fpar
->fmtstate_
.width_
= n
;
180 fpar
->argN_
= format_item_t::argN_no_posit
;
181 goto parse_precision
;
187 while ( start
!= last
) { // as long as char is one of + - = _ # 0 l h or ' '
189 switch ( wrap_narrow(fac
, *start
, 0)) {
190 case '\'' : break; // no effect yet. (painful to implement)
192 case 'h': // short/long modifier : for printf-comaptibility (no action needed)
195 fpar
->fmtstate_
.flags_
|= std::ios_base::left
;
198 fpar
->pad_scheme_
|= format_item_t::centered
;
201 fpar
->fmtstate_
.flags_
|= std::ios_base::internal
;
204 fpar
->pad_scheme_
|= format_item_t::spacepad
;
207 fpar
->fmtstate_
.flags_
|= std::ios_base::showpos
;
210 fpar
->pad_scheme_
|= format_item_t::zeropad
;
211 // need to know alignment before really setting flags,
212 // so just add 'zeropad' flag for now, it will be processed later.
215 fpar
->fmtstate_
.flags_
|= std::ios_base::showpoint
| std::ios_base::showbase
;
224 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
229 // first skip 'asterisk fields' : *, or *N$
230 if(*start
== const_or_not(fac
).widen( '*') )
231 start
= skip_asterisk(start
, last
, fac
);
232 if(start
!=last
&& wrap_isdigit(fac
, *start
))
233 start
= str2int(start
, last
, fpar
->fmtstate_
.width_
, fac
);
237 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
240 // handle precision spec
241 if (*start
== const_or_not(fac
).widen( '.')) {
243 if(start
!= last
&& *start
== const_or_not(fac
).widen( '*') )
244 start
= skip_asterisk(start
, last
, fac
);
245 if(start
!= last
&& wrap_isdigit(fac
, *start
)) {
246 start
= str2int(start
, last
, fpar
->fmtstate_
.precision_
, fac
);
247 precision_set
= true;
250 fpar
->fmtstate_
.precision_
=0;
253 // handle formatting-type flags :
254 while( start
!= last
&& ( *start
== const_or_not(fac
).widen( 'l')
255 || *start
== const_or_not(fac
).widen( 'L')
256 || *start
== const_or_not(fac
).widen( 'h')) )
259 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
263 if( in_brackets
&& *start
== const_or_not(fac
).widen( '|') ) {
267 switch ( wrap_narrow(fac
, *start
, 0) ) {
269 fpar
->fmtstate_
.flags_
|= std::ios_base::uppercase
;
270 case 'p': // pointer => set hex.
272 fpar
->fmtstate_
.flags_
&= ~std::ios_base::basefield
;
273 fpar
->fmtstate_
.flags_
|= std::ios_base::hex
;
277 fpar
->fmtstate_
.flags_
&= ~std::ios_base::basefield
;
278 fpar
->fmtstate_
.flags_
|= std::ios_base::oct
;
282 fpar
->fmtstate_
.flags_
|= std::ios_base::uppercase
;
284 fpar
->fmtstate_
.flags_
&= ~std::ios_base::floatfield
;
285 fpar
->fmtstate_
.flags_
|= std::ios_base::scientific
;
287 fpar
->fmtstate_
.flags_
&= ~std::ios_base::basefield
;
288 fpar
->fmtstate_
.flags_
|= std::ios_base::dec
;
292 fpar
->fmtstate_
.flags_
&= ~std::ios_base::floatfield
;
293 fpar
->fmtstate_
.flags_
|= std::ios_base::fixed
;
297 fpar
->fmtstate_
.flags_
&= ~std::ios_base::basefield
;
298 fpar
->fmtstate_
.flags_
|= std::ios_base::dec
;
304 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
306 fpar
->fmtstate_
.fill_
= *start
;
307 fpar
->pad_scheme_
|= format_item_t::tabulation
;
308 fpar
->argN_
= format_item_t::argN_tabulation
;
311 fpar
->fmtstate_
.fill_
= const_or_not(fac
).widen( ' ');
312 fpar
->pad_scheme_
|= format_item_t::tabulation
;
313 fpar
->argN_
= format_item_t::argN_tabulation
;
317 fpar
->fmtstate_
.flags_
|= std::ios_base::uppercase
;
319 case 'g': // 'g' conversion is default for floats.
320 fpar
->fmtstate_
.flags_
&= ~std::ios_base::basefield
;
321 fpar
->fmtstate_
.flags_
|= std::ios_base::dec
;
323 // CLEAR all floatield flags, so stream will CHOOSE
324 fpar
->fmtstate_
.flags_
&= ~std::ios_base::floatfield
;
333 if(precision_set
) // handle truncation manually, with own parameter.
334 fpar
->truncate_
= fpar
->fmtstate_
.precision_
;
335 fpar
->fmtstate_
.precision_
= 6; // default stream precision.
338 fpar
->argN_
= format_item_t::argN_ignored
;
341 maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
346 if( start
!= last
&& *start
== const_or_not(fac
).widen( '|') ) {
350 else maybe_throw_exception(exceptions
, start
-start0
+offset
, fstring_size
);
354 // -end parse_printf_directive()
356 template<class String
, class Facet
>
357 int upper_bound_from_fstring(const String
& buf
,
358 const typename
String::value_type arg_mark
,
360 unsigned char exceptions
)
362 // quick-parsing of the format-string to count arguments mark (arg_mark, '%')
363 // returns : upper bound on the number of format items in the format strings
364 using namespace boost::io
;
365 typename
String::size_type i1
=0;
367 while( (i1
=buf
.find(arg_mark
,i1
)) != String::npos
) {
368 if( i1
+1 >= buf
.size() ) {
369 if(exceptions
& bad_format_string_bit
)
370 boost::throw_exception(bad_format_string(i1
, buf
.size() )); // must not end in ".. %"
376 if(buf
[i1
+1] == buf
[i1
] ) {// escaped "%%"
381 // in case of %N% directives, dont count it double (wastes allocations..) :
382 i1
= detail::wrap_scan_notdigit(fac
, buf
.begin()+i1
, buf
.end()) - buf
.begin();
383 if( i1
< buf
.size() && buf
[i1
] == arg_mark
)
389 template<class String
> inline
390 void append_string(String
& dst
, const String
& src
,
391 const typename
String::size_type beg
,
392 const typename
String::size_type end
) {
393 #if !defined(BOOST_NO_STRING_APPEND)
394 dst
.append(src
.begin()+beg
, src
.begin()+end
);
396 dst
+= src
.substr(beg
, end
-beg
);
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(io::bad_format_string(max_argN
, 0));
479 // else do nothing. => positionnal arguments are processed as non-positionnal
481 // set things like it would have been with positional directives :
482 int non_ordered_items
= 0;
483 for(int i
=0; i
< num_items
; ++i
)
484 if(items_
[i
].argN_
== format_item_t::argN_no_posit
) {
485 items_
[i
].argN_
= non_ordered_items
;
488 max_argN
= non_ordered_items
-1;
491 // C: set some member data :
492 items_
.resize(num_items
, format_item_t(io::detail::const_or_not(fac
).widen( ' ')) );
494 if(special_things
) style_
|= special_needs
;
495 num_args_
= max_argN
+ 1;
496 if(ordered_args
) style_
|= ordered
;
497 else style_
&= ~ordered
;
504 #endif // BOOST_FORMAT_PARSING_HPP