Unicode encoding utilities. More...
Macros | |
| #define | SOURCEMETA_CORE_JOINING_TYPE_LIST(X) |
| #define | SOURCEMETA_CORE_BIDI_CLASS_LIST(X) |
| #define | SOURCEMETA_CORE_UNICODE_SCRIPT_LIST(X) |
| #define | SOURCEMETA_CORE_NFC_QUICK_CHECK_LIST(X) |
Enumerations | |
| enum class | sourcemeta::core::JoiningType : std::uint8_t |
| enum class | sourcemeta::core::BidiClass : std::uint8_t |
| enum class | sourcemeta::core::UnicodeScript : std::uint8_t |
| enum class | sourcemeta::core::NFCQuickCheck : std::uint8_t |
Functions | |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::codepoint_to_utf8 (const char32_t codepoint) -> std::string |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::codepoint_to_utf8 (const char32_t codepoint, std::ostream &output) -> void |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::codepoint_to_utf8 (const char32_t codepoint, std::string &output) -> void |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::utf8_to_utf32 (std::istream &input) -> std::optional< std::u32string > |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::utf8_to_utf32 (const std::string_view input) -> std::optional< std::u32string > |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::utf32_to_utf8 (const std::u32string_view input) -> std::string |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::utf32_to_utf8_lenient (const std::u32string_view input) -> std::string |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::to_valid_utf8 (const std::string_view input) -> std::string |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::utf8_to_wide (const std::string_view input) -> std::wstring |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::wide_to_utf8 (const std::wstring_view input) -> std::string |
| constexpr auto | sourcemeta::core::utf8_lead_byte_size (const unsigned char byte) -> std::uint8_t |
| constexpr auto | sourcemeta::core::is_utf8_tail (const unsigned char lead, const std::size_t position, const unsigned char byte) -> bool |
| constexpr auto | sourcemeta::core::utf8_sequence_size (const std::string_view input) -> std::size_t |
| constexpr auto | sourcemeta::core::is_utf8_continuation (const unsigned char byte) -> bool |
| constexpr auto | sourcemeta::core::utf8_codepoint_count (const std::string_view input) -> std::size_t |
| constexpr auto | sourcemeta::core::utf8_codepoint_within (const std::string_view input, const std::size_t minimum, const std::size_t maximum) -> bool |
| constexpr auto | sourcemeta::core::is_surrogate (const char32_t codepoint) -> bool |
| constexpr auto | sourcemeta::core::is_valid_codepoint (const char32_t codepoint) -> bool |
| constexpr auto | sourcemeta::core::is_ucschar (const char32_t codepoint) -> bool |
| constexpr auto | sourcemeta::core::is_iprivate (const char32_t codepoint) -> bool |
| constexpr auto | sourcemeta::core::utf8_codepoint_byte_count (const char32_t codepoint) -> std::uint8_t |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::combining_class (const char32_t codepoint) noexcept -> std::uint8_t |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::joining_type (const char32_t codepoint) noexcept -> JoiningType |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::bidi_class (const char32_t codepoint) noexcept -> BidiClass |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::script (const char32_t codepoint) noexcept -> UnicodeScript |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::is_combining_mark (const char32_t codepoint) noexcept -> bool |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::nfc_quick_check (const char32_t codepoint) noexcept -> NFCQuickCheck |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::canonical_decomposition (const char32_t codepoint) noexcept -> std::u32string_view |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::canonical_composition (const char32_t starter, const char32_t combining) noexcept -> std::optional< char32_t > |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::nfc (const std::u32string_view input) -> std::u32string |
| SOURCEMETA_CORE_UNICODE_EXPORT auto | sourcemeta::core::is_nfc (const std::u32string_view input) -> bool |
| constexpr auto | sourcemeta::core::utf8_codepoint_length (const std::string_view input, const std::string_view::size_type position) -> std::size_t |
| constexpr auto | sourcemeta::core::utf8_decode (const std::string_view input, const std::string_view::size_type position) -> std::optional< std::pair< char32_t, std::size_t > > |
Unicode encoding utilities.
This functionality is included as follows:
| #define SOURCEMETA_CORE_BIDI_CLASS_LIST | ( | X | ) |
Each entry maps a BidiClass enum name to its UCD short alias.
| #define SOURCEMETA_CORE_JOINING_TYPE_LIST | ( | X | ) |
Each entry maps a JoiningType enum name to its UCD short alias.
| #define SOURCEMETA_CORE_NFC_QUICK_CHECK_LIST | ( | X | ) |
Each entry maps an NFCQuickCheck enum name to its UCD short alias.
| #define SOURCEMETA_CORE_UNICODE_SCRIPT_LIST | ( | X | ) |
Each entry maps a UnicodeScript enum name to its UCD long alias. Per UAX #24 §1.4, Katakana_Or_Hiragana only appears in the Script_Extensions property and never in the Script property itself.
|
strong |
The bidirectional class of a Unicode codepoint per UAX #44. See https://www.unicode.org/reports/tr44/ for the property's definition.
|
strong |
The joining type of a Unicode codepoint per UAX #44. See https://www.unicode.org/reports/tr44/ for the property's definition.
|
strong |
The NFC quick-check result for a Unicode codepoint per UAX #15. See https://www.unicode.org/reports/tr15/ for the property's definition.
|
strong |
The script of a Unicode codepoint per UAX #24. See https://www.unicode.org/reports/tr24/ for the property's definition.
|
noexcept |
Return the bidirectional class of a Unicode codepoint. See https://www.unicode.org/reports/tr44/ for the property's definition. For example:
|
noexcept |
Return the primary composite of a starter codepoint and a following codepoint per UAX #15, or std::nullopt if the pair does not form a primary composite. Hangul jamo composition is algorithmic per UAX #15 §3.12 and is not included in this table. For example:
|
noexcept |
Return the non-recursive canonical decomposition of a Unicode codepoint per UAX #15. The view points into static data and remains valid for the program's lifetime. An empty view means the codepoint has no canonical decomposition. Hangul precomposed syllables decompose algorithmically per UAX #15 and are reported as empty here. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::codepoint_to_utf8 | ( | const char32_t | codepoint | ) | -> std::string |
Encode a single Unicode codepoint as a UTF-8 string. The codepoint must be a valid Unicode scalar value, otherwise the output is unspecified. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::codepoint_to_utf8 | ( | const char32_t | codepoint, |
| std::ostream & | output ) -> void |
Encode a single Unicode codepoint as UTF-8 into an output stream. The codepoint must be a valid Unicode scalar value, otherwise the output is unspecified. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::codepoint_to_utf8 | ( | const char32_t | codepoint, |
| std::string & | output ) -> void |
Encode a single Unicode codepoint as UTF-8, appending to an existing string. The codepoint must be a valid Unicode scalar value, otherwise the output is unspecified. For example:
|
noexcept |
Return the canonical combining class of a Unicode codepoint. See https://www.unicode.org/reports/tr44/ for the property's definition. For example:
|
noexcept |
Return whether a Unicode codepoint is a combining mark, in the sense of UAX #44 general category Mn (Nonspacing_Mark), Mc (Spacing_Mark), or Me (Enclosing_Mark). See https://www.unicode.org/reports/tr44/ for the property's definition. For example:
|
inlineconstexpr |
Check whether the given codepoint matches the iprivate production of RFC 3987 Section 2.2, the set of private-use characters that an IRI may carry in the query component. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::is_nfc | ( | const std::u32string_view | input | ) | -> bool |
Return whether input is already in Unicode Normalisation Form C per UAX #15. Uses a quick-check fast path and falls back to a full normalise-and-compare when the quick check is inconclusive. For example:
|
inlineconstexpr |
Check whether the given codepoint is in the UTF-16 surrogate range (U+D800 to U+DFFF), which is forbidden in scalar Unicode text. For example:
|
inlineconstexpr |
Check whether the given codepoint matches the ucschar production of RFC 3987 Section 2.2, the set of non-ASCII characters that an IRI may carry in components other than the scheme, host, and percent-encoded octets. For example:
|
inlineconstexpr |
Check whether the given byte is a UTF-8 continuation byte (x80-BF per RFC 3629 Section 4). For example:
|
inlineconstexpr |
Check whether the given byte may follow the given lead byte at the given position of a UTF-8 sequence, where position 1 is the byte right after the lead. RFC 3629 Section 4 narrows that first position below the general x80-BF for four of the leads, so as to exclude the overlong encodings and the surrogate range that the shorter grammar would otherwise admit:
UTF8-3 = xE0 xA0-BF UTF8-tail / xE1-EC 2( UTF8-tail ) / xED x80-9F UTF8-tail / xEE-EF 2( UTF8-tail ) UTF8-4 = xF0 x90-BF 2( UTF8-tail ) / xF1-F3 3( UTF8-tail ) / xF4 x80-8F 2( UTF8-tail )
For example:
|
inlineconstexpr |
Check whether the given value is a valid Unicode codepoint: in the range U+0000 to U+10FFFF, excluding the UTF-16 surrogate range (U+D800 to U+DFFF). For example:
|
noexcept |
Return the joining type of a Unicode codepoint. See https://www.unicode.org/reports/tr44/ for the property's definition. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::nfc | ( | const std::u32string_view | input | ) | -> std::u32string |
Return the Unicode Normalisation Form C of input per UAX #15. For example:
|
noexcept |
Return the NFC quick-check property of a Unicode codepoint per UAX #15. See https://www.unicode.org/reports/tr15/ for the property's definition. For example:
|
noexcept |
Return the script of a Unicode codepoint. See https://www.unicode.org/reports/tr24/ for the property's definition. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::to_valid_utf8 | ( | const std::string_view | input | ) | -> std::string |
Read a byte sequence as UTF-8, standing in the replacement character U+FFFD for every sequence that is not well-formed. One replacement stands in for a whole maximal subpart, so a sequence cut short yields a single one rather than one per stray byte. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::utf32_to_utf8 | ( | const std::u32string_view | input | ) | -> std::string |
Encode a sequence of Unicode codepoints (UTF-32) as a UTF-8 string, the inverse of utf8_to_utf32. Every codepoint must be a valid Unicode scalar value (in particular, not a surrogate), otherwise the output is unspecified. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::utf32_to_utf8_lenient | ( | const std::u32string_view | input | ) | -> std::string |
Encode a sequence of codepoints as UTF-8, the lenient counterpart of utf32_to_utf8. Unlike that function, surrogate codepoints are permitted and encoded as their three-byte WTF-8 sequence rather than being rejected. Each codepoint must still be within the Unicode codespace (U+0000 to U+10FFFF), otherwise the output is unspecified. Because a surrogate is not a valid scalar value, its encoding is ill-formed UTF-8: it does not round-trip through utf8_to_utf32 (which rejects surrogates) and is meant for byte-preserving workflows, such as feeding a strict decoder or validator with input that carries lone surrogates. For example:
|
inlineconstexpr |
Determine the number of UTF-8 bytes that a codepoint encodes to per RFC 3629: 1 byte for U+0000-U+007F, 2 bytes for U+0080-U+07FF, 3 bytes for U+0800-U+FFFF, and 4 bytes for U+10000 and above. The caller is responsible for ensuring the codepoint is in range. For example:
|
inlineconstexpr |
Count the number of Unicode code points in a UTF-8 string, assuming the input is well-formed. Each code point begins at a byte that is not a continuation byte (x80-BF per RFC 3629 Section 4). For example:
|
inlineconstexpr |
Determine the byte length of the valid UTF-8 codepoint starting at the given position within the input. Returns 1 for an ASCII byte, 2/3/4 for a valid multi-byte UTF-8 sequence (RFC 3629 Section 4, excluding overlong encodings, surrogates, and code points above U+10FFFF), or 0 if the bytes at that position do not start a valid UTF-8 codepoint. For example:
|
inlineconstexpr |
Check whether the number of Unicode code points in a well-formed UTF-8 string is within an inclusive range, without necessarily scanning the whole string. Because a code point occupies between one and four bytes, the byte length bounds the code point count, so the extreme cases are decided in constant time and the fallback scan stops as soon as the range is exceeded. For example:
|
inlineconstexpr |
Decode the single UTF-8 codepoint that begins at the given position within the input, returning the codepoint together with the number of bytes it occupies, or an empty result when the bytes at that position do not start a valid UTF-8 codepoint (RFC 3629 Section 4, excluding overlong encodings, surrogates, and code points above U+10FFFF). For example:
|
inlineconstexpr |
Determine the byte length encoded by a UTF-8 lead byte. Returns 1 for an ASCII byte (x00-7F), 2 for a 2-byte lead (xC2-DF), 3 for a 3-byte lead (xE0-EF), 4 for a 4-byte lead (xF0-F4), or 0 for any other byte (continuation byte, overlong xC0/xC1, or out-of-range xF5-FF). For example:
|
inlineconstexpr |
Determine the byte length of the well-formed UTF-8 sequence that the given input begins with, or 0 when it does not begin with one. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::utf8_to_utf32 | ( | const std::string_view | input | ) | -> std::optional< std::u32string > |
Decode a UTF-8 string into a sequence of Unicode codepoints (UTF-32). Returns std::nullopt if the input contains invalid UTF-8. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::utf8_to_utf32 | ( | std::istream & | input | ) | -> std::optional< std::u32string > |
Decode a UTF-8 byte stream into a sequence of Unicode codepoints (UTF-32). Returns std::nullopt if the input contains invalid UTF-8. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::utf8_to_wide | ( | const std::string_view | input | ) | -> std::wstring |
Convert a UTF-8 string into its wide character form without validation. The input must be valid UTF-8, otherwise the result is undefined. For example:
| SOURCEMETA_CORE_UNICODE_EXPORT auto sourcemeta::core::wide_to_utf8 | ( | const std::wstring_view | input | ) | -> std::string |
Convert a wide string into its UTF-8 form without validation. The input must be valid, otherwise the result is undefined. For example: