summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringconverter.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-04-27 15:08:50 +0200
committerLars Knoll <lars.knoll@qt.io>2020-05-14 07:48:42 +0200
commit3ce9162ab5b99594d59b654dcdf2009bc500d3d7 (patch)
treeac4b7f25d252ca1f68ce42d9803f0e3cc2b7ea61 /src/corelib/text/qstringconverter.cpp
parent2d43f735b4768c9924f9f7ce97211043b22cd84c (diff)
Construct a string converter by name
Add a constructor, that allows constructing a string converter by name. This is required in some cases and also makes it possible to (in the future) extend the API to 3rd party encodings. Also add a name() accessor. Change-Id: I606d6ce9405ee967f76197b803615e27c5b001cf Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstringconverter.cpp')
-rw-r--r--src/corelib/text/qstringconverter.cpp49
1 files changed, 40 insertions, 9 deletions
diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp
index dfd819e203..9322fcd35e 100644
--- a/src/corelib/text/qstringconverter.cpp
+++ b/src/corelib/text/qstringconverter.cpp
@@ -1441,15 +1441,46 @@ static qsizetype toLatin1Len(qsizetype l) { return l + 1; }
const QStringConverter::Interface QStringConverter::encodingInterfaces[QStringConverter::LastEncoding + 1] =
{
- { QUtf8::convertToUnicode, fromUtf8Len, QUtf8::convertFromUnicode, toUtf8Len },
- { fromUtf16, fromUtf16Len, toUtf16, toUtf16Len },
- { fromUtf16LE, fromUtf16Len, toUtf16LE, toUtf16Len },
- { fromUtf16BE, fromUtf16Len, toUtf16BE, toUtf16Len },
- { fromUtf32, fromUtf32Len, toUtf32, toUtf32Len },
- { fromUtf32LE, fromUtf32Len, toUtf32LE, toUtf32Len },
- { fromUtf32BE, fromUtf32Len, toUtf32BE, toUtf32Len },
- { fromLatin1, fromLatin1Len, toLatin1, toLatin1Len },
- { fromLocal8Bit, fromUtf8Len, toLocal8Bit, toUtf8Len }
+ { "UTF-8", QUtf8::convertToUnicode, fromUtf8Len, QUtf8::convertFromUnicode, toUtf8Len },
+ { "UTF-16", fromUtf16, fromUtf16Len, toUtf16, toUtf16Len },
+ { "UTF-16LE", fromUtf16LE, fromUtf16Len, toUtf16LE, toUtf16Len },
+ { "UTF-16BE", fromUtf16BE, fromUtf16Len, toUtf16BE, toUtf16Len },
+ { "UTF-32", fromUtf32, fromUtf32Len, toUtf32, toUtf32Len },
+ { "UTF-32LE", fromUtf32LE, fromUtf32Len, toUtf32LE, toUtf32Len },
+ { "UTF-32BE", fromUtf32BE, fromUtf32Len, toUtf32BE, toUtf32Len },
+ { "ISO-8859-1", fromLatin1, fromLatin1Len, toLatin1, toLatin1Len },
+ { "Locale", fromLocal8Bit, fromUtf8Len, toLocal8Bit, toUtf8Len }
};
+// match names case insensitive and skipping '-' and '_'
+static bool nameMatch(const char *a, const char *b)
+{
+ while (*a && *b) {
+ if (*a == '-' || *a == '_') {
+ ++a;
+ continue;
+ }
+ if (*b == '-' || *b == '_') {
+ ++b;
+ continue;
+ }
+ if (toupper(*a) != toupper(*b))
+ return false;
+ ++a;
+ ++b;
+ }
+ return !*a && !*b;
+}
+
+QStringConverter::QStringConverter(const char *name)
+ : iface(nullptr)
+{
+ for (int i = 0; i < LastEncoding + 1; ++i) {
+ if (nameMatch(encodingInterfaces[i].name, name)) {
+ iface = encodingInterfaces + i;
+ break;
+ }
+ }
+}
+
QT_END_NAMESPACE