From 9ed389bf15787266c207435d712b9e225db07ad9 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 28 Feb 2017 12:03:46 +0100 Subject: Bugfix in QDateTimeParser's findTextEntry() If a later month-or-day were to have a name that's a prefix of an earlier one's name, the code would have selected the longer name as best match when the text matched is the shorter name, simply because it found that one first. (Found, on Turkish Cuma(rtesi)? in Thiago's recent new test, by reversing the loop that iterated the list.) Make an exact match win and a match of a full name beat any prefix match of the same length. Change-Id: I8d954b83ccc25e4f47af2e558036d714685cef5e Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetimeparser.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index 5871587f8c..62dd25e072 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -1248,9 +1248,12 @@ end: \internal \brief Returns the index in \a entries with the best prefix match to \a text - Scans \a entries looking for an entry overlapping \a text as much as possible. - Records the length of overlap in *used (if \a used is non-NULL) and the first - entry that overlapped this much in *usedText (if \a usedText is non-NULL). + Scans \a entries looking for an entry overlapping \a text as much as possible + (an exact match beats any prefix match; a match of the full entry as prefix of + text beats any entry but one matching a longer prefix; otherwise, the match of + longest prefix wins, earlier entries beating later on a draw). Records the + length of overlap in *used (if \a used is non-NULL) and the first entry that + overlapped this much in *usedText (if \a usedText is non-NULL). */ static int findTextEntry(const QString &text, const QVector &entries, QString *usedText, int *used) { @@ -1267,9 +1270,12 @@ static int findTextEntry(const QString &text, const QVector &entries, Q int i = 0; while (i < limit && text.at(i) == name.at(i).toLower()) ++i; - if (i > bestCount) { + // Full match beats an equal prefix match: + if (i > bestCount || (i == bestCount && i == name.size())) { bestCount = i; bestMatch = n; + if (i == name.size() && i == text.size()) + break; // Exact match, name == text, wins. } } if (usedText && bestMatch != -1) -- cgit v1.2.3