aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/StringUtil.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/common/StringUtil.hpp')
-rw-r--r--include/cru/common/StringUtil.hpp36
1 files changed, 13 insertions, 23 deletions
diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp
index 2d53fc2b..c840960d 100644
--- a/include/cru/common/StringUtil.hpp
+++ b/include/cru/common/StringUtil.hpp
@@ -2,9 +2,6 @@
#include "Base.hpp"
#include <functional>
-#include <stdexcept>
-#include <string>
-#include <string_view>
namespace cru {
using CodePoint = std::int32_t;
@@ -118,9 +115,6 @@ using Utf8CodePointIterator = CodePointIterator<char, &Utf8NextCodePoint>;
using Utf16CodePointIterator = CodePointIterator<char16_t, &Utf16NextCodePoint>;
-void CRU_BASE_API Utf8EncodeCodePointAppend(CodePoint code_point,
- std::string& str);
-
namespace details {
template <typename UInt, int number_of_bit, typename ReturnType>
inline std::enable_if_t<std::is_unsigned_v<UInt>, ReturnType> ExtractBits(
@@ -129,18 +123,18 @@ inline std::enable_if_t<std::is_unsigned_v<UInt>, ReturnType> ExtractBits(
}
} // namespace details
-template <typename TAppend>
-bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) {
- auto write_continue_byte = [&append](std::uint8_t byte6) {
- append((1u << 7) + (((1u << 6) - 1) & byte6));
+template <typename TStr>
+bool Utf8EncodeCodePointAppend(CodePoint code_point, TStr& str) {
+ auto write_continue_byte = [&str](std::uint8_t byte6) {
+ str.push_back((1u << 7) + (((1u << 6) - 1) & byte6));
};
if (code_point >= 0 && code_point <= 0x007F) {
- append(static_cast<char>(code_point));
+ str.push_back(static_cast<char>(code_point));
return true;
} else if (code_point >= 0x0080 && code_point <= 0x07FF) {
std::uint32_t unsigned_code_point = code_point;
- append(
+ str.push_back(
static_cast<char>(details::ExtractBits<std::uint32_t, 5, std::uint8_t>(
(unsigned_code_point >> 6)) +
0b11000000));
@@ -149,7 +143,7 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) {
return true;
} else if (code_point >= 0x0800 && code_point <= 0xFFFF) {
std::uint32_t unsigned_code_point = code_point;
- append(
+ str.push_back(
static_cast<char>(details::ExtractBits<std::uint32_t, 4, std::uint8_t>(
(unsigned_code_point >> (6 * 2))) +
0b11100000));
@@ -160,7 +154,7 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) {
return true;
} else if (code_point >= 0x10000 && code_point <= 0x10FFFF) {
std::uint32_t unsigned_code_point = code_point;
- append(
+ str.push_back(
static_cast<char>(details::ExtractBits<std::uint32_t, 3, std::uint8_t>(
(unsigned_code_point >> (6 * 3))) +
0b11110000));
@@ -176,19 +170,18 @@ bool Utf8EncodeCodePointAppendWithFunc(CodePoint code_point, TAppend&& append) {
}
}
-template <typename TAppend>
-bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point,
- TAppend&& append) {
+template <typename TStr>
+bool Utf16EncodeCodePointAppend(CodePoint code_point, TStr& str) {
if ((code_point >= 0 && code_point <= 0xD7FF) ||
(code_point >= 0xE000 && code_point <= 0xFFFF)) {
- append(static_cast<char16_t>(code_point));
+ str.push_back(static_cast<char16_t>(code_point));
return true;
} else if (code_point >= 0x10000 && code_point <= 0x10FFFF) {
std::uint32_t u = code_point - 0x10000;
- append(static_cast<char16_t>(
+ str.push_back(static_cast<char16_t>(
details::ExtractBits<std::uint32_t, 10, std::uint32_t>(u >> 10) +
0xD800u));
- append(static_cast<char16_t>(
+ str.push_back(static_cast<char16_t>(
details::ExtractBits<std::uint32_t, 10, std::uint32_t>(u) + 0xDC00u));
return true;
} else {
@@ -196,9 +189,6 @@ bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point,
}
}
-std::string CRU_BASE_API ToUtf8(const char16_t* ptr, Index size);
-std::u16string CRU_BASE_API ToUtf16(const char* s, Index size);
-
// If given s is not a valid utf16 string, return value is UD.
bool CRU_BASE_API Utf16IsValidInsertPosition(const char16_t* ptr, Index size,
Index position);