Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-34177

Compare QString with const char*

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.1.1
    • None

    Description

      Comparing "const char*" and QString with operator "<", ">", "<=" and ">=" gives wrong results. The inline functions in "qstring.h" from line 1043 till 1050 make terrible mistakes:

      inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2)

      { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }

      inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2)

      { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }

      inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2)

      { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }

      inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2)

      { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }

      Also in line 1374 till 1381:
      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; }

      As you can see the arguments s1 and s2 are swap for "compare_helper" but the operators aren't swap. Also the operator "<=" returns "> 0" and ">" returns "<= 0". So "a <= a" would return false and "a > a" would return true.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            mwaldi Michael Waldmann
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes