50 return ((
static_cast<char32_t>(high) - 0xD800) << 10
51 |
static_cast<char32_t>(low) - 0xDC00) + 0x10000;
61 inline std::string
to_hex_string(
const char32_t cChar,
const std::size_t nLength) {
62 std::ostringstream oStream;
63 if (nLength == 1) oStream.write(
"\\x", 2);
64 if (nLength == 2) oStream.write(
"\\u", 2);
65 if (nLength == 4) oStream.write(
"\\U", 2);
66 oStream << std::hex << std::setfill(
'0')
67 << std::setw(
static_cast<std::ostream::int_type
>(nLength))
68 <<
static_cast<std::uint32_t
>(cChar);
84 oStream.
put(
static_cast<char>(cChar));
85 }
else if (cChar <= 0x7FF) {
87 oStream.
put(
static_cast<char>(cChar >> 6 | 0xC0));
88 oStream.
put(
static_cast<char>(cChar & 0x3F | 0x80));
89 }
else if (cChar <= 0xFFFF) {
91 oStream.
put(
static_cast<char>(cChar >> 12 | 0xE0));
92 oStream.
put(
static_cast<char>(cChar >> 6 & 0x3F | 0x80));
93 oStream.
put(
static_cast<char>(cChar & 0x3F | 0x80));
94 }
else if (cChar <= 0x10FFFF) {
96 oStream.
put(
static_cast<char>(cChar >> 18 | 0xF0));
97 oStream.
put(
static_cast<char>(cChar >> 12 & 0x3F | 0x80));
98 oStream.
put(
static_cast<char>(cChar >> 6 & 0x3F | 0x80));
99 oStream.
put(
static_cast<char>(cChar & 0x3F | 0x80));
112 return oStream.
str();
130 std::string sValue(nCount * 2,
'\0');
131 oIstream.
read(sValue.data(), nCount * 2);
134 static std::regex oRegex(
"^[0-9A-Fa-f]+$");
135 if (!std::regex_match(sValue, oRegex))
137 auto cChar =
static_cast<char32_t>(std::stoull(sValue,
nullptr, 16));
147 if (cChar >= 0xD800 && cChar <= 0xDBFF) {
148 if (oIstream.
get() !=
'\\')
return false;
149 if (oIstream.
get() !=
'u')
return false;
152 oIstream.
read(sValue.data(), nCount * 2);
153 if (!std::regex_match(sValue, oRegex))
156 const auto cLow =
static_cast<char32_t>(std::stoull(sValue,
nullptr, 16));
157 if (cLow < 0xDC00 || cLow > 0xDFFF)
161 static_cast<char16_t>(cChar),
162 static_cast<char16_t>(cLow)
164 }
else if (cChar >= 0xDC00) {
172 if ((cChar >= 0xD800 && cChar <= 0xDFFF) || cChar > 0x10FFFF)
A class for input stream behavior with a character type.
Definition fakestream.h:70
ALWAYS_INLINE int_type get() noexcept
Retrieves the next character character from the stream and moves to the next.
Definition fakestream.h:119
ALWAYS_INLINE std::size_t read(char_type *const pBuffer, const std::size_t nCount) noexcept
Reads a specified number of characters into a buffer.
Definition fakestream.h:154
A class for output stream behavior with a character type.
Definition fakestream.h:263
ALWAYS_INLINE std::basic_string< char_type > str() const
Converts the current data to a string.
Definition fakestream.h:382
ALWAYS_INLINE void put(const char_type cChar)
Writes a single character to the stream.
Definition fakestream.h:309
Generic input and output stream classes with character types.
BashSpark main namespace.
Definition command.h:39
bool parse_utf(ifakestream &oIstream, const std::size_t nCount, char32_t &cResult)
Parses a UTF-n encoded character from an input stream.
Definition utf.h:128
std::string to_hex_string(const char32_t cChar, const std::size_t nLength)
Converts a UTF-32 character to a hexadecimal string representation.
Definition utf.h:61
constexpr char32_t combine_surrogates(const char16_t high, const char16_t low)
Combines high and low UTF-16 surrogates into a UTF-32 code point.
Definition utf.h:49
void write_char32_t(ofakestream &oStream, const char32_t cChar)
Writes a UTF-32 character to an output stream in UTF-8 encoding.
Definition utf.h:81