123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #ifndef BOOST_FORMAT_PARSING_HPP
- #define BOOST_FORMAT_PARSING_HPP
- #include <boost/format/format_class.hpp>
- #include <boost/format/exceptions.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/assert.hpp>
- namespace boost {
- namespace io {
- namespace detail {
- #if defined(BOOST_NO_STD_LOCALE)
-
- template<class T>
- T& const_or_not(const T& x) {
- return const_cast<T&> (x);
- }
- #else
- template<class T>
- const T& const_or_not(const T& x) {
- return x;
- }
- #endif
- template<class Ch, class Facet> inline
- char wrap_narrow(const Facet& fac, Ch c, char deflt) {
- return const_or_not(fac).narrow(c, deflt);
- }
- template<class Ch, class Facet> inline
- bool wrap_isdigit(const Facet& fac, Ch c) {
- #if ! defined( BOOST_NO_LOCALE_ISDIGIT )
- return fac.is(std::ctype<Ch>::digit, c);
- # else
- (void) fac;
- using namespace std;
- return isdigit(c) != 0;
- #endif
- }
-
- template<class Iter, class Facet>
- Iter wrap_scan_notdigit(const Facet & fac, Iter beg, Iter end) {
- using namespace std;
- for( ; beg!=end && wrap_isdigit(fac, *beg); ++beg) ;
- return beg;
- }
-
-
-
-
- template<class Res, class Iter, class Facet>
- Iter str2int (const Iter & start, const Iter & last, Res & res,
- const Facet& fac)
- {
- using namespace std;
- Iter it;
- res=0;
- for(it=start; it != last && wrap_isdigit(fac, *it); ++it ) {
- char cur_ch = wrap_narrow(fac, *it, 0);
- res *= 10;
- res += cur_ch - '0';
- }
- return it;
- }
-
-
-
-
-
- template<class Iter, class Facet>
- Iter skip_asterisk(Iter start, Iter last, const Facet& fac)
- {
- using namespace std;
- ++ start;
- start = wrap_scan_notdigit(fac, start, last);
- if(start!=last && *start== const_or_not(fac).widen( '$') )
- ++start;
- return start;
- }
-
-
-
- inline void maybe_throw_exception(unsigned char exceptions,
- std::size_t pos, std::size_t size)
- {
- if(exceptions & io::bad_format_string_bit)
- boost::throw_exception(io::bad_format_string(pos, size) );
- }
-
-
-
-
-
-
-
-
-
-
-
- template<class Ch, class Tr, class Alloc, class Iter, class Facet>
- bool parse_printf_directive(Iter & start, const Iter& last,
- detail::format_item<Ch, Tr, Alloc> * fpar,
- const Facet& fac,
- std::size_t offset, unsigned char exceptions)
- {
- typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
- fpar->argN_ = format_item_t::argN_no_posit;
- bool precision_set = false;
- bool in_brackets=false;
- Iter start0 = start;
- std::size_t fstring_size = last-start0+offset;
- if(start>= last) {
- maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
- return false;
- }
-
- if(*start== const_or_not(fac).widen( '|')) {
- in_brackets=true;
- if( ++start >= last ) {
- maybe_throw_exception(exceptions, start-start0 + offset, fstring_size);
- return false;
- }
- }
-
- if(*start== const_or_not(fac).widen( '0'))
- goto parse_flags;
-
- if(wrap_isdigit(fac, *start)) {
- int n;
- start = str2int(start, last, n, fac);
- if( start >= last ) {
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- return false;
- }
-
-
- if( *start == const_or_not(fac).widen( '%') ) {
- fpar->argN_ = n-1;
- ++start;
- if( in_brackets)
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
-
- else
- return true;
- }
- if ( *start== const_or_not(fac).widen( '$') ) {
- fpar->argN_ = n-1;
- ++start;
- }
- else {
-
- fpar->fmtstate_.width_ = n;
- fpar->argN_ = format_item_t::argN_no_posit;
- goto parse_precision;
- }
- }
-
- parse_flags:
-
- while ( start != last) {
-
- switch ( wrap_narrow(fac, *start, 0)) {
- case '\'' : break;
- case 'l':
- case 'h':
- break;
- case '-':
- fpar->fmtstate_.flags_ |= std::ios_base::left;
- break;
- case '=':
- fpar->pad_scheme_ |= format_item_t::centered;
- break;
- case '_':
- fpar->fmtstate_.flags_ |= std::ios_base::internal;
- break;
- case ' ':
- fpar->pad_scheme_ |= format_item_t::spacepad;
- break;
- case '+':
- fpar->fmtstate_.flags_ |= std::ios_base::showpos;
- break;
- case '0':
- fpar->pad_scheme_ |= format_item_t::zeropad;
-
-
- break;
- case '#':
- fpar->fmtstate_.flags_ |= std::ios_base::showpoint | std::ios_base::showbase;
- break;
- default:
- goto parse_width;
- }
- ++start;
- }
- if( start>=last) {
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- return true;
- }
- parse_width:
-
-
- if(*start == const_or_not(fac).widen( '*') )
- start = skip_asterisk(start, last, fac);
- if(start!=last && wrap_isdigit(fac, *start))
- start = str2int(start, last, fpar->fmtstate_.width_, fac);
- parse_precision:
- if( start>= last) {
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- return true;
- }
-
- if (*start== const_or_not(fac).widen( '.')) {
- ++start;
- if(start != last && *start == const_or_not(fac).widen( '*') )
- start = skip_asterisk(start, last, fac);
- if(start != last && wrap_isdigit(fac, *start)) {
- start = str2int(start, last, fpar->fmtstate_.precision_, fac);
- precision_set = true;
- }
- else
- fpar->fmtstate_.precision_ =0;
- }
-
-
- while( start != last && ( *start== const_or_not(fac).widen( 'l')
- || *start== const_or_not(fac).widen( 'L')
- || *start== const_or_not(fac).widen( 'h')) )
- ++start;
- if( start>=last) {
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- return true;
- }
- if( in_brackets && *start== const_or_not(fac).widen( '|') ) {
- ++start;
- return true;
- }
- switch ( wrap_narrow(fac, *start, 0) ) {
- case 'X':
- fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
- case 'p':
- case 'x':
- fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
- fpar->fmtstate_.flags_ |= std::ios_base::hex;
- break;
- case 'o':
- fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
- fpar->fmtstate_.flags_ |= std::ios_base::oct;
- break;
- case 'E':
- fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
- case 'e':
- fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
- fpar->fmtstate_.flags_ |= std::ios_base::scientific;
- fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
- fpar->fmtstate_.flags_ |= std::ios_base::dec;
- break;
-
- case 'f':
- fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
- fpar->fmtstate_.flags_ |= std::ios_base::fixed;
- case 'u':
- case 'd':
- case 'i':
- fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
- fpar->fmtstate_.flags_ |= std::ios_base::dec;
- break;
- case 'T':
- ++start;
- if( start >= last)
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- else
- fpar->fmtstate_.fill_ = *start;
- fpar->pad_scheme_ |= format_item_t::tabulation;
- fpar->argN_ = format_item_t::argN_tabulation;
- break;
- case 't':
- fpar->fmtstate_.fill_ = const_or_not(fac).widen( ' ');
- fpar->pad_scheme_ |= format_item_t::tabulation;
- fpar->argN_ = format_item_t::argN_tabulation;
- break;
- case 'G':
- fpar->fmtstate_.flags_ |= std::ios_base::uppercase;
- break;
- case 'g':
- fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
- fpar->fmtstate_.flags_ |= std::ios_base::dec;
-
- fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
- break;
- case 'C':
- case 'c':
- fpar->truncate_ = 1;
- break;
- case 'S':
- case 's':
- if(precision_set)
- fpar->truncate_ = fpar->fmtstate_.precision_;
- fpar->fmtstate_.precision_ = 6;
- break;
- case 'n' :
- fpar->argN_ = format_item_t::argN_ignored;
- break;
- default:
- maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- }
- ++start;
- if( in_brackets ) {
- if( start != last && *start== const_or_not(fac).widen( '|') ) {
- ++start;
- return true;
- }
- else maybe_throw_exception(exceptions, start-start0+offset, fstring_size);
- }
- return true;
- }
-
- template<class String, class Facet>
- int upper_bound_from_fstring(const String& buf,
- const typename String::value_type arg_mark,
- const Facet& fac,
- unsigned char exceptions)
- {
-
-
- using namespace boost::io;
- typename String::size_type i1=0;
- int num_items=0;
- while( (i1=buf.find(arg_mark,i1)) != String::npos ) {
- if( i1+1 >= buf.size() ) {
- if(exceptions & bad_format_string_bit)
- boost::throw_exception(bad_format_string(i1, buf.size() ));
- else {
- ++num_items;
- break;
- }
- }
- if(buf[i1+1] == buf[i1] ) {
- i1+=2; continue;
- }
- ++i1;
-
- i1 = detail::wrap_scan_notdigit(fac, buf.begin()+i1, buf.end()) - buf.begin();
- if( i1 < buf.size() && buf[i1] == arg_mark )
- ++i1;
- ++num_items;
- }
- return num_items;
- }
- template<class String> inline
- void append_string(String& dst, const String& src,
- const typename String::size_type beg,
- const typename String::size_type end) {
- dst.append(src.begin()+beg, src.begin()+end);
- }
- }
- }
- template<class Ch, class Tr, class Alloc>
- basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
- parse (const string_type& buf) {
-
- using namespace std;
- #if !defined(BOOST_NO_STD_LOCALE)
- const std::ctype<Ch> & fac = BOOST_USE_FACET( std::ctype<Ch>, getloc());
- #else
- io::basic_oaltstringstream<Ch, Tr, Alloc> fac;
-
- #endif
- const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
- bool ordered_args=true;
- int max_argN=-1;
-
- int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
- make_or_reuse_data(num_items);
-
- num_items=0;
- typename string_type::size_type i0=0, i1=0;
- typename string_type::const_iterator it;
- bool special_things=false;
- int cur_item=0;
- while( (i1=buf.find(arg_mark,i1)) != string_type::npos ) {
- string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
- if( buf[i1+1] == buf[i1] ) {
- io::detail::append_string(piece, buf, i0, i1+1);
- i1+=2; i0=i1;
- continue;
- }
- BOOST_ASSERT( static_cast<unsigned int>(cur_item) < items_.size() || cur_item==0);
- if(i1!=i0) {
- io::detail::append_string(piece, buf, i0, i1);
- i0=i1;
- }
- ++i1;
- it = buf.begin()+i1;
- bool parse_ok = io::detail::parse_printf_directive(
- it, buf.end(), &items_[cur_item], fac, i1, exceptions());
- i1 = it - buf.begin();
- if( ! parse_ok )
- continue;
- i0=i1;
- items_[cur_item].compute_states();
- int argN=items_[cur_item].argN_;
- if(argN == format_item_t::argN_ignored)
- continue;
- if(argN ==format_item_t::argN_no_posit)
- ordered_args=false;
- else if(argN == format_item_t::argN_tabulation) special_things=true;
- else if(argN > max_argN) max_argN = argN;
- ++num_items;
- ++cur_item;
- }
- BOOST_ASSERT(cur_item == num_items);
-
-
- {
- string_type & piece = (cur_item==0) ? prefix_ : items_[cur_item-1].appendix_;
- io::detail::append_string(piece, buf, i0, buf.size());
- }
-
- if( !ordered_args) {
- if(max_argN >= 0 ) {
- if(exceptions() & io::bad_format_string_bit)
- boost::throw_exception(
- io::bad_format_string(static_cast<std::size_t>(max_argN), 0));
-
- }
-
- int non_ordered_items = 0;
- for(int i=0; i< num_items; ++i)
- if(items_[i].argN_ == format_item_t::argN_no_posit) {
- items_[i].argN_ = non_ordered_items;
- ++non_ordered_items;
- }
- max_argN = non_ordered_items-1;
- }
-
-
- items_.resize(num_items, format_item_t(io::detail::const_or_not(fac).widen( ' ')) );
- if(special_things) style_ |= special_needs;
- num_args_ = max_argN + 1;
- if(ordered_args) style_ |= ordered;
- else style_ &= ~ordered;
- return *this;
- }
- }
- #endif
|