summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2025-10-22 19:43:07 +0200
committerMarc Mutz <marc.mutz@qt.io>2025-10-27 21:49:18 +0200
commitdae484f08252194d0d4978d72e97f68a693d47dd (patch)
treed2f01974c5e496c3fc324c660407c1c686b16a1e /src
parent961271052a8998d87ba84c0b56a5c55c8354c09e (diff)
QUnicodeTools: fix weird variable assignment in initScripts() loop
The old code updated the `eor` variable in the third field of the for loop, after the increment of the loop variable, `i`, to the then-value of `i`. The variable was initialized as zero. This is a very roundabout way of doing things, because, if you look at it from the right angle, `eor` will always have the value 'i' has when entering the loop body. Proof: - First round: i = 0, eor = 0. So i == eor. Check. - Next round: i = 1 + whatever value `i` had at the end of the previous iteration. eor := i, so i == eor. Check. So rewrite the code to create `eor` at the beginning of the loop body, with the then-value of 'i'. This allows marking it const, too, and scoping it correctly, drastically improving readability. The tighter scoping runs afoul of the assert(eor == string.size()) after the loop, which, however, is pointless, because it's true by construction: the loop has no break statement, so the only way it can be exited is by failing the loop condition. At that point, eor := i and i == string.size(), so eor == string.size(). Partially reverts 3df159ba174c1775a0e77d2305a639eeab1ea71d, but the loose scope of the variable was present even before that. Pick-to: 6.10 6.8 6.5 Change-Id: I983aef94caa8a3bc09ab378b8bb9bb4a18dabeb4 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qunicodetools.cpp5
1 files changed, 2 insertions, 3 deletions
diff --git a/src/corelib/text/qunicodetools.cpp b/src/corelib/text/qunicodetools.cpp
index 634a3da4890..c0cb43a0685 100644
--- a/src/corelib/text/qunicodetools.cpp
+++ b/src/corelib/text/qunicodetools.cpp
@@ -2819,10 +2819,10 @@ Q_CORE_EXPORT void initCharAttributes(QStringView string,
Q_CORE_EXPORT void initScripts(QStringView string, ScriptItemArray *scripts)
{
qsizetype sor = 0;
- qsizetype eor = 0;
QChar::Script script = QChar::Script_Common;
- for (qsizetype i = 0; i < string.size(); ++i, eor = i) {
+ for (qsizetype i = 0; i < string.size(); ++i) {
+ const auto eor = i;
char32_t ucs4 = string[i].unicode();
if (QChar::isHighSurrogate(ucs4) && i + 1 < string.size()) {
ushort low = string[i + 1].unicode();
@@ -2863,7 +2863,6 @@ Q_CORE_EXPORT void initScripts(QStringView string, ScriptItemArray *scripts)
}
Q_ASSERT(script >= QChar::Script_Common);
- Q_ASSERT(eor == string.size());
scripts->append(ScriptItem{sor, script});
}