-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fdd578
commit 6f40fff
Showing
6 changed files
with
114 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
#ifndef ESC_DETAIL_U32_TO_MB_HPP | ||
#define ESC_DETAIL_U32_TO_MB_HPP | ||
#include <array> | ||
#include <cstddef> | ||
#include <utility> | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace esc::detail { | ||
|
||
/// Convert the given char32_t into a multi-byte array of chars. | ||
/** Depends on the currently set clocale to transform the char32_t. */ | ||
[[nodiscard]] auto u32_to_mb(char32_t c) | ||
-> std::pair<std::size_t, std::array<char, 4>>; | ||
/// Convert the given char32_t into a multi-byte reprensentation as a string. | ||
/// @param c The char32_t to convert. | ||
/// @return The multi-byte representation of the given char32_t. | ||
/// @throws std::runtime_error if the conversion fails. | ||
[[nodiscard]] auto u32_to_mb(char32_t c) -> std::string; | ||
|
||
} // namespace esc::detail | ||
#endif // ESC_DETAIL_U32_TO_MB_HPP | ||
} // namespace esc::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
#include <esc/detail/u32_to_mb.hpp> | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <stdexcept> | ||
#include <utility> | ||
#include <string> | ||
|
||
#ifdef __APPLE__ | ||
# include <cwchar> | ||
#else | ||
# include <cuchar> | ||
#endif | ||
#include <unicode/ucnv.h> | ||
#include <unicode/unistr.h> | ||
#include <unicode/utypes.h> | ||
|
||
namespace esc::detail { | ||
|
||
auto u32_to_mb(char32_t c) -> std::pair<std::size_t, std::array<char, 4>> | ||
auto u32_to_mb(char32_t c) -> std::string | ||
{ | ||
auto result = std::array<char, 4>{}; | ||
auto state = std::mbstate_t{}; | ||
auto status = U_ZERO_ERROR; | ||
auto u_str = icu::UnicodeString(static_cast<UChar32>(c)); | ||
auto result = std::string{}; | ||
u_str.toUTF8String(result); | ||
|
||
#ifdef __APPLE__ | ||
static_assert(sizeof(wchar_t) == sizeof(char32_t)); | ||
static_assert(alignof(wchar_t) == alignof(char32_t)); | ||
auto const count = | ||
std::wcrtomb(result.data(), static_cast<wchar_t>(c), &state); | ||
#else | ||
auto const count = std::c32rtomb(result.data(), c, &state); | ||
#endif | ||
if (count == std::size_t(-1)) | ||
throw std::runtime_error{"u32_to_mb(char32_t): Error in Conversion."}; | ||
return {count, result}; | ||
if (U_FAILURE(status)) { | ||
throw std::runtime_error{"Unicode conversion failed"}; | ||
} | ||
return result; | ||
} | ||
|
||
} // namespace esc::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters