Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Defined in header <text_encoding> | ||
struct text_encoding; | (since C++26) | |
The classtext_encoding
provides a mechanism for identifying character encodings. It is used to determine theordinary character literal encoding of the translation environment at compile-time and the character encoding of the execution environment at runtime.
Eachtext_encoding
object encapsulates acharacter encoding scheme, uniquely identified by an enumerator intext_encoding::id
and a corresponding name represented by a null-terminated byte string. These can be accessed through themib() andname() member functions, respectively. The determination of whether an object represents a character encoding scheme implemented in the translation or execution environment is implementation-defined.
The classtext_encoding
is aTriviallyCopyable type. The array object representing the corresponding name of the character encoding scheme isnested within thetext_encoding
object itself. The stored name is limited to a maximum ofmax_name_length characters excluding the null character'\0'.
The class supports both registered and non-registered character encodings. Registered encodings are those found in theIANA Character Sets Registry excluding the following character encodings:
In addition, the class provides access for registered character encodings to:
Non-registered encodings can be represented with an enumeratorid::other orid::unknown and a custom name.
Atext_encoding
objecte whose MIBenum value is neitherid::other norid::unknown maintains the following invariants:
Contents |
represents the MIBenum value of the character encoding (public member enum)[edit] | |
aview over aliases of the character encoding(public member class)[edit] |
Name | Value |
constexprstd::size_t max_name_length [static] | 63 (public static member constant) |
Member | Description |
std::text_encoding::idmib_ (private) | a MIBenum value withid::unknown as the default value (exposition-only member object*) |
char[max_name_length+1]name_ (private) | a stored primary name (exposition-only member object*) |
Creation | |
constructs newtext_encoding object(public member function)[edit] | |
[static] | constructs a newtext_encoding representing theordinary character literal encoding(public static member function)[edit] |
[static] | constructs a newtext_encoding representing the implementation-defined character encoding scheme of the execution environment(public static member function)[edit] |
Observers | |
returns the MIBenum value of the current character encoding (public member function)[edit] | |
returns the primary name of the current character encoding (public member function)[edit] | |
returns aview over aliases of the current character encoding(public member function)[edit] | |
[static] | checks the character encoding scheme of the execution environment with the specified MIB value (public static member function)[edit] |
Helpers | |
[static](private) | compares two alias names usingCharset Alias Matching (exposition-only static member function*)[edit] |
compares twotext_encoding objects.(public member function)[edit] |
hash support forstd::text_encoding (class template specialization)[edit] |
When working with character encodings, it is important to note that the primary names or aliases of two distinct registered character encodings are not equivalent when compared usingCharset Alias Matching as described by the Unicode Technical Standard.
For convenience, the enumerators oftext_encoding::id
are introduced as members oftext_encoding
and can be accessed directly. This means thattext_encoding::ASCII andtext_encoding::id::ASCII refer to the same entity.
It is recommended that the implementation should treat registered encodings as not interchangeable. Additionally, the primary name of a registered encoding should not be used to describe a similar but different non-registered encoding, unless there is a clear precedent for doing so.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_text_encoding | 202306L | (C++26) | std::text_encoding |
#include <locale>#include <print>#include <text_encoding> int main(){// literal encoding is known at compile-timeconstexprstd::text_encoding literal_encoding= std::text_encoding::literal(); // check for literal encoding static_assert(literal_encoding.mib()!= std::text_encoding::other&& literal_encoding.mib()!= std::text_encoding::unknown); // environment encoding is only known at runtimestd::text_encoding env_encoding= std::text_encoding::environment(); // associated encoding of the default localestd::text_encoding locale_encoding=std::locale("").encoding(); std::println("The literal encoding is {}", literal_encoding.name());std::println("The aliases of literal encoding:");for(constchar* alias_name: literal_encoding.aliases())std::println(" -> {}", alias_name); if(env_encoding== locale_encoding)std::println("Both environment and locale encodings are the same"); std::println("The environment encoding is {}", env_encoding.name());std::println("The aliases of environment encoding:");for(constchar* alias_name: env_encoding.aliases())std::println(" -> {}", alias_name);}
Possible output:
The literal encoding is UTF-8The aliases of literal encoding: -> UTF-8 -> csUTF8Both environment and locale encodings are the sameThe environment encoding is ANSI_X3.4-1968The aliases of environment encoding: -> US-ASCII -> iso-ir-6 -> ANSI_X3.4-1968 -> ANSI_X3.4-1986 -> ISO_646.irv:1991 -> ISO646-US -> us -> IBM367 -> cp367 -> csASCII -> ASCII
set of polymorphic facets that encapsulate cultural differences (class)[edit] |