BOOST_PROGRAM_OPTIONS_DECL std::wstring from_local_8_bit(const std::string& s) { typedef codecvt<wchar_t, char, mbstate_t> facet_type; return from_8_bit(s, BOOST_USE_FACET(facet_type, locale())); }
std::string to_local_8_bit(const std::wstring& s) { typedef codecvt<wchar_t, char, mbstate_t> facet_type; return to_8_bit(s, BOOST_USE_FACET(facet_type, locale())); }
std::wstring widen_string( const std::string & str, const std::locale & loc = std::locale() ) { std::wstring result; const std::string::size_type len = str.length(); if(len != 0) { result.resize(len); BOOST_USE_FACET(std::ctype<wchar_t>, loc) .widen(&str[0], 1 + &str[len-1], &result[0]); } return result; }
void print_cpp_char(charT c) { #ifndef BOOST_NO_STD_LOCALE std::locale l; const std::collate<charT>& col = BOOST_USE_FACET(std::collate<charT>, l); std::basic_string<charT> result = col.transform(&c, &c+1); std::cout << result.size() << " "; print_string(result); std::size_t n = result.find(charT(0)); if(n != std::basic_string<charT>::npos) { std::cerr << "(Error in location of null, found: " << n << ")"; } #endif }
void basic_format<Ch, Tr, Alloc>:: make_or_reuse_data (std::size_t nbitems) { #if !defined(BOOST_NO_STD_LOCALE) Ch fill = ( BOOST_USE_FACET(std::ctype<Ch>, getloc()) ). widen(' '); #else Ch fill = ' '; #endif if(items_.size() == 0) items_.assign( nbitems, format_item_t(fill) ); else { if(nbitems>items_.size()) items_.resize(nbitems, format_item_t(fill)); bound_.resize(0); for(std::size_t i=0; i < nbitems; ++i) items_[i].reset(fill); // strings are resized, instead of reallocated } }
void print_ctype_info(charT, const char* name) { std::locale l; const std::ctype<charT>& ct = BOOST_USE_FACET(std::ctype<charT>, l); typedef typename std::ctype<charT>::mask mask_type; mask_type m = static_cast<mask_type>(std::ctype<charT>::lower | std::ctype<charT>::upper); bool result = ct.is(m, static_cast<charT>('a')) && ct.is(m , static_cast<charT>('A')); std::cout << "Checking std::ctype<" << name << ">::is(mask, c):" << std::endl; #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET std::cout << " Boost.Regex believes this facet to be buggy..." << std::endl; #else std::cout << " Boost.Regex believes this facet to be correct..." << std::endl; #endif std::cout << " Actual behavior, appears to be " << (result ? "correct." : "buggy.") << std::endl; assert(ct.is(std::ctype<charT>::alnum, 'a')); assert(ct.is(std::ctype<charT>::alnum, 'A')); assert(ct.is(std::ctype<charT>::alnum, '0')); }
inline bool lcast_ret_float(T& value, const CharT* begin, const CharT* const end) { value = static_cast<T>(0); if (begin == end) return false; if (parse_inf_nan(begin, end, value)) return true; CharT const czero = lcast_char_constants<CharT>::zero; CharT const minus = lcast_char_constants<CharT>::minus; CharT const plus = lcast_char_constants<CharT>::plus; CharT const capital_e = lcast_char_constants<CharT>::capital_e; CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e; /* Getting the plus/minus sign */ bool const has_minus = Traits::eq(*begin, minus); if (has_minus || Traits::eq(*begin, plus)) { ++ begin; if (begin == end) return false; } #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE std::locale loc; typedef std::numpunct<CharT> numpunct; numpunct const& np = BOOST_USE_FACET(numpunct, loc); std::string const grouping( (loc == std::locale::classic()) ? std::string() : np.grouping() ); std::string::size_type const grouping_size = grouping.size(); CharT const thousands_sep = static_cast<CharT>(grouping_size ? np.thousands_sep() : 0); CharT const decimal_point = np.decimal_point(); bool found_grouping = false; std::string::size_type last_grouping_pos = grouping_size - 1; #else CharT const decimal_point = lcast_char_constants<CharT>::c_decimal_separator; #endif bool found_decimal = false; bool found_number_before_exp = false; typedef int pow_of_10_t; pow_of_10_t pow_of_10 = 0; typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::type mantissa_type; mantissa_type mantissa=0; bool is_mantissa_full = false; char length_since_last_delim = 0; while (begin != end) { if (found_decimal) { /* We allow no thousand_separators after decimal point */ const mantissa_type tmp_sub_value = static_cast<mantissa_type>(*begin - czero); if (Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e)) break; if ( *begin < czero || *begin >= czero + 10 ) return false; if ( is_mantissa_full || ((std::numeric_limits<mantissa_type>::max)() - tmp_sub_value) / 10u < mantissa ) { is_mantissa_full = true; ++ begin; continue; } -- pow_of_10; mantissa = static_cast<mantissa_type>(mantissa * 10 + tmp_sub_value); found_number_before_exp = true; } else { if (*begin >= czero && *begin < czero + 10) { /* Checking for mantissa overflow. If overflow will * occur, them we only increase multiplyer */ const mantissa_type tmp_sub_value = static_cast<mantissa_type>(*begin - czero); if( is_mantissa_full || ((std::numeric_limits<mantissa_type>::max)() - tmp_sub_value) / 10u < mantissa ) { is_mantissa_full = true; ++ pow_of_10; } else { mantissa = static_cast<mantissa_type>(mantissa * 10 + tmp_sub_value); } found_number_before_exp = true; ++ length_since_last_delim; } else if (Traits::eq(*begin, decimal_point) || Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e)) { #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE /* If ( we need to check grouping * and ( grouping missmatches * or grouping position is incorrect * or we are using the grouping position 0 twice * ) * ) then return error */ if( grouping_size && found_grouping && ( length_since_last_delim != grouping[0] || last_grouping_pos>1 || (last_grouping_pos==0 && grouping_size>1) ) ) return false; #endif if (Traits::eq(*begin, decimal_point)) { ++ begin; found_decimal = true; if (!found_number_before_exp && begin==end) return false; continue; } else { if (!found_number_before_exp) return false; break; } } #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE else if (grouping_size && Traits::eq(*begin, thousands_sep)){ if(found_grouping) { /* It is not he first time, when we find thousands separator, * so we need to chek, is the distance between two groupings * equal to grouping[last_grouping_pos] */ if (length_since_last_delim != grouping[last_grouping_pos] ) { if (!last_grouping_pos) return false; else { -- last_grouping_pos; if (length_since_last_delim != grouping[last_grouping_pos]) return false; } } else /* We are calling the grouping[0] twice, when grouping size is more than 1 */ if (grouping_size>1u && last_grouping_pos+1<grouping_size) return false; } else { /* Delimiter at the begining ',000' */ if (!length_since_last_delim) return false; found_grouping = true; if (length_since_last_delim > grouping[last_grouping_pos] ) return false; } length_since_last_delim = 0; ++ begin; /* Delimiter at the end '100,' */ if (begin == end) return false; continue; } #endif else return false; } ++begin; } // Exponent found if (begin != end && (Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e))) { ++ begin; if (begin == end) return false; bool const exp_has_minus = Traits::eq(*begin, minus); if (exp_has_minus || Traits::eq(*begin, plus)) { ++ begin; if (begin == end) return false; } pow_of_10_t exp_pow_of_10 = 0; while (begin != end) { pow_of_10_t const sub_value = *begin - czero; if ( *begin < czero || *begin >= czero + 10 || ((std::numeric_limits<pow_of_10_t>::max)() - sub_value) / 10 < exp_pow_of_10) return false; exp_pow_of_10 *= 10; exp_pow_of_10 += sub_value; ++ begin; }; if (exp_has_minus) { if ((std::numeric_limits<pow_of_10_t>::min)() + exp_pow_of_10 > pow_of_10) return false; // failed overflow check pow_of_10 -= exp_pow_of_10; } else { if ((std::numeric_limits<pow_of_10_t>::max)() - exp_pow_of_10 < pow_of_10) return false; // failed overflow check pow_of_10 += exp_pow_of_10; } } /* We need a more accurate algorithm... We can not use current algorithm * with long doubles (and with doubles if sizeof(double)==sizeof(long double)). */ typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::wide_result_t wide_result_t; const wide_result_t result = std::pow(static_cast<wide_result_t>(10.0), pow_of_10) * mantissa; value = static_cast<T>( has_minus ? (boost::math::changesign)(result) : result); return !((boost::math::isinf)(value) || (boost::math::isnan)(value)); }
std::basic_string<char> ucs_to_mbcs(const std::basic_string<wchar_t>& s, const std::locale& loc) { typedef std::codecvt<wchar_t, char, mbstate_t> facet_type; return ucs_to_8_bit(s, BOOST_USE_FACET(facet_type, loc)); }
std::basic_string<wchar_t> mbcs_to_ucs(const std::basic_string<char>& s) { typedef std::codecvt<wchar_t, char, mbstate_t> facet_type; return ucs_from_8_bit(s, BOOST_USE_FACET(facet_type, std::locale())); }