aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmltyperegistrar/qanystringviewutils_p.h
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-12-13 17:11:09 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-12-20 13:05:21 +0100
commit62ed428324cc024b45881b4098d5a7576059e689 (patch)
tree5af7263a3d8082caffcc4fe4d7af6daf65c23f8f /src/qmltyperegistrar/qanystringviewutils_p.h
parent9b87e051cdbe1c8507071e2834b069b808bc1e04 (diff)
qmltyperegistrar: Fix QAnyStringViewUtils
The logic for startsWith, endsWith, and split only works if at least one parameter is encoded in US-ASCII. To avoid false negatives we would like to make sure that slicing the first parameter by the second parameter's length results in a substring that encodes the same number of characters as the second parameter. This is clearly not always the case if the two parameters are given in different variable-length encodings. US-ASCII is a fixed length encoding and all its characters are encoded with a fixed length in UTF-8 and UTF-16, too. Therefore, if we give the second parameter in US-ASCII and the result matches, we can be sure it actually matches. If not, the reason may be: a, the number of characters covered by the given offset in the first parameter is different. In that case we know we have some "high" characters that couldn't be encoded in ASCII and cannot match. b, we have a regular mismatch between two ASCII-encodable character sequences. Both are actual differences between the strings. Since we have no specific type to hold only US-ASCII strings, we use QLatin1StringView as the closest type we have. Change-Id: I8cced40c8e61fc753f7a8ee888513769f99d8085 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/qmltyperegistrar/qanystringviewutils_p.h')
-rw-r--r--src/qmltyperegistrar/qanystringviewutils_p.h13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/qmltyperegistrar/qanystringviewutils_p.h b/src/qmltyperegistrar/qanystringviewutils_p.h
index ed5cc0bca7..b4a84d958a 100644
--- a/src/qmltyperegistrar/qanystringviewutils_p.h
+++ b/src/qmltyperegistrar/qanystringviewutils_p.h
@@ -47,13 +47,17 @@ inline QAnyStringView toStringView(const QCborMap &map, QLatin1StringView key)
return toStringView(map[key]);
}
-inline bool endsWith(QAnyStringView whole, QAnyStringView part)
+// Note: This only works if part is US-ASCII, but there is no type to encode this information!
+inline bool endsWith(QAnyStringView whole, QLatin1StringView part)
{
+ Q_ASSERT(QtPrivate::isAscii(part));
return whole.length() >= part.length() && whole.last(part.length()) == part;
}
-inline bool startsWith(QAnyStringView whole, QAnyStringView part)
+// Note: This only works if part is US-ASCII, but there is no type to encode this information!
+inline bool startsWith(QAnyStringView whole, QLatin1StringView part)
{
+ Q_ASSERT(QtPrivate::isAscii(part));
return whole.length() >= part.length() && whole.first(part.length()) == part;
}
@@ -131,8 +135,11 @@ auto processAsUtf8(StringView string, Handler &&handler)
Q_UNREACHABLE();
}
-inline QList<QAnyStringView> split(QAnyStringView source, QAnyStringView sep)
+// Note: This only works if sep is US-ASCII, but there is no type to encode this information!
+inline QList<QAnyStringView> split(QAnyStringView source, QLatin1StringView sep)
{
+ Q_ASSERT(QtPrivate::isAscii(sep));
+
QList<QAnyStringView> list;
if (source.isEmpty()) {
list.append(source);