cpp/string/char traits
Character traits
- include <string>
template< class charT > class char_traits;
The char_traits class defines the stream and string operation properties of a character type. Specifically, they define the types used for manipulating the characters and character strings, as well as all the common operations for the given character type.
Each set of character traits is defined as a template specialization Traits< charT > where charT is the type of the character.
The template shall to specify the following members:
^ Expression ^ Explanation ^ Complexity ^ | char_type | the type of the character, charT | compile time | | int_type | type, able to hold all values of charT and EOF value | compile time | | off_type | //implementation defined// | compile time | | pos_type | //implementation defined// | compile time | | state_type | //implementation defined// | compile time | | void assign( charT &r, charT a ) | assigns r = a | constant | | bool eq( charT a, charT b ) | shall return true if a and b are equivalent | constant | | bool lt( charT a, charT b ) | shall return true if a is less than b | constant | | char_type *assign( charT *p, size_t n, charT a ) | shall assign value a to character string [p, p+n). Shall return p | linear | | char_type *move( charT *p, const charT *q, size_t n) | shall assign character string [q, q+n) to character string [p, p+n).\\ Shall return p. Performs correctly even if '[p, p+n) and '[q, q+n) overlap. | linear | | char_type *copy( charT *p, const charT *q, size_t n) | shall assign character string [q, q+n) to character string [p, p+n).\\ Shall return p. q shall not be in [p, p+n) | linear | | int compare( const charT *p, const charT *q, size_t n ) | shall return 0 if character strings [p, p+n) and [q, q+n) are equal,\\ shall return negative value if [p, p+n) is less than [q, q+n) and\\ shall return positive value if [p, p+n) is greater than [q, q+n).\\ The comparison is done lexicographically, using eq and lt functions. | linear | | size_t length() | shall return the position of the terminating Char() (i.e. null-character) | linear | | const char_type *find( const charT *p, size_t n, charT a ) | shall return the pointer to the first character of value a in character string p | linear | | char_type to_char_type( int_type a ) | shall return equivalent value of char_type type.\\ If there is no such value, return value is unspecified | constant | | int_type to_int_type( char_type a ) | shall return equivalent value of int_type type. | constant | | bool eq_int_type( int_type a, int_type b ) | shall return false if one of a or b equals eof() and the other is not. \\ Shall return true if a equals b. Return value is unspecified in other cases. | constant | | int_type eof() | shall return a value not equal to any value of type char_type | constant | | bool not_eof( int_type a ) | shall return true if a and eof() are equivalent| constant |
There is class template char_traits defined, which serves as a basis for explicit instantiations. It fulfills all requirements of Traits concept.
Also, several specializations are defined for most common character types which which has to specify the following members:
^ Instantiation ^ char_type ^ int_type ^ off_type ^ pos_type ^ state_type ^ | char_traits< char > | char | int | streamoff | streampos | mbstate_t | | char_traits< wchar_t > | wchar_t | wint_t | wstreamoff | wstreampos | mbstate_t | | char_traits< char16_t > (/C++0x feature) | char16_t | int_least16_t | streamoff | u16streampos | mbstate_t | | char_traits< char32_t > (/C++0x feature) | char32_t | int_least32_t | streamoff | u32streampos | mbstate_t |