📄 parsing.hpp
字号:
|| *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': // pointer => set hex.
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': // 'g' conversion is default for floats.
fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;
fpar->fmtstate_.flags_ |= std::ios_base::dec;
// CLEAR all floatield flags, so stream will CHOOSE
fpar->fmtstate_.flags_ &= ~std::ios_base::floatfield;
break;
case 'C':
case 'c':
fpar->truncate_ = 1;
break;
case 'S':
case 's':
if(precision_set) // handle truncation manually, with own parameter.
fpar->truncate_ = fpar->fmtstate_.precision_;
fpar->fmtstate_.precision_ = 6; // default stream precision.
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;
}
// -end parse_printf_directive()
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)
{
// quick-parsing of the format-string to count arguments mark (arg_mark, '%')
// returns : upper bound on the number of format items in the format strings
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() )); // must not end in ".. %"
else break; // stop there, ignore last '%'
}
if(buf[i1+1] == buf[i1] ) {// escaped "%%"
i1+=2; continue;
}
++i1;
// in case of %N% directives, dont count it double (wastes allocations..) :
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) {
#if !defined(BOOST_NO_STRING_APPEND)
dst.append(src.begin()+beg, src.begin()+end);
#else
dst += src.substr(beg, end-beg);
#endif
}
} // detail namespace
} // io namespace
// -----------------------------------------------
// format :: parse(..)
template<class Ch, class Tr, class Alloc>
basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
parse (const string_type& buf) {
// parse the format-string
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;
//has widen and narrow even on compilers without locale
#endif
const Ch arg_mark = io::detail::const_or_not(fac).widen( '%');
bool ordered_args=true;
int max_argN=-1;
// A: find upper_bound on num_items and allocates arrays
int num_items = io::detail::upper_bound_from_fstring(buf, arg_mark, fac, exceptions());
make_or_reuse_data(num_items);
// B: Now the real parsing of the format string :
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] ) { // escaped mark, '%%'
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);
++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 ) // the directive will be printed verbatim
continue;
i0=i1;
items_[cur_item].compute_states(); // process complex options, like zeropad, into params
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;
} // loop on %'s
BOOST_ASSERT(cur_item == num_items);
// store the final piece of string
{
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 ) { // dont mix positional with non-positionnal directives
if(exceptions() & io::bad_format_string_bit)
boost::throw_exception(io::bad_format_string(max_argN, 0));
// else do nothing. => positionnal arguments are processed as non-positionnal
}
// set things like it would have been with positional directives :
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;
}
// C: set some member data :
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;
}
} // namespace boost
#endif // BOOST_FORMAT_PARSING_HPP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -