diff options
author | mitchell <unknown> | 2018-05-05 12:08:22 -0400 |
---|---|---|
committer | mitchell <unknown> | 2018-05-05 12:08:22 -0400 |
commit | ad5951840a7e9d19c3652151e57466fa2c94e6a7 (patch) | |
tree | a77144132bd8fe505c8e0340e1e4e7d66d44bf07 /src/UniConversion.cxx | |
parent | 93462d87c3c8f398d5900be84349f29cb088d849 (diff) | |
download | scintilla-mirror-ad5951840a7e9d19c3652151e57466fa2c94e6a7.tar.gz |
Backport: Feature [feature-requests:#1212]. Move Unicode conversions into UniConversion.
Move Unicode conversion functions UnicodeFromUTF8 and UTF8FromUTF32Character into UniConversion.
Backport of changeset 6645:463fa6965d9a.
Diffstat (limited to 'src/UniConversion.cxx')
-rw-r--r-- | src/UniConversion.cxx | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/UniConversion.cxx b/src/UniConversion.cxx index c4025c403..f2bf8d30f 100644 --- a/src/UniConversion.cxx +++ b/src/UniConversion.cxx @@ -65,6 +65,26 @@ void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len) { putf[k] = '\0'; } +void UTF8FromUTF32Character(int uch, char *putf) { + size_t k = 0; + if (uch < 0x80) { + putf[k++] = static_cast<char>(uch); + } else if (uch < 0x800) { + putf[k++] = static_cast<char>(0xC0 | (uch >> 6)); + putf[k++] = static_cast<char>(0x80 | (uch & 0x3f)); + } else if (uch < 0x10000) { + putf[k++] = static_cast<char>(0xE0 | (uch >> 12)); + putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f)); + putf[k++] = static_cast<char>(0x80 | (uch & 0x3f)); + } else { + putf[k++] = static_cast<char>(0xF0 | (uch >> 18)); + putf[k++] = static_cast<char>(0x80 | ((uch >> 12) & 0x3f)); + putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f)); + putf[k++] = static_cast<char>(0x80 | (uch & 0x3f)); + } + putf[k] = '\0'; +} + size_t UTF16Length(const char *s, size_t len) { size_t ulen = 0; const unsigned char *us = reinterpret_cast<const unsigned char *>(s); @@ -101,7 +121,7 @@ size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) { break; } - const size_t outLen = (byteCount < 4) ? 1 : 2; + const size_t outLen = UTF16LengthFromUTF8ByteCount(byteCount); if (ui + outLen > tlen) { throw std::runtime_error("UTF16FromUTF8: attempted write beyond end"); } |