diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2018-05-13 21:53:07 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2018-06-22 20:12:41 +0000 |
commit | cad7100fda1b27ba56c4d9efc6bceba62859dfbc (patch) | |
tree | 942ff836a94aab364ae8c408dcfa44d8808c8134 /src/corelib/tools/qbytearray.h | |
parent | c699daeceb4448c4545a67ffdba27bcb3b994114 (diff) |
QByteArray: add compare() with case sensitivity options
Need to do the same for startsWith() and endsWith(). indexOf() is a lot
harder.
[ChangeLog][QtCore][QByteArray] Added compare(), which takes
Qt::CaseSensitivity as one of the parameters. This function is more
efficient than using toLower() or toUpper() and then comparing.
Change-Id: Ib48364abee9f464c96c6fffd152e69bde4194df7
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/corelib/tools/qbytearray.h')
-rw-r--r-- | src/corelib/tools/qbytearray.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index bed710c597..8ee3a29ecc 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -99,6 +99,7 @@ inline int qstrncmp(const char *str1, const char *str2, uint len) } Q_CORE_EXPORT int qstricmp(const char *, const char *); Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len); +Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1); // implemented in qvsnprintf.cpp Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap); @@ -231,6 +232,9 @@ public: int count(const char *a) const; int count(const QByteArray &a) const; + inline int compare(const char *c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline int compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT QByteArray left(int len) const; Q_REQUIRED_RESULT QByteArray right(int len) const; Q_REQUIRED_RESULT QByteArray mid(int index, int len = -1) const; @@ -603,6 +607,16 @@ inline bool QByteArray::contains(const QByteArray &a) const { return indexOf(a) != -1; } inline bool QByteArray::contains(char c) const { return indexOf(c) != -1; } +inline int QByteArray::compare(const char *c, Qt::CaseSensitivity cs) const +{ + return cs == Qt::CaseSensitive ? qstrcmp(*this, c) : + qstrnicmp(data(), size(), c, -1); +} +inline int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs) const +{ + return cs == Qt::CaseSensitive ? qstrcmp(*this, a) : + qstrnicmp(data(), size(), a.data(), a.size()); +} inline bool operator==(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW { return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); } inline bool operator==(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW |