From d4b731aeaf16c6f3f7d0d30118122d753780e2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Fri, 22 Dec 2023 09:32:47 +0100 Subject: [PATCH] Use MIB numbers for QTextCodec fetching Discard the "broken name cache" workaround and use QTextCodec search based on MIB numbers. --- src/common/textcodec.cpp | 91 ++++++++++++++++++++++++---------------- src/common/textcodec.h | 2 - 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/common/textcodec.cpp b/src/common/textcodec.cpp index 28db0683..36b66ff5 100644 --- a/src/common/textcodec.cpp +++ b/src/common/textcodec.cpp @@ -1,48 +1,67 @@ #include #include "textcodec.h" -/* When Qt is compiled with ICU support, QTextCodec::codecForName() is very - slow due to broken codec name caching (the function does dozens of - comparisons and only then asks the cache...), so we use our own map. */ -static QMap initCodecs() +static QTextCodec *codec(int mib) { - QMap map; + QTextCodec *c = QTextCodec::codecForMib(mib); + if (!c) + qWarning("MIB-%d: No such QTextCodec, using UTF-8", mib); - map.insert(65001, 0); - map.insert(874, QTextCodec::codecForName("Windows-874")); - map.insert(932, QTextCodec::codecForName("Shift-JIS")); - map.insert(936, QTextCodec::codecForName("GB18030")); - map.insert(949, QTextCodec::codecForName("EUC-KR")); - map.insert(950, QTextCodec::codecForName("Big5")); - map.insert(1250, QTextCodec::codecForName("Windows-1250")); - map.insert(1251, QTextCodec::codecForName("Windows-1251")); - map.insert(1252, QTextCodec::codecForName("Windows-1252")); - map.insert(1253, QTextCodec::codecForName("Windows-1253")); - map.insert(1254, QTextCodec::codecForName("Windows-1254")); - map.insert(1255, QTextCodec::codecForName("Windows-1255")); - map.insert(1256, QTextCodec::codecForName("Windows-1256")); - map.insert(1257, QTextCodec::codecForName("Windows-1257")); - map.insert(1258, QTextCodec::codecForName("Windows-1258")); - - return map; -} - -const QMap &TextCodec::codecs() -{ - static QMap map = initCodecs(); - return map; + return c; } TextCodec::TextCodec(int codepage) { - const QMap &map = codecs(); - - QMap::const_iterator it(map.find(codepage)); - if (it == map.cend()) { - qWarning("%d: Unsupported codepage, using UTF-8", codepage); - _codec = 0; - } else - _codec = *it; + switch (codepage) { + case 874: + _codec = codec(2109); + break; + case 932: + _codec = codec(17); + break; + case 936: + _codec = codec(114); + break; + case 949: + _codec = codec(38); + break; + case 950: + _codec = codec(2026); + break; + case 1250: + _codec = codec(2250); + break; + case 1251: + _codec = codec(2251); + break; + case 1252: + _codec = codec(2252); + break; + case 1253: + _codec = codec(2253); + break; + case 1254: + _codec = codec(2254); + break; + case 1255: + _codec = codec(2255); + break; + case 1256: + _codec = codec(2256); + break; + case 1257: + _codec = codec(2257); + break; + case 1258: + _codec = codec(2258); + break; + case 65001: + _codec = 0; + break; + default: + qWarning("%d: Unknown codepage, using UTF-8", codepage); + _codec = 0; + } } QString TextCodec::toString(const QByteArray &ba) const diff --git a/src/common/textcodec.h b/src/common/textcodec.h index a5279ce7..9cb68067 100644 --- a/src/common/textcodec.h +++ b/src/common/textcodec.h @@ -16,8 +16,6 @@ public: private: QTextCodec *_codec; - - static const QMap &codecs(); }; #endif // TEXTCODEC_H