📄 locale_facets.h
字号:
* This virtual function converts each wchar_t in the range [lo,hi) to
* char using the simplest reasonable transformation and writes the
* results to the destination array. For any wchar_t in the input that
* cannot be converted, @a dfault is used instead. For an underived
* ctype<wchar_t> facet, the argument will be copied, casting each
* element to char.
*
* do_narrow() is a hook for a derived facet to change the behavior of
* narrowing. do_narrow() must always return the same result for the
* same input.
*
* Note: this is not what you want for codepage conversions. See
* codecvt for that.
*
* @param lo Pointer to start of range.
* @param hi Pointer to end of range.
* @param dfault Char to use if conversion fails.
* @param to Pointer to the destination array.
* @return @a hi.
*/
virtual const char_type*
do_narrow(const char_type* __lo, const char_type* __hi,
char __dfault, char* __dest) const;
// For use at construction time only.
void
_M_initialize_ctype();
};
template<>
const ctype<wchar_t>&
use_facet<ctype<wchar_t> >(const locale& __loc);
#endif //_GLIBCXX_USE_WCHAR_T
// Include host and configuration specific ctype inlines.
#include <bits/ctype_inline.h>
// 22.2.1.2 Template class ctype_byname
template<typename _CharT>
class ctype_byname : public ctype<_CharT>
{
public:
typedef _CharT char_type;
explicit
ctype_byname(const char* __s, size_t __refs = 0);
protected:
virtual
~ctype_byname() { };
};
// 22.2.1.4 Class ctype_byname specializations.
template<>
ctype_byname<char>::ctype_byname(const char*, size_t refs);
template<>
ctype_byname<wchar_t>::ctype_byname(const char*, size_t refs);
// 22.2.1.5 Template class codecvt
#include <bits/codecvt.h>
// 22.2.2 The numeric category.
class __num_base
{
public:
// NB: Code depends on the order of _S_atoms_out elements.
// Below are the indices into _S_atoms_out.
enum
{
_S_ominus,
_S_oplus,
_S_ox,
_S_oX,
_S_odigits,
_S_odigits_end = _S_odigits + 16,
_S_oudigits = _S_odigits_end,
_S_oudigits_end = _S_oudigits + 16,
_S_oe = _S_odigits + 14, // For scientific notation, 'e'
_S_oE = _S_oudigits + 14, // For scientific notation, 'E'
_S_oend = _S_oudigits_end
};
// A list of valid numeric literals for output. This array
// contains chars that will be passed through the current locale's
// ctype<_CharT>.widen() and then used to render numbers.
// For the standard "C" locale, this is
// "-+xX0123456789abcdef0123456789ABCDEF".
static const char* _S_atoms_out;
// String literal of acceptable (narrow) input, for num_get.
// "-+xX0123456789abcdefABCDEF"
static const char* _S_atoms_in;
enum
{
_S_iminus,
_S_iplus,
_S_ix,
_S_iX,
_S_izero,
_S_ie = _S_izero + 14,
_S_iE = _S_izero + 20,
_S_iend = 26
};
// num_put
// Construct and return valid scanf format for floating point types.
static void
_S_format_float(const ios_base& __io, char* __fptr, char __mod);
};
template<typename _CharT>
struct __numpunct_cache : public locale::facet
{
const char* _M_grouping;
size_t _M_grouping_size;
bool _M_use_grouping;
const _CharT* _M_truename;
size_t _M_truename_size;
const _CharT* _M_falsename;
size_t _M_falsename_size;
_CharT _M_decimal_point;
_CharT _M_thousands_sep;
// A list of valid numeric literals for output: in the standard
// "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
// This array contains the chars after having been passed
// through the current locale's ctype<_CharT>.widen().
_CharT _M_atoms_out[__num_base::_S_oend];
// A list of valid numeric literals for input: in the standard
// "C" locale, this is "-+xX0123456789abcdefABCDEF"
// This array contains the chars after having been passed
// through the current locale's ctype<_CharT>.widen().
_CharT _M_atoms_in[__num_base::_S_iend];
bool _M_allocated;
__numpunct_cache(size_t __refs = 0) : facet(__refs),
_M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
_M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
_M_falsename_size(0), _M_decimal_point(_CharT()),
_M_thousands_sep(_CharT()), _M_allocated(false)
{ }
~__numpunct_cache();
void
_M_cache(const locale& __loc);
private:
__numpunct_cache&
operator=(const __numpunct_cache&);
explicit
__numpunct_cache(const __numpunct_cache&);
};
template<typename _CharT>
__numpunct_cache<_CharT>::~__numpunct_cache()
{
if (_M_allocated)
{
delete [] _M_grouping;
delete [] _M_truename;
delete [] _M_falsename;
}
}
/**
* @brief Numpunct facet.
*
* This facet stores several pieces of information related to printing and
* scanning numbers, such as the decimal point character. It takes a
* template parameter specifying the char type. The numpunct facet is
* used by streams for many I/O operations involving numbers.
*
* The numpunct template uses protected virtual functions to provide the
* actual results. The public accessors forward the call to the virtual
* functions. These virtual functions are hooks for developers to
* implement the behavior they require from a numpunct facet.
*/
template<typename _CharT>
class numpunct : public locale::facet
{
public:
// Types:
//@{
/// Public typedefs
typedef _CharT char_type;
typedef basic_string<_CharT> string_type;
//@}
typedef __numpunct_cache<_CharT> __cache_type;
protected:
__cache_type* _M_data;
public:
/// Numpunct facet id.
static locale::id id;
/**
* @brief Numpunct constructor.
*
* @param refs Refcount to pass to the base class.
*/
explicit
numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
{ _M_initialize_numpunct(); }
/**
* @brief Internal constructor. Not for general use.
*
* This is a constructor for use by the library itself to set up the
* predefined locale facets.
*
* @param cache __numpunct_cache object.
* @param refs Refcount to pass to the base class.
*/
explicit
numpunct(__cache_type* __cache, size_t __refs = 0)
: facet(__refs), _M_data(__cache)
{ _M_initialize_numpunct(); }
/**
* @brief Internal constructor. Not for general use.
*
* This is a constructor for use by the library itself to set up new
* locales.
*
* @param cloc The "C" locale.
* @param refs Refcount to pass to the base class.
*/
explicit
numpunct(__c_locale __cloc, size_t __refs = 0)
: facet(__refs), _M_data(NULL)
{ _M_initialize_numpunct(__cloc); }
/**
* @brief Return decimal point character.
*
* This function returns a char_type to use as a decimal point. It
* does so by returning returning
* numpunct<char_type>::do_decimal_point().
*
* @return @a char_type representing a decimal point.
*/
char_type
decimal_point() const
{ return this->do_decimal_point(); }
/**
* @brief Return thousands separator character.
*
* This function returns a char_type to use as a thousands
* separator. It does so by returning returning
* numpunct<char_type>::do_thousands_sep().
*
* @return char_type representing a thousands separator.
*/
char_type
thousands_sep() const
{ return this->do_thousands_sep(); }
/**
* @brief Return grouping specification.
*
* This function returns a string representing groupings for the
* integer part of a number. Groupings indicate where thousands
* separators should be inserted in the integer part of a number.
*
* Each char in the return string is interpret as an integer
* rather than a character. These numbers represent the number
* of digits in a group. The first char in the string
* represents the number of digits in the least significant
* group. If a char is negative, it indicates an unlimited
* number of digits for the group. If more chars from the
* string are required to group a number, the last char is used
* repeatedly.
*
* For example, if the grouping() returns "\003\002" and is
* applied to the number 123456789, this corresponds to
* 12,34,56,789. Note that if the string was "32", this would
* put more than 50 digits into the least significant group if
* the character set is ASCII.
*
* The string is returned by calling
* numpunct<char_type>::do_grouping().
*
* @return string representing grouping specification.
*/
string
grouping() const
{ return this->do_grouping(); }
/**
* @brief Return string representation of bool true.
*
* This function returns a string_type containing the text
* representation for true bool variables. It does so by calling
* numpunct<char_type>::do_truename().
*
* @return string_type representing printed form of true.
*/
string_type
truename() const
{ return this->do_truename(); }
/**
* @brief Return string representation of bool false.
*
* This function returns a string_type containing the text
* representation for false bool variables. It does so by calling
* numpunct<char_type>::do_falsename().
*
* @return string_type representing printed form of false.
*/
string_type
falsename() const
{ return this->do_falsename(); }
protected:
/// Destructor.
virtual
~numpunct();
/**
* @brief Return decimal point character.
*
* Returns a char_type to use as a decimal point. This function is a
* hook for derived classes to change the value returned.
*
* @return @a char_type representing a decimal point.
*/
virtual char_type
do_decimal_point() const
{ return _M_data->_M_decimal_point; }
/**
* @brief Return thousands separator character.
*
* Returns a char_type to use as a thousands separator. This function
* is a hook for derived classes to change the value returned.
*
* @return @a char_type representing a thousands separator.
*/
virtual char_type
do_thousands_sep() const
{ return _M_data->_M_thousands_sep; }
/**
* @brief Return grouping specification.
*
* Returns a string representing groupings for the integer part of a
* number. This function is a hook for derived classes to change the
* value returned. @see grouping() for details.
*
* @return String representing grouping specification.
*/
virtual string
do_grouping() const
{ return _M_data->_M_grouping; }
/**
* @brief Return string representation of bool true.
*
* Returns a string_type containing the text representation for true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -