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