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

Compare QString with const char*

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2: Important P2: Important
    • 5.2.1
    • 5.1.1
    • None
    • 9eba69d7f914e4823e94699c5653df5ad279eb46

      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.

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

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

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes