diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-10-30 13:02:03 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-10-31 19:36:00 +0200 |
commit | e90705687f5faba41d9a8544c875d8c6b1cedbdc (patch) | |
tree | 8b82b8142355b14f0e31cc71f0f0c0679c434a1e /src/corelib/kernel/qmetaobject.cpp | |
parent | 7c41f31efa651cd3cbba33b3220cc810da8b174e (diff) |
QMetaEnum: refactor keysToValue
- Show warning messages for malformed keys string
- Use QBAV instead of QL1SV, some methods in the public API may get
QU8SV overloads in later commits
- Extend unittests; also rename the unittest, it's really testing
key(s)ToValue()
Change-Id: Iec944ef4c2c5d18ab038cb933e954cf50c912523
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel/qmetaobject.cpp')
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 6c3f3a54f0..1c7536eb09 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -3240,6 +3240,51 @@ const char *QMetaEnum::valueToKey(int value) const return nullptr; } +static bool parseEnumFlags(QByteArrayView v, QVarLengthArray<QByteArrayView, 10> &list) +{ + v = v.trimmed(); + if (v.empty()) { + qWarning("QMetaEnum::keysToValue: empty keys string."); + return false; + } + + qsizetype sep = v.indexOf('|', 0); + if (sep == 0) { + qWarning("QMetaEnum::keysToValue: malformed keys string, starts with '|', \"%s\"", + v.constData()); + return false; + } + + if (sep == -1) { // One flag + list.push_back(v); + return true; + } + + if (v.endsWith('|')) { + qWarning("QMetaEnum::keysToValue: malformed keys string, ends with '|', \"%s\"", + v.constData()); + return false; + } + + const auto begin = v.begin(); + const auto end = v.end(); + auto b = begin; + for (; b != end && sep != -1; sep = v.indexOf('|', sep)) { + list.push_back({b, begin + sep}); + ++sep; // Skip over '|' + b = begin + sep; + if (*b == '|') { + qWarning("QMetaEnum::keysToValue: malformed keys string, has two consecutive '|': " + "\"%s\"", v.constData()); + return false; + } + } + + // The rest of the string + list.push_back({b, end}); + return true; +} + /*! Returns the value derived from combining together the values of the \a keys using the OR operator, or -1 if \a keys is not @@ -3266,7 +3311,11 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const }; int value = 0; - for (const QLatin1StringView &untrimmed : qTokenize(QLatin1StringView{keys}, u'|')) { + QVarLengthArray<QByteArrayView, 10> list; + const bool r = parseEnumFlags(QByteArrayView{keys}, list); + if (!r) + return -1; + for (const auto &untrimmed : list) { const auto parsed = parse_scope(untrimmed.trimmed()); if (parsed.scope && *parsed.scope != objectClassName(mobj)) return -1; // wrong type name in qualified name |