aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/UniConversion.cxx
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2019-03-20 19:18:57 +1100
committerNeil <nyamatongwe@gmail.com>2019-03-20 19:18:57 +1100
commit31a735c8ec0a4f7c62b162cd1c173bfef76737c5 (patch)
treee7a3ad283f5b7c44d79cfbcdb78d8cdf34b68bf2 /src/UniConversion.cxx
parent4ac748ba2238d3abd5227918d8174507d8fa3ec1 (diff)
downloadscintilla-mirror-31a735c8ec0a4f7c62b162cd1c173bfef76737c5.tar.gz
Backport: Implement WStringFromUTF8 to simplify code that creates wstring objects for
regular expressions and calling the Win32 API. Backport of changeset 7325:6148329fb2f3, but replaced std::string_view usage with const char* and size_t components. Also used #ifdef instead of C++17 `if constexpr` at suggestion of Neil.
Diffstat (limited to 'src/UniConversion.cxx')
-rw-r--r--src/UniConversion.cxx25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/UniConversion.cxx b/src/UniConversion.cxx
index 6cd6a8ba9..8cbb3cdd2 100644
--- a/src/UniConversion.cxx
+++ b/src/UniConversion.cxx
@@ -162,6 +162,17 @@ size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) {
return ui;
}
+size_t UTF32Length(const char *s, size_t len) noexcept {
+ size_t ulen = 0;
+ for (size_t i = 0; i < len;) {
+ const unsigned char ch = s[i];
+ const unsigned int byteCount = UTF8BytesOfLead[ch];
+ i += byteCount;
+ ulen++;
+ }
+ return ulen;
+}
+
size_t UTF32FromUTF8(const char *s, size_t len, unsigned int *tbuf, size_t tlen) {
size_t ui = 0;
for (size_t i = 0; i < len;) {
@@ -215,6 +226,20 @@ size_t UTF32FromUTF8(const char *s, size_t len, unsigned int *tbuf, size_t tlen)
return ui;
}
+std::wstring WStringFromUTF8(const char *s, size_t len) {
+#ifdef _WIN32
+ const size_t len16 = UTF16Length(s, len);
+ std::wstring ws(len16, 0);
+ UTF16FromUTF8(s, len, &ws[0], len16);
+ return ws;
+#else
+ const size_t len32 = UTF32Length(s, len);
+ std::wstring ws(len32, 0);
+ UTF32FromUTF8(s, len, reinterpret_cast<unsigned int *>(&ws[0]), len32);
+ return ws;
+#endif
+}
+
unsigned int UTF16FromUTF32Character(unsigned int val, wchar_t *tbuf) noexcept {
if (val < SUPPLEMENTAL_PLANE_FIRST) {
tbuf[0] = static_cast<wchar_t>(val);