From bebf89e1379d6948728a4ebd669de6b63dc3f426 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 4 Apr 2016 10:58:06 +0200 Subject: QDateTimeParser: Avoid repetition in sectionMaxSize The format to use was computed, every time round a loop, in both branches of a ?: choice, duplicating code and potentially computation. Pull it out into a const computed once before the loop. A conditional return 2 is pointless for the #if-branch which returns 2 unconditionally, so move it into the #else. Change-Id: Ia583e958e24f9f37b92cb3f2a173bc07e88bcd06 Reviewed-by: Marc Mutz --- src/corelib/tools/qdatetimeparser.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index d4004098c5..8e3eaf7074 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -598,19 +598,20 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const // fall through #endif case MonthSection: - if (count <= 2) - return 2; - #ifdef QT_NO_TEXTDATE return 2; #else + if (count <= 2) + return 2; + { int ret = 0; const QLocale l = locale(); + const QLocale::FormatType format = count == 4 ? QLocale::LongFormat : QLocale::ShortFormat; for (int i=1; i<=mcount; ++i) { const QString str = (s == MonthSection - ? l.monthName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat) - : l.dayName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat)); + ? l.monthName(i, format) + : l.dayName(i, format)); ret = qMax(str.size(), ret); } return ret; -- cgit v1.2.3 From 6a7f5dab0d6e86950f87123c69724018aa140770 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 18 Jan 2016 13:03:49 +0100 Subject: Explain QTimeZonePrivate::isValidId a bit more carefully. Its "rules" are actually guidelines, its suggested regex was wrong, its actual implementation was fuzzier than its documentation suggested and the exception it tacitly permitted should be distinguished from the stricter rules it otherwise appears to implement. There was also a redundant check ('-' had been handled earlier in the chained if). Explain why the situation is tricky, fix the regex mentioned (making it more readable, too) and note what might be worth doing a little more fussily, without actually changing code behavior. Change-Id: I93fa0da0640a134e5d84011b435a186576824063 Reviewed-by: Marc Mutz --- src/corelib/tools/qtimezoneprivate.cpp | 57 ++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qtimezoneprivate.cpp b/src/corelib/tools/qtimezoneprivate.cpp index ab8332aea3..be53a07591 100644 --- a/src/corelib/tools/qtimezoneprivate.cpp +++ b/src/corelib/tools/qtimezoneprivate.cpp @@ -441,22 +441,45 @@ QTimeZone::OffsetData QTimeZonePrivate::toOffsetData(const QTimeZonePrivate::Dat return offsetData; } -// If the format of the ID is valid +// Is the format of the ID valid ? bool QTimeZonePrivate::isValidId(const QByteArray &ianaId) { - // Rules for defining TZ/IANA names as per ftp://ftp.iana.org/tz/code/Theory - // 1. Use only valid POSIX file name components - // 2. Within a file name component, use only ASCII letters, `.', `-' and `_'. - // 3. Do not use digits - // 4. A file name component must not exceed 14 characters or start with `-' - // Aliases such as "Etc/GMT+7" and "SystemV/EST5EDT" are valid so we need to accept digits, ':', and '+'. - - // The following would be preferable if QRegExp would work on QByteArrays directly: - // const QRegExp rx(QStringLiteral("[a-z0-9:+._][a-z0-9:+._-]{,13}(?:/[a-z0-9:+._][a-z0-9:+._-]{,13})*"), - // Qt::CaseInsensitive); - // return rx.exactMatch(ianaId); - - // hand-rolled version: + /* + Main rules for defining TZ/IANA names as per ftp://ftp.iana.org/tz/code/Theory + 1. Use only valid POSIX file name components + 2. Within a file name component, use only ASCII letters, `.', `-' and `_'. + 3. Do not use digits (except in a [+-]\d+ suffix, when used). + 4. A file name component must not exceed 14 characters or start with `-' + However, the rules are really guidelines - a later one says + - Do not change established names if they only marginally violate the + above rules. + We may, therefore, need to be a bit slack in our check here, if we hit + legitimate exceptions in real time-zone databases. + + In particular, aliases such as "Etc/GMT+7" and "SystemV/EST5EDT" are valid + so we need to accept digits, ':', and '+'; aliases typically have the form + of POSIX TZ strings, which allow a suffix to a proper IANA name. A POSIX + suffix starts with an offset (as in GMT+7) and may continue with another + name (as in EST5EDT, giving the DST name of the zone); a further offset is + allowed (for DST). The ("hard to describe and [...] error-prone in + practice") POSIX form even allows a suffix giving the dates (and + optionally times) of the annual DST transitions. Hopefully, no TZ aliases + go that far, but we at least need to accept an offset and (single + fragment) DST-name. + + But for the legacy complications, the following would be preferable if + QRegExp would work on QByteArrays directly: + const QRegExp rx(QStringLiteral("[a-z+._][a-z+._-]{,13}" + "(?:/[a-z+._][a-z+._-]{,13})*" + // Optional suffix: + "(?:[+-]?\d{1,2}(?::\d{1,2}){,2}" // offset + // one name fragment (DST): + "(?:[a-z+._][a-z+._-]{,13})?)"), + Qt::CaseInsensitive); + return rx.exactMatch(ianaId); + */ + + // Somewhat slack hand-rolled version: const int MinSectionLength = 1; const int MaxSectionLength = 14; int sectionLength = 0; @@ -472,11 +495,11 @@ bool QTimeZonePrivate::isValidId(const QByteArray &ianaId) } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch == '_') + && !(ch == '.') + // Should ideally check these only happen as an offset: && !(ch >= '0' && ch <= '9') - && !(ch == '-') && !(ch == '+') - && !(ch == ':') - && !(ch == '.')) { + && !(ch == ':')) { return false; // violates (2) } } -- cgit v1.2.3 From 2a282551ac7735538d9627537941c58de266f2c4 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 5 Apr 2016 16:23:50 +0200 Subject: Remove empty first lines of files. They might upset licensing related tools. Change-Id: I858d21fc418ba16959c88847b559b11bea29ed6b Reviewed-by: Oswald Buddenhagen --- src/corelib/tools/qhash.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index ac9e08de8b..d40570d347 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -1,4 +1,3 @@ - /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. -- cgit v1.2.3 From aee197da01b6fd6b5bd56e203388c60ca64ed6a6 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 6 Apr 2016 12:11:40 +0200 Subject: QStringRef: fix some QStringRef <> const char * relational operators Their implementations were swapped. Found while extending tst_qstringapisymmetry, which only exists in dev, so test will be added to dev once this change has merged up. [ChangeLog][QtCore][QStringRef] Fixed relational operators against (const char*) to return the correct result. Change-Id: I3f331037571b9a543a6885802836b768143d1c1a Reviewed-by: Konstantin Ritt --- src/corelib/tools/qstring.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 1fbcff35d1..886973fe10 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1535,9 +1535,9 @@ inline QT_ASCII_CAST_WARN bool QStringRef::operator!=(const char *s) const inline QT_ASCII_CAST_WARN bool QStringRef::operator<(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) < 0; } inline QT_ASCII_CAST_WARN bool QStringRef::operator<=(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) > 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator>(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) <= 0; } +inline QT_ASCII_CAST_WARN bool QStringRef::operator>(const char *s) const +{ return QString::compare_helper(constData(), size(), s, -1) > 0; } inline QT_ASCII_CAST_WARN bool QStringRef::operator>=(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) >= 0; } @@ -1546,13 +1546,13 @@ inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2) inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2) { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } -inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2) { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } +inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2) +{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } +{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } +{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const -- cgit v1.2.3