dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
utf8.hpp File Reference

Conversion between UTF-8 and std::wstring. More...

#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <string_view>

Go to the source code of this file.

Functions

constexpr std::size_t dasmig::core::utf8_invalid_offset (std::string_view text) noexcept
 Whether a byte string is well-formed UTF-8.
 
constexpr bool dasmig::core::is_utf8 (std::string_view text) noexcept
 Whether a byte string is well-formed UTF-8.
 
std::string dasmig::core::to_utf8 (std::wstring_view text)
 Convert a wide string to UTF-8.
 
std::wstring dasmig::core::from_utf8 (std::string_view text)
 Convert UTF-8 to a wide string.
 

Detailed Description

Conversion between UTF-8 and std::wstring.

Author
Diego Dasso Migotto (diegomigotto at hotmail dot com)

The core works in UTF-8 (std::string). Libraries with a std::wstring API convert at the boundary. wchar_t is UTF-16 on Windows and UTF-32 elsewhere; both are handled, so the UTF-8 is the same on every platform.

Definition in file utf8.hpp.

Function Documentation

◆ from_utf8()

std::wstring dasmig::core::from_utf8 ( std::string_view  text)
inline

Convert UTF-8 to a wide string.

Parameters
textUTF-8 text.
Returns
UTF-16 (Windows) or UTF-32 text.
Exceptions
std::invalid_argumentIf text is not well-formed UTF-8.

Definition at line 131 of file utf8.hpp.

◆ is_utf8()

constexpr bool dasmig::core::is_utf8 ( std::string_view  text)
constexprnoexcept

Whether a byte string is well-formed UTF-8.

Parameters
textBytes to check.
Returns
True when valid.

Definition at line 67 of file utf8.hpp.

◆ to_utf8()

std::string dasmig::core::to_utf8 ( std::wstring_view  text)
inline

Convert a wide string to UTF-8.

Parameters
textUTF-16 (Windows) or UTF-32 text.
Returns
The UTF-8 encoding.
Exceptions
std::invalid_argumentOn an unpaired surrogate or a value above U+10FFFF.

Definition at line 77 of file utf8.hpp.

◆ utf8_invalid_offset()

constexpr std::size_t dasmig::core::utf8_invalid_offset ( std::string_view  text)
constexprnoexcept

Whether a byte string is well-formed UTF-8.

Rejects overlong forms, surrogates (U+D800 to U+DFFF) and code points above U+10FFFF.

Parameters
textBytes to check.
Returns
The offset of the first invalid byte, or std::string_view::npos when the text is valid.

Definition at line 32 of file utf8.hpp.