diff options
| author | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-01-29 18:05:37 +0900 |
|---|---|---|
| committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-01-30 19:06:16 +0900 |
| commit | c5cda03125a6d34c179d968011083bceb87976bd (patch) | |
| tree | 33c2ba873b23cf503ed3c3aa1c52d3fac1006245 /src/codecs/tqutfcodec.cpp | |
| parent | d517cda6bdb0160be39a96712d4cf6036b920be3 (diff) | |
| download | tqt-c5cda03125a6d34c179d968011083bceb87976bd.tar.gz tqt-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 <michele.calgaro@yahoo.it>
Diffstat (limited to 'src/codecs/tqutfcodec.cpp')
| -rw-r--r-- | src/codecs/tqutfcodec.cpp | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/src/codecs/tqutfcodec.cpp b/src/codecs/tqutfcodec.cpp index 1125aa9f3..eba25e505 100644 --- a/src/codecs/tqutfcodec.cpp +++ b/src/codecs/tqutfcodec.cpp @@ -64,13 +64,10 @@ TQCString TQUtf8Codec::fromUnicode(const TQString& uc, int& lenInOut) const if ( u < 0x0800 ) { *cursor++ = 0xc0 | ((uchar) (u >> 6)); } else { - if (u >= 0xd800 && u < 0xdc00 && i < l-1) { - unsigned short low = ch[1].unicode(); - if (low >= 0xdc00 && low < 0xe000) { - ++ch; - ++i; - u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000; - } + if (ch[0].isHighSurrogate() && i < (l - 1) && ch[1].isLowSurrogate()) { + u = TQChar::surrogateToUcs4(ch[0], ch[1]); + ++ch; + ++i; } if (u > 0xffff) { // see TQString::fromUtf8() and TQString::utf8() for explanations @@ -179,16 +176,14 @@ public: uc = (uc << 6) | (ch & 0x3f); need--; if ( !need ) { - if (uc > 0xffff) { + if (TQChar::requiresSurrogates(uc)) { // surrogate pair - uc -= 0x10000; - unsigned short high = uc/0x400 + 0xd800; - unsigned short low = uc%0x400 + 0xdc00; - *qch++ = TQChar(high); - *qch++ = TQChar(low); + *qch++ = TQChar(TQChar::highSurrogate(uc)); + *qch++ = TQChar(TQChar::lowSurrogate(uc)); headerDone = TRUE; } else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || (uc >= 0xfffe)) { - *qch++ = TQChar::replacement; + // overlong sequence, UTF16 surrogate or BOM + *qch++ = TQChar::replacement; } else { if (headerDone || TQChar(uc) != TQChar::byteOrderMark) *qch++ = uc; |
