diff options
author | Michele Calgaro <[email protected]> | 2025-01-29 18:05:37 +0900 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2025-01-30 19:06:16 +0900 |
commit | c5cda03125a6d34c179d968011083bceb87976bd (patch) | |
tree | 33c2ba873b23cf503ed3c3aa1c52d3fac1006245 /src/codecs/tqgb18030codec.cpp | |
parent | d517cda6bdb0160be39a96712d4cf6036b920be3 (diff) | |
download | tqt3-c5cda03125a6d34c179d968011083bceb87976bd.tar.gz tqt3-c5cda03125a6d34c179d968011083bceb87976bd.zip |
Add support for surrogate pairs to TQChar API.
This relates to issue #162.
The new code is partially taken from Qt4 but with some local rework.
Signed-off-by: Michele Calgaro <[email protected]>
Diffstat (limited to 'src/codecs/tqgb18030codec.cpp')
-rw-r--r-- | src/codecs/tqgb18030codec.cpp | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/src/codecs/tqgb18030codec.cpp b/src/codecs/tqgb18030codec.cpp index 0ae2fb4ff..d2578dc8e 100644 --- a/src/codecs/tqgb18030codec.cpp +++ b/src/codecs/tqgb18030codec.cpp @@ -184,18 +184,16 @@ TQCString TQGb18030Codec::fromUnicode(const TQString& uc, int& lenInOut) const if ( ch.row() == 0x00 && ch.cell() < 0x80 ) { // ASCII *cursor++ = ch.cell(); - } else if ((ch.unicode() & 0xf800) == 0xd800) { - unsigned short high = ch.unicode(); + } else if (ch.isHighSurrogate()) { // surrogates area. check for correct encoding // we need at least one more character, first the high surrogate, then the low one - if (i == l-1 || high >= 0xdc00) + if (i == l-1) *cursor++ = '?'; else { - unsigned short low = uc[i+1].unicode(); - if (low >= 0xdc00 && low <= 0xdfff) { + if (uc[i+1].isLowSurrogate()) { // valid surrogate pair + uint u = TQChar::surrogateToUcs4(uc[i], uc[i + 1]); ++i; - uint u = (high-0xd800)*0x400+(low-0xdc00)+0x10000; len = qt_UnicodeToGb18030(u, buf); if (len >= 2) { for (int j=0; j<len; j++) @@ -241,15 +239,13 @@ TQString TQGb18030Codec::toUnicode(const char* chars, int len) const uint u = qt_Gb18030ToUnicode( (const uchar*)(chars + i), clen ); if (clen == 2 || clen == 4) { - if (u < 0x10000) + if (!TQChar::requiresSurrogates(u)) { result += TQValidChar(u); + } else { // encode into surrogate pair - u -= 0x10000; - unsigned short high = u/0x400 + 0xd800; - unsigned short low = u%0x400 + 0xdc00; - result += TQChar(high); - result += TQChar(low); + result += TQChar(TQChar::highSurrogate(u)); + result += TQChar(TQChar::lowSurrogate(u)); } i += clen; } else if (i < len) { @@ -402,15 +398,13 @@ public: int clen = 4; uint u = qt_Gb18030ToUnicode(buf, clen); if (clen == 4) { - if (u < 0x10000) + if (!TQChar::requiresSurrogates(u)) { result += TQValidChar(u); + } else { // encode into surrogate pair - u -= 0x10000; - unsigned short high = u/0x400 + 0xd800; - unsigned short low = u%0x400 + 0xdc00; - result += TQChar(high); - result += TQChar(low); + result += TQChar(TQChar::highSurrogate(u)); + result += TQChar(TQChar::lowSurrogate(u)); } } else { result += TQChar::replacement; |