]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // ---------------------------------------------------------------------------- |
2 | // parsing.hpp : implementation of the parsing member functions | |
3 | // ( parse, parse_printf_directive) | |
4 | // ---------------------------------------------------------------------------- | |
5 | ||
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) | |
9 | ||
10 | // see http://www.boost.org/libs/format for library home page | |
11 | ||
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #ifndef BOOST_FORMAT_PARSING_HPP | |
15 | #define BOOST_FORMAT_PARSING_HPP | |
16 | ||
17 | ||
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 | ||
24 | ||
25 | namespace boost { | |
26 | namespace io { | |
27 | namespace detail { | |
28 | ||
29 | #if defined(BOOST_NO_STD_LOCALE) | |
30 | // streams will be used for narrow / widen. but these methods are not const | |
31 | template<class T> | |
32 | T& const_or_not(const T& x) { | |
33 | return const_cast<T&> (x); | |
34 | } | |
35 | #else | |
36 | template<class T> | |
37 | const T& const_or_not(const T& x) { | |
38 | return x; | |
39 | } | |
40 | #endif | |
41 | ||
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); | |
45 | } | |
46 | ||
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); | |
51 | # else | |
52 | (void) fac; // remove "unused parameter" warning | |
53 | using namespace std; | |
54 | return isdigit(c) != 0; | |
55 | #endif | |
56 | } | |
57 | ||
58 | template<class Iter, class Facet> | |
59 | Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) { | |
60 | using namespace std; | |
61 | for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ; | |
62 | return beg; | |
63 | } | |
64 | ||
65 | ||
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 | |
69 | // Returns : n | |
70 | template<class Res, class Iter, class Facet> | |
71 | Iter str2int (const Iter & start, const Iter & last, Res & res, | |
72 | const Facet& fac) | |
73 | { | |
74 | using namespace std; | |
75 | Iter it; | |
76 | res=0; | |
77 | for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) { | |
78 | char cur_ch = wrap_narrow(fac, *it, 0); // cant fail. | |
79 | res *= 10; | |
80 | res += cur_ch - '0'; // 22.2.1.1.2.13 of the C++ standard | |
81 | } | |
82 | return it; | |
83 | } | |
84 | ||
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. | |
89 | // Returns : nothing | |
90 | template<class Iter, class Facet> | |
91 | Iter skip_asterisk(Iter start, Iter last, const Facet& fac) | |
92 | { | |
93 | using namespace std; | |
94 | ++ start; | |
95 | start = wrap_scan_notdigit(fac, start, last); | |
96 | if(start!=last && *start== const_or_not(fac).widen( '$') ) | |
97 | ++start; | |
98 | return start; | |
99 | } | |
100 | ||
101 | ||
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) | |
107 | { | |
108 | if(exceptions & io::bad_format_string_bit) | |
109 | boost::throw_exception(io::bad_format_string(pos, size) ); | |
110 | } | |
111 | ||
112 | ||
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. | |
116 | // Returns: | |
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 | |
119 | // Effects: | |
120 | // start is incremented so that *start is the first char after | |
121 | // this directive | |
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, | |
126 | const Facet& fac, | |
127 | std::size_t offset, unsigned char exceptions) | |
128 | { | |
129 | typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t; | |
130 | ||
131 | fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive | |
132 | bool precision_set = false; | |
133 | bool in_brackets=false; | |
134 | Iter start0 = start; | |
135 | std::size_t fstring_size = last-start0+offset; | |
136 | ||
137 | if(start>= last) { // empty directive : this is a trailing % | |
138 | maybe_throw_exception(exceptions, start-start0 + offset, fstring_size); | |
139 | return false; | |
140 | } | |
141 | ||
142 | if(*start== const_or_not(fac).widen( '|')) { | |
143 | in_brackets=true; | |
144 | if( ++start >= last ) { | |
145 | maybe_throw_exception(exceptions, start-start0 + offset, fstring_size); | |
146 | return false; | |
147 | } | |
148 | } | |
149 | ||
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')) | |
152 | goto parse_flags; | |
153 | ||
154 | // handle argument order (%2$d) or possibly width specification: %2d | |
155 | if(wrap_isdigit(fac, *start)) { | |
156 | int n; | |
157 | start = str2int(start, last, n, fac); | |
158 | if( start >= last ) { | |
159 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
160 | return false; | |
161 | } | |
162 | ||
163 | // %N% case : this is already the end of the directive | |
164 | if( *start == const_or_not(fac).widen( '%') ) { | |
165 | fpar->argN_ = n-1; | |
166 | ++start; | |
167 | if( in_brackets) | |
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. | |
170 | else | |
171 | return true; | |
172 | } | |
173 | ||
174 | if ( *start== const_or_not(fac).widen( '$') ) { | |
175 | fpar->argN_ = n-1; | |
176 | ++start; | |
177 | } | |
178 | else { | |
179 | // non-positionnal directive | |
180 | fpar->fmtstate_.width_ = n; | |
181 | fpar->argN_ = format_item_t::argN_no_posit; | |
182 | goto parse_precision; | |
183 | } | |
184 | } | |
185 | ||
186 | parse_flags: | |
187 | // handle flags | |
188 | while ( start != last) { // as long as char is one of + - = _ # 0 l h or ' ' | |
189 | // misc switches | |
190 | switch ( wrap_narrow(fac, *start, 0)) { | |
191 | case '\'' : break; // no effect yet. (painful to implement) | |
192 | case 'l': | |
193 | case 'h': // short/long modifier : for printf-comaptibility (no action needed) | |
194 | break; | |
195 | case '-': | |
196 | fpar->fmtstate_.flags_ |= std::ios_base::left; | |
197 | break; | |
198 | case '=': | |
199 | fpar->pad_scheme_ |= format_item_t::centered; | |
200 | break; | |
201 | case '_': | |
202 | fpar->fmtstate_.flags_ |= std::ios_base::internal; | |
203 | break; | |
204 | case ' ': | |
205 | fpar->pad_scheme_ |= format_item_t::spacepad; | |
206 | break; | |
207 | case '+': | |
208 | fpar->fmtstate_.flags_ |= std::ios_base::showpos; | |
209 | break; | |
210 | case '0': | |
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. | |
214 | break; | |
215 | case '#': | |
216 | fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase; | |
217 | break; | |
218 | default: | |
219 | goto parse_width; | |
220 | } | |
221 | ++start; | |
222 | } // loop on flag. | |
223 | ||
224 | if( start>=last) { | |
225 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
226 | return true; | |
227 | } | |
228 | parse_width: | |
229 | // handle width spec | |
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); | |
235 | ||
236 | parse_precision: | |
237 | if( start>= last) { | |
238 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
239 | return true; | |
240 | } | |
241 | // handle precision spec | |
242 | if (*start== const_or_not(fac).widen( '.')) { | |
243 | ++start; | |
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; | |
249 | } | |
250 | else | |
251 | fpar->fmtstate_.precision_ =0; | |
252 | } | |
253 | ||
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')) ) | |
258 | ++start; | |
259 | if( start>=last) { | |
260 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
261 | return true; | |
262 | } | |
263 | ||
264 | if( in_brackets && *start== const_or_not(fac).widen( '|') ) { | |
265 | ++start; | |
266 | return true; | |
267 | } | |
268 | switch ( wrap_narrow(fac, *start, 0) ) { | |
269 | case 'X': | |
270 | fpar->fmtstate_.flags_ |= std::ios_base::uppercase; | |
271 | BOOST_FALLTHROUGH; | |
272 | case 'p': // pointer => set hex. | |
273 | case 'x': | |
274 | fpar->fmtstate_.flags_ &= ~std::ios_base::basefield; | |
275 | fpar->fmtstate_.flags_ |= std::ios_base::hex; | |
276 | break; | |
277 | ||
278 | case 'o': | |
279 | fpar->fmtstate_.flags_ &= ~std::ios_base::basefield; | |
280 | fpar->fmtstate_.flags_ |= std::ios_base::oct; | |
281 | break; | |
282 | ||
283 | case 'E': | |
284 | fpar->fmtstate_.flags_ |= std::ios_base::uppercase; | |
285 | BOOST_FALLTHROUGH; | |
286 | case 'e': | |
287 | fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield; | |
288 | fpar->fmtstate_.flags_ |= std::ios_base::scientific; | |
289 | ||
290 | fpar->fmtstate_.flags_ &= ~std::ios_base::basefield; | |
291 | fpar->fmtstate_.flags_ |= std::ios_base::dec; | |
292 | break; | |
293 | ||
294 | case 'f': | |
295 | fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield; | |
296 | fpar->fmtstate_.flags_ |= std::ios_base::fixed; | |
297 | BOOST_FALLTHROUGH; | |
298 | case 'u': | |
299 | case 'd': | |
300 | case 'i': | |
301 | fpar->fmtstate_.flags_ &= ~std::ios_base::basefield; | |
302 | fpar->fmtstate_.flags_ |= std::ios_base::dec; | |
303 | break; | |
304 | ||
305 | case 'T': | |
306 | ++start; | |
307 | if( start >= last) | |
308 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
309 | else | |
310 | fpar->fmtstate_.fill_ = *start; | |
311 | fpar->pad_scheme_ |= format_item_t::tabulation; | |
312 | fpar->argN_ = format_item_t::argN_tabulation; | |
313 | break; | |
314 | case 't': | |
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; | |
318 | break; | |
319 | ||
320 | case 'G': | |
321 | fpar->fmtstate_.flags_ |= std::ios_base::uppercase; | |
322 | break; | |
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; | |
326 | ||
327 | // CLEAR all floatield flags, so stream will CHOOSE | |
328 | fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield; | |
329 | break; | |
330 | ||
331 | case 'C': | |
332 | case 'c': | |
333 | fpar->truncate_ = 1; | |
334 | break; | |
335 | case 'S': | |
336 | case 's': | |
337 | if(precision_set) // handle truncation manually, with own parameter. | |
338 | fpar->truncate_ = fpar->fmtstate_.precision_; | |
339 | fpar->fmtstate_.precision_ = 6; // default stream precision. | |
340 | break; | |
341 | case 'n' : | |
342 | fpar->argN_ = format_item_t::argN_ignored; | |
343 | break; | |
344 | default: | |
345 | maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
346 | } | |
347 | ++start; | |
348 | ||
349 | if( in_brackets ) { | |
350 | if( start != last && *start== const_or_not(fac).widen( '|') ) { | |
351 | ++start; | |
352 | return true; | |
353 | } | |
354 | else maybe_throw_exception(exceptions, start-start0+offset, fstring_size); | |
355 | } | |
356 | return true; | |
357 | } | |
358 | // -end parse_printf_directive() | |
359 | ||
360 | template<class String, class Facet> | |
361 | int upper_bound_from_fstring(const String& buf, | |
362 | const typename String::value_type arg_mark, | |
363 | const Facet& fac, | |
364 | unsigned char exceptions) | |
365 | { | |
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; | |
370 | int num_items=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 ".. %" | |
375 | else { | |
376 | ++num_items; | |
377 | break; | |
378 | } | |
379 | } | |
380 | if(buf[i1+1] == buf[i1] ) {// escaped "%%" | |
381 | i1+=2; continue; | |
382 | } | |
383 | ||
384 | ++i1; | |
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 ) | |
388 | ++i1; | |
389 | ++num_items; | |
390 | } | |
391 | return num_items; | |
392 | } | |
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); | |
398 | } | |
399 | ||
400 | } // detail namespace | |
401 | } // io namespace | |
402 | ||
403 | ||
404 | ||
405 | // ----------------------------------------------- | |
406 | // format :: parse(..) | |
407 | ||
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 | |
412 | using namespace std; | |
413 | #if !defined(BOOST_NO_STD_LOCALE) | |
414 | const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc()); | |
415 | #else | |
416 | io::basic_oaltstringstream<Ch, Tr, Alloc> fac; | |
417 | //has widen and narrow even on compilers without locale | |
418 | #endif | |
419 | ||
420 | const Ch arg_mark = io::detail::const_or_not(fac).widen( '%'); | |
421 | bool ordered_args=true; | |
422 | int max_argN=-1; | |
423 | ||
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); | |
427 | ||
428 | // B: Now the real parsing of the format string : | |
429 | num_items=0; | |
430 | typename string_type::size_type i0=0, i1=0; | |
431 | typename string_type::const_iterator it; | |
432 | bool special_things=false; | |
433 | int cur_item=0; | |
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); | |
438 | i1+=2; i0=i1; | |
439 | continue; | |
440 | } | |
441 | BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0); | |
442 | ||
443 | if(i1!=i0) { | |
444 | io::detail::append_string(piece, buf, i0, i1); | |
445 | i0=i1; | |
446 | } | |
447 | ++i1; | |
448 | it = buf.begin()+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 | |
453 | continue; | |
454 | i0=i1; | |
455 | items_[cur_item].compute_states(); // process complex options, like zeropad, into params | |
456 | ||
457 | int argN=items_[cur_item].argN_; | |
458 | if(argN == format_item_t::argN_ignored) | |
459 | continue; | |
460 | if(argN ==format_item_t::argN_no_posit) | |
461 | ordered_args=false; | |
462 | else if(argN == format_item_t::argN_tabulation) special_things=true; | |
463 | else if(argN > max_argN) max_argN = argN; | |
464 | ++num_items; | |
465 | ++cur_item; | |
466 | } // loop on %'s | |
467 | BOOST_ASSERT(cur_item == num_items); | |
468 | ||
469 | // store the final piece of string | |
470 | { | |
471 | string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_; | |
472 | io::detail::append_string(piece, buf, i0, buf.size()); | |
473 | } | |
474 | ||
475 | if( !ordered_args) { | |
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 | |
481 | } | |
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; | |
487 | ++non_ordered_items; | |
488 | } | |
489 | max_argN = non_ordered_items-1; | |
490 | } | |
491 | ||
492 | // C: set some member data : | |
493 | items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) ); | |
494 | ||
495 | if(special_things) style_ |= special_needs; | |
496 | num_args_ = max_argN + 1; | |
497 | if(ordered_args) style_ |= ordered; | |
498 | else style_ &= ~ordered; | |
499 | return *this; | |
500 | } | |
501 | ||
502 | } // namespace boost | |
503 | ||
504 | ||
505 | #endif // BOOST_FORMAT_PARSING_HPP |