sl@0
|
1 |
// ----------------------------------------------------------------------------
|
sl@0
|
2 |
// parsing.hpp : implementation of the parsing member functions
|
sl@0
|
3 |
// ( parse, parse_printf_directive)
|
sl@0
|
4 |
// ----------------------------------------------------------------------------
|
sl@0
|
5 |
|
sl@0
|
6 |
// Copyright Samuel Krempp 2003. Use, modification, and distribution are
|
sl@0
|
7 |
// subject to the Boost Software License, Version 1.0. (See accompanying
|
sl@0
|
8 |
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
9 |
|
sl@0
|
10 |
// see http://www.boost.org/libs/format for library home page
|
sl@0
|
11 |
|
sl@0
|
12 |
// ----------------------------------------------------------------------------
|
sl@0
|
13 |
|
sl@0
|
14 |
#ifndef BOOST_FORMAT_PARSING_HPP
|
sl@0
|
15 |
#define BOOST_FORMAT_PARSING_HPP
|
sl@0
|
16 |
|
sl@0
|
17 |
|
sl@0
|
18 |
#include <boost/format/format_class.hpp>
|
sl@0
|
19 |
#include <boost/throw_exception.hpp>
|
sl@0
|
20 |
#include <boost/assert.hpp>
|
sl@0
|
21 |
|
sl@0
|
22 |
|
sl@0
|
23 |
namespace boost {
|
sl@0
|
24 |
namespace io {
|
sl@0
|
25 |
namespace detail {
|
sl@0
|
26 |
|
sl@0
|
27 |
#if defined(BOOST_NO_STD_LOCALE)
|
sl@0
|
28 |
// streams will be used for narrow / widen. but these methods are not const
|
sl@0
|
29 |
template<class T>
|
sl@0
|
30 |
T& const_or_not(const T& x) {
|
sl@0
|
31 |
return const_cast<T&> (x);
|
sl@0
|
32 |
}
|
sl@0
|
33 |
#else
|
sl@0
|
34 |
template<class T>
|
sl@0
|
35 |
const T& const_or_not(const T& x) {
|
sl@0
|
36 |
return x;
|
sl@0
|
37 |
}
|
sl@0
|
38 |
#endif
|
sl@0
|
39 |
|
sl@0
|
40 |
template<class Ch, class Facet> inline
|
sl@0
|
41 |
char wrap_narrow(const Facet& fac, Ch c, char deflt) {
|
sl@0
|
42 |
return const_or_not(fac).narrow(c, deflt);
|
sl@0
|
43 |
}
|
sl@0
|
44 |
|
sl@0
|
45 |
template<class Ch, class Facet> inline
|
sl@0
|
46 |
bool wrap_isdigit(const Facet& fac, Ch c) {
|
sl@0
|
47 |
#if ! defined( BOOST_NO_LOCALE_ISDIGIT )
|
sl@0
|
48 |
return fac.is(std::ctype<Ch>::digit, c);
|
sl@0
|
49 |
# else
|
sl@0
|
50 |
using namespace std;
|
sl@0
|
51 |
return isdigit(c);
|
sl@0
|
52 |
#endif
|
sl@0
|
53 |
}
|
sl@0
|
54 |
|
sl@0
|
55 |
template<class Iter, class Facet>
|
sl@0
|
56 |
Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) {
|
sl@0
|
57 |
using namespace std;
|
sl@0
|
58 |
for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ;
|
sl@0
|
59 |
return beg;
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
|
sl@0
|
63 |
// Input : [start, last) iterators range and a
|
sl@0
|
64 |
// a Facet to use its widen/narrow member function
|
sl@0
|
65 |
// Effects : read sequence and convert digits into integral n, of type Res
|
sl@0
|
66 |
// Returns : n
|
sl@0
|
67 |
template<class Res, class Iter, class Facet>
|
sl@0
|
68 |
Iter str2int (const Iter & start, const Iter & last, Res & res,
|
sl@0
|
69 |
const Facet& fac)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
using namespace std;
|
sl@0
|
72 |
Iter it;
|
sl@0
|
73 |
res=0;
|
sl@0
|
74 |
for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) {
|
sl@0
|
75 |
char cur_ch = wrap_narrow(fac, *it, 0); // cant fail.
|
sl@0
|
76 |
res *= 10;
|
sl@0
|
77 |
res += cur_ch - '0'; // 22.2.1.1.2.13 of the C++ standard
|
sl@0
|
78 |
}
|
sl@0
|
79 |
return it;
|
sl@0
|
80 |
}
|
sl@0
|
81 |
|
sl@0
|
82 |
// skip printf's "asterisk-fields" directives in the format-string buf
|
sl@0
|
83 |
// Input : char string, with starting index *pos_p
|
sl@0
|
84 |
// a Facet merely to use its widen/narrow member function
|
sl@0
|
85 |
// Effects : advance *pos_p by skipping printf's asterisk fields.
|
sl@0
|
86 |
// Returns : nothing
|
sl@0
|
87 |
template<class Iter, class Facet>
|
sl@0
|
88 |
Iter skip_asterisk(Iter start, Iter last, const Facet& fac)
|
sl@0
|
89 |
{
|
sl@0
|
90 |
using namespace std;
|
sl@0
|
91 |
++ start;
|
sl@0
|
92 |
start = wrap_scan_notdigit(fac, start, last);
|
sl@0
|
93 |
if(start!=last && *start== const_or_not(fac).widen( '$') )
|
sl@0
|
94 |
++start;
|
sl@0
|
95 |
return start;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
|
sl@0
|
99 |
// auxiliary func called by parse_printf_directive
|
sl@0
|
100 |
// for centralising error handling
|
sl@0
|
101 |
// it either throws if user sets the corresponding flag, or does nothing.
|
sl@0
|
102 |
inline void maybe_throw_exception(unsigned char exceptions,
|
sl@0
|
103 |
std::size_t pos, std::size_t size)
|
sl@0
|
104 |
{
|
sl@0
|
105 |
if(exceptions & io::bad_format_string_bit)
|
sl@0
|
106 |
boost::throw_exception(io::bad_format_string(pos, size) );
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
|
sl@0
|
110 |
// Input: the position of a printf-directive in the format-string
|
sl@0
|
111 |
// a basic_ios& merely to use its widen/narrow member function
|
sl@0
|
112 |
// a bitset'exceptions' telling whether to throw exceptions on errors.
|
sl@0
|
113 |
// Returns:
|
sl@0
|
114 |
// true if parse succeeded (ignore some errors if exceptions disabled)
|
sl@0
|
115 |
// false if it failed so bad that the directive should be printed verbatim
|
sl@0
|
116 |
// Effects:
|
sl@0
|
117 |
// start is incremented so that *start is the first char after
|
sl@0
|
118 |
// this directive
|
sl@0
|
119 |
// *fpar is set with the parameters read in the directive
|
sl@0
|
120 |
template<class Ch, class Tr, class Alloc, class Iter, class Facet>
|
sl@0
|
121 |
bool parse_printf_directive(Iter & start, const Iter& last,
|
sl@0
|
122 |
detail::format_item<Ch, Tr, Alloc> * fpar,
|
sl@0
|
123 |
const Facet& fac,
|
sl@0
|
124 |
std::size_t offset, unsigned char exceptions)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
|
sl@0
|
127 |
|
sl@0
|
128 |
fpar->argN_ = format_item_t::argN_no_posit; // if no positional-directive
|
sl@0
|
129 |
bool precision_set = false;
|
sl@0
|
130 |
bool in_brackets=false;
|
sl@0
|
131 |
Iter start0 = start;
|
sl@0
|
132 |
std::size_t fstring_size = last-start0+offset;
|
sl@0
|
133 |
if(*start== const_or_not(fac).widen( '|')) {
|
sl@0
|
134 |
in_brackets=true;
|
sl@0
|
135 |
if( ++start >= last ) {
|
sl@0
|
136 |
maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
|
sl@0
|
137 |
return false;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
}
|
sl@0
|
140 |
|
sl@0
|
141 |
// the flag '0' would be picked as a digit for argument order, but here it's a flag :
|
sl@0
|
142 |
if(*start== const_or_not(fac).widen( '0'))
|
sl@0
|
143 |
goto parse_flags;
|
sl@0
|
144 |
|
sl@0
|
145 |
// handle argument order (%2$d) or possibly width specification: %2d
|
sl@0
|
146 |
if(wrap_isdigit(fac, *start)) {
|
sl@0
|
147 |
int n;
|
sl@0
|
148 |
start = str2int(start, last, n, fac);
|
sl@0
|
149 |
if( start >= last ) {
|
sl@0
|
150 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
151 |
return false;
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
// %N% case : this is already the end of the directive
|
sl@0
|
155 |
if( *start == const_or_not(fac).widen( '%') ) {
|
sl@0
|
156 |
fpar->argN_ = n-1;
|
sl@0
|
157 |
++start;
|
sl@0
|
158 |
if( in_brackets)
|
sl@0
|
159 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
160 |
// but don't return. maybe "%" was used in lieu of '$', so we go on.
|
sl@0
|
161 |
else
|
sl@0
|
162 |
return true;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
if ( *start== const_or_not(fac).widen( '$') ) {
|
sl@0
|
166 |
fpar->argN_ = n-1;
|
sl@0
|
167 |
++start;
|
sl@0
|
168 |
}
|
sl@0
|
169 |
else {
|
sl@0
|
170 |
// non-positionnal directive
|
sl@0
|
171 |
fpar->fmtstate_.width_ = n;
|
sl@0
|
172 |
fpar->argN_ = format_item_t::argN_no_posit;
|
sl@0
|
173 |
goto parse_precision;
|
sl@0
|
174 |
}
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
parse_flags:
|
sl@0
|
178 |
// handle flags
|
sl@0
|
179 |
while ( start != last) { // as long as char is one of + - = _ # 0 l h or ' '
|
sl@0
|
180 |
// misc switches
|
sl@0
|
181 |
switch ( wrap_narrow(fac, *start, 0)) {
|
sl@0
|
182 |
case '\'' : break; // no effect yet. (painful to implement)
|
sl@0
|
183 |
case 'l':
|
sl@0
|
184 |
case 'h': // short/long modifier : for printf-comaptibility (no action needed)
|
sl@0
|
185 |
break;
|
sl@0
|
186 |
case '-':
|
sl@0
|
187 |
fpar->fmtstate_.flags_ |= std::ios_base::left;
|
sl@0
|
188 |
break;
|
sl@0
|
189 |
case '=':
|
sl@0
|
190 |
fpar->pad_scheme_ |= format_item_t::centered;
|
sl@0
|
191 |
break;
|
sl@0
|
192 |
case '_':
|
sl@0
|
193 |
fpar->fmtstate_.flags_ |= std::ios_base::internal;
|
sl@0
|
194 |
break;
|
sl@0
|
195 |
case ' ':
|
sl@0
|
196 |
fpar->pad_scheme_ |= format_item_t::spacepad;
|
sl@0
|
197 |
break;
|
sl@0
|
198 |
case '+':
|
sl@0
|
199 |
fpar->fmtstate_.flags_ |= std::ios_base::showpos;
|
sl@0
|
200 |
break;
|
sl@0
|
201 |
case '0':
|
sl@0
|
202 |
fpar->pad_scheme_ |= format_item_t::zeropad;
|
sl@0
|
203 |
// need to know alignment before really setting flags,
|
sl@0
|
204 |
// so just add 'zeropad' flag for now, it will be processed later.
|
sl@0
|
205 |
break;
|
sl@0
|
206 |
case '#':
|
sl@0
|
207 |
fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
|
sl@0
|
208 |
break;
|
sl@0
|
209 |
default:
|
sl@0
|
210 |
goto parse_width;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
++start;
|
sl@0
|
213 |
} // loop on flag.
|
sl@0
|
214 |
|
sl@0
|
215 |
if( start>=last) {
|
sl@0
|
216 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
217 |
return true;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
parse_width:
|
sl@0
|
220 |
// handle width spec
|
sl@0
|
221 |
// first skip 'asterisk fields' : *, or *N$
|
sl@0
|
222 |
if(*start == const_or_not(fac).widen( '*') )
|
sl@0
|
223 |
start = skip_asterisk(start, last, fac);
|
sl@0
|
224 |
if(start!=last && wrap_isdigit(fac, *start))
|
sl@0
|
225 |
start = str2int(start, last, fpar->fmtstate_.width_, fac);
|
sl@0
|
226 |
|
sl@0
|
227 |
parse_precision:
|
sl@0
|
228 |
if( start>= last) {
|
sl@0
|
229 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
230 |
return true;
|
sl@0
|
231 |
}
|
sl@0
|
232 |
// handle precision spec
|
sl@0
|
233 |
if (*start== const_or_not(fac).widen( '.')) {
|
sl@0
|
234 |
++start;
|
sl@0
|
235 |
if(start != last && *start == const_or_not(fac).widen( '*') )
|
sl@0
|
236 |
start = skip_asterisk(start, last, fac);
|
sl@0
|
237 |
if(start != last && wrap_isdigit(fac, *start)) {
|
sl@0
|
238 |
start = str2int(start, last, fpar->fmtstate_.precision_, fac);
|
sl@0
|
239 |
precision_set = true;
|
sl@0
|
240 |
}
|
sl@0
|
241 |
else
|
sl@0
|
242 |
fpar->fmtstate_.precision_ =0;
|
sl@0
|
243 |
}
|
sl@0
|
244 |
|
sl@0
|
245 |
// handle formatting-type flags :
|
sl@0
|
246 |
while( start != last && ( *start== const_or_not(fac).widen( 'l')
|
sl@0
|
247 |
|| *start== const_or_not(fac).widen( 'L')
|
sl@0
|
248 |
|| *start== const_or_not(fac).widen( 'h')) )
|
sl@0
|
249 |
++start;
|
sl@0
|
250 |
if( start>=last) {
|
sl@0
|
251 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
252 |
return true;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
|
sl@0
|
255 |
if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
|
sl@0
|
256 |
++start;
|
sl@0
|
257 |
return true;
|
sl@0
|
258 |
}
|
sl@0
|
259 |
switch ( wrap_narrow(fac, *start, 0) ) {
|
sl@0
|
260 |
case 'X':
|
sl@0
|
261 |
fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
|
sl@0
|
262 |
case 'p': // pointer => set hex.
|
sl@0
|
263 |
case 'x':
|
sl@0
|
264 |
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
|
sl@0
|
265 |
fpar->fmtstate_.flags_ |= std::ios_base::hex;
|
sl@0
|
266 |
break;
|
sl@0
|
267 |
|
sl@0
|
268 |
case 'o':
|
sl@0
|
269 |
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
|
sl@0
|
270 |
fpar->fmtstate_.flags_ |= std::ios_base::oct;
|
sl@0
|
271 |
break;
|
sl@0
|
272 |
|
sl@0
|
273 |
case 'E':
|
sl@0
|
274 |
fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
|
sl@0
|
275 |
case 'e':
|
sl@0
|
276 |
fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
|
sl@0
|
277 |
fpar->fmtstate_.flags_ |= std::ios_base::scientific;
|
sl@0
|
278 |
|
sl@0
|
279 |
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
|
sl@0
|
280 |
fpar->fmtstate_.flags_ |= std::ios_base::dec;
|
sl@0
|
281 |
break;
|
sl@0
|
282 |
|
sl@0
|
283 |
case 'f':
|
sl@0
|
284 |
fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
|
sl@0
|
285 |
fpar->fmtstate_.flags_ |= std::ios_base::fixed;
|
sl@0
|
286 |
case 'u':
|
sl@0
|
287 |
case 'd':
|
sl@0
|
288 |
case 'i':
|
sl@0
|
289 |
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
|
sl@0
|
290 |
fpar->fmtstate_.flags_ |= std::ios_base::dec;
|
sl@0
|
291 |
break;
|
sl@0
|
292 |
|
sl@0
|
293 |
case 'T':
|
sl@0
|
294 |
++start;
|
sl@0
|
295 |
if( start >= last)
|
sl@0
|
296 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
297 |
else
|
sl@0
|
298 |
fpar->fmtstate_.fill_ = *start;
|
sl@0
|
299 |
fpar->pad_scheme_ |= format_item_t::tabulation;
|
sl@0
|
300 |
fpar->argN_ = format_item_t::argN_tabulation;
|
sl@0
|
301 |
break;
|
sl@0
|
302 |
case 't':
|
sl@0
|
303 |
fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
|
sl@0
|
304 |
fpar->pad_scheme_ |= format_item_t::tabulation;
|
sl@0
|
305 |
fpar->argN_ = format_item_t::argN_tabulation;
|
sl@0
|
306 |
break;
|
sl@0
|
307 |
|
sl@0
|
308 |
case 'G':
|
sl@0
|
309 |
fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
|
sl@0
|
310 |
break;
|
sl@0
|
311 |
case 'g': // 'g' conversion is default for floats.
|
sl@0
|
312 |
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
|
sl@0
|
313 |
fpar->fmtstate_.flags_ |= std::ios_base::dec;
|
sl@0
|
314 |
|
sl@0
|
315 |
// CLEAR all floatield flags, so stream will CHOOSE
|
sl@0
|
316 |
fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
|
sl@0
|
317 |
break;
|
sl@0
|
318 |
|
sl@0
|
319 |
case 'C':
|
sl@0
|
320 |
case 'c':
|
sl@0
|
321 |
fpar->truncate_ = 1;
|
sl@0
|
322 |
break;
|
sl@0
|
323 |
case 'S':
|
sl@0
|
324 |
case 's':
|
sl@0
|
325 |
if(precision_set) // handle truncation manually, with own parameter.
|
sl@0
|
326 |
fpar->truncate_ = fpar->fmtstate_.precision_;
|
sl@0
|
327 |
fpar->fmtstate_.precision_ = 6; // default stream precision.
|
sl@0
|
328 |
break;
|
sl@0
|
329 |
case 'n' :
|
sl@0
|
330 |
fpar->argN_ = format_item_t::argN_ignored;
|
sl@0
|
331 |
break;
|
sl@0
|
332 |
default:
|
sl@0
|
333 |
maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
334 |
}
|
sl@0
|
335 |
++start;
|
sl@0
|
336 |
|
sl@0
|
337 |
if( in_brackets ) {
|
sl@0
|
338 |
if( start != last && *start== const_or_not(fac).widen( '|') ) {
|
sl@0
|
339 |
++start;
|
sl@0
|
340 |
return true;
|
sl@0
|
341 |
}
|
sl@0
|
342 |
else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
|
sl@0
|
343 |
}
|
sl@0
|
344 |
return true;
|
sl@0
|
345 |
}
|
sl@0
|
346 |
// -end parse_printf_directive()
|
sl@0
|
347 |
|
sl@0
|
348 |
template<class String, class Facet>
|
sl@0
|
349 |
int upper_bound_from_fstring(const String& buf,
|
sl@0
|
350 |
const typename String::value_type arg_mark,
|
sl@0
|
351 |
const Facet& fac,
|
sl@0
|
352 |
unsigned char exceptions)
|
sl@0
|
353 |
{
|
sl@0
|
354 |
// quick-parsing of the format-string to count arguments mark (arg_mark, '%')
|
sl@0
|
355 |
// returns : upper bound on the number of format items in the format strings
|
sl@0
|
356 |
using namespace boost::io;
|
sl@0
|
357 |
typename String::size_type i1=0;
|
sl@0
|
358 |
int num_items=0;
|
sl@0
|
359 |
while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
|
sl@0
|
360 |
if( i1+1 >= buf.size() ) {
|
sl@0
|
361 |
if(exceptions & bad_format_string_bit)
|
sl@0
|
362 |
boost::throw_exception(bad_format_string(i1, buf.size() )); // must not end in ".. %"
|
sl@0
|
363 |
else break; // stop there, ignore last '%'
|
sl@0
|
364 |
}
|
sl@0
|
365 |
if(buf[i1+1] == buf[i1] ) {// escaped "%%"
|
sl@0
|
366 |
i1+=2; continue;
|
sl@0
|
367 |
}
|
sl@0
|
368 |
|
sl@0
|
369 |
++i1;
|
sl@0
|
370 |
// in case of %N% directives, dont count it double (wastes allocations..) :
|
sl@0
|
371 |
i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
|
sl@0
|
372 |
if( i1 < buf.size() && buf[i1] == arg_mark )
|
sl@0
|
373 |
++i1;
|
sl@0
|
374 |
++num_items;
|
sl@0
|
375 |
}
|
sl@0
|
376 |
return num_items;
|
sl@0
|
377 |
}
|
sl@0
|
378 |
template<class String> inline
|
sl@0
|
379 |
void append_string(String& dst, const String& src,
|
sl@0
|
380 |
const typename String::size_type beg,
|
sl@0
|
381 |
const typename String::size_type end) {
|
sl@0
|
382 |
#if !defined(BOOST_NO_STRING_APPEND)
|
sl@0
|
383 |
dst.append(src.begin()+beg, src.begin()+end);
|
sl@0
|
384 |
#else
|
sl@0
|
385 |
dst += src.substr(beg, end-beg);
|
sl@0
|
386 |
#endif
|
sl@0
|
387 |
}
|
sl@0
|
388 |
|
sl@0
|
389 |
} // detail namespace
|
sl@0
|
390 |
} // io namespace
|
sl@0
|
391 |
|
sl@0
|
392 |
|
sl@0
|
393 |
|
sl@0
|
394 |
// -----------------------------------------------
|
sl@0
|
395 |
// format :: parse(..)
|
sl@0
|
396 |
|
sl@0
|
397 |
template<class Ch, class Tr, class Alloc>
|
sl@0
|
398 |
basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
|
sl@0
|
399 |
parse (const string_type& buf) {
|
sl@0
|
400 |
// parse the format-string
|
sl@0
|
401 |
using namespace std;
|
sl@0
|
402 |
#if !defined(BOOST_NO_STD_LOCALE)
|
sl@0
|
403 |
const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
|
sl@0
|
404 |
#else
|
sl@0
|
405 |
io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
|
sl@0
|
406 |
//has widen and narrow even on compilers without locale
|
sl@0
|
407 |
#endif
|
sl@0
|
408 |
|
sl@0
|
409 |
const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
|
sl@0
|
410 |
bool ordered_args=true;
|
sl@0
|
411 |
int max_argN=-1;
|
sl@0
|
412 |
|
sl@0
|
413 |
// A: find upper_bound on num_items and allocates arrays
|
sl@0
|
414 |
int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
|
sl@0
|
415 |
make_or_reuse_data(num_items);
|
sl@0
|
416 |
|
sl@0
|
417 |
// B: Now the real parsing of the format string :
|
sl@0
|
418 |
num_items=0;
|
sl@0
|
419 |
typename string_type::size_type i0=0, i1=0;
|
sl@0
|
420 |
typename string_type::const_iterator it;
|
sl@0
|
421 |
bool special_things=false;
|
sl@0
|
422 |
int cur_item=0;
|
sl@0
|
423 |
while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
|
sl@0
|
424 |
string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
|
sl@0
|
425 |
if( buf[i1+1] == buf[i1] ) { // escaped mark, '%%'
|
sl@0
|
426 |
io::detail::append_string(piece, buf, i0, i1+1);
|
sl@0
|
427 |
i1+=2; i0=i1;
|
sl@0
|
428 |
continue;
|
sl@0
|
429 |
}
|
sl@0
|
430 |
BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
|
sl@0
|
431 |
|
sl@0
|
432 |
if(i1!=i0)
|
sl@0
|
433 |
io::detail::append_string(piece, buf, i0, i1);
|
sl@0
|
434 |
++i1;
|
sl@0
|
435 |
it = buf.begin()+i1;
|
sl@0
|
436 |
bool parse_ok = io::detail::parse_printf_directive(
|
sl@0
|
437 |
it, buf.end(), &items_[cur_item], fac, i1, exceptions());
|
sl@0
|
438 |
i1 = it - buf.begin();
|
sl@0
|
439 |
if( ! parse_ok ) // the directive will be printed verbatim
|
sl@0
|
440 |
continue;
|
sl@0
|
441 |
i0=i1;
|
sl@0
|
442 |
items_[cur_item].compute_states(); // process complex options, like zeropad, into params
|
sl@0
|
443 |
|
sl@0
|
444 |
int argN=items_[cur_item].argN_;
|
sl@0
|
445 |
if(argN == format_item_t::argN_ignored)
|
sl@0
|
446 |
continue;
|
sl@0
|
447 |
if(argN ==format_item_t::argN_no_posit)
|
sl@0
|
448 |
ordered_args=false;
|
sl@0
|
449 |
else if(argN == format_item_t::argN_tabulation) special_things=true;
|
sl@0
|
450 |
else if(argN > max_argN) max_argN = argN;
|
sl@0
|
451 |
++num_items;
|
sl@0
|
452 |
++cur_item;
|
sl@0
|
453 |
} // loop on %'s
|
sl@0
|
454 |
BOOST_ASSERT(cur_item == num_items);
|
sl@0
|
455 |
|
sl@0
|
456 |
// store the final piece of string
|
sl@0
|
457 |
{
|
sl@0
|
458 |
string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
|
sl@0
|
459 |
io::detail::append_string(piece, buf, i0, buf.size());
|
sl@0
|
460 |
}
|
sl@0
|
461 |
|
sl@0
|
462 |
if( !ordered_args) {
|
sl@0
|
463 |
if(max_argN >= 0 ) { // dont mix positional with non-positionnal directives
|
sl@0
|
464 |
if(exceptions() & io::bad_format_string_bit)
|
sl@0
|
465 |
boost::throw_exception(io::bad_format_string(max_argN, 0));
|
sl@0
|
466 |
// else do nothing. => positionnal arguments are processed as non-positionnal
|
sl@0
|
467 |
}
|
sl@0
|
468 |
// set things like it would have been with positional directives :
|
sl@0
|
469 |
int non_ordered_items = 0;
|
sl@0
|
470 |
for(int i=0; i< num_items; ++i)
|
sl@0
|
471 |
if(items_[i].argN_ == format_item_t::argN_no_posit) {
|
sl@0
|
472 |
items_[i].argN_ = non_ordered_items;
|
sl@0
|
473 |
++non_ordered_items;
|
sl@0
|
474 |
}
|
sl@0
|
475 |
max_argN = non_ordered_items-1;
|
sl@0
|
476 |
}
|
sl@0
|
477 |
|
sl@0
|
478 |
// C: set some member data :
|
sl@0
|
479 |
items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
|
sl@0
|
480 |
|
sl@0
|
481 |
if(special_things) style_ |= special_needs;
|
sl@0
|
482 |
num_args_ = max_argN + 1;
|
sl@0
|
483 |
if(ordered_args) style_ |= ordered;
|
sl@0
|
484 |
else style_ &= ~ordered;
|
sl@0
|
485 |
return *this;
|
sl@0
|
486 |
}
|
sl@0
|
487 |
|
sl@0
|
488 |
} // namespace boost
|
sl@0
|
489 |
|
sl@0
|
490 |
|
sl@0
|
491 |
#endif // BOOST_FORMAT_PARSING_HPP
|