Fix comparison issue with QIByteArray and improve performance by inlining all methods.

This commit is contained in:
Nathan Osman
2016-10-11 13:07:09 -07:00
parent 1c1c1e7752
commit e35fdbde78
4 changed files with 75 additions and 108 deletions

View File

@@ -23,6 +23,8 @@
#ifndef QHTTPENGINE_QIBYTEARRAY_H
#define QHTTPENGINE_QIBYTEARRAY_H
#include <cctype>
#include <QByteArray>
#include "qhttpengine_global.h"
@@ -38,28 +40,57 @@ class QHTTPENGINE_EXPORT QIByteArray : public QByteArray
{
public:
/**
* @brief Create an empty QIByteArray
*/
QIByteArray();
QIByteArray() {}
QIByteArray(const QByteArray &other) : QByteArray(other) {}
QIByteArray(const QIByteArray &other) : QByteArray(other) {}
QIByteArray(const char *data, int size = -1) : QByteArray(data, size) {}
/**
* @brief QIByteArray copy constructor
*/
QIByteArray(const QByteArray &other);
inline bool operator==(const QString &s2) const { return toLower() == s2.toLower(); }
inline bool operator!=(const QString &s2) const { return toLower() != s2.toLower(); }
inline bool operator<(const QString &s2) const { return toLower() < s2.toLower(); }
inline bool operator>(const QString &s2) const { return toLower() > s2.toLower(); }
inline bool operator<=(const QString &s2) const { return toLower() <= s2.toLower(); }
inline bool operator>=(const QString &s2) const { return toLower() >= s2.toLower(); }
/**
* @brief Create a QIByteArray from a const char *
*/
QIByteArray(const char *data, int size = -1);
bool contains(char c) const { return toLower().contains(tolower(c)); }
bool contains(const char *c) const { return toLower().contains(QByteArray(c).toLower()); }
bool contains(const QByteArray &a) const { return toLower().contains(a.toLower()); }
};
QHTTPENGINE_EXPORT bool operator==(const QIByteArray &a1, const QIByteArray &a2);
QHTTPENGINE_EXPORT bool operator==(const QIByteArray &a1, const QString &a2);
QHTTPENGINE_EXPORT bool operator==(const QString &a1, const QIByteArray &a2);
QHTTPENGINE_EXPORT bool operator==(const QIByteArray &a1, const QByteArray &a2);
QHTTPENGINE_EXPORT bool operator==(const QByteArray &a1, const QIByteArray &a2);
QHTTPENGINE_EXPORT bool operator==(const QIByteArray &a1, const char *a2);
QHTTPENGINE_EXPORT bool operator==(const char *a1, const QIByteArray &a2);
inline bool operator==(const QIByteArray &a1, const char *a2) { return a1.toLower() == QByteArray(a2).toLower(); }
inline bool operator==(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() == a2.toLower(); }
inline bool operator==(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() == a2.toLower(); }
inline bool operator==(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() == a2.toLower(); }
inline bool operator==(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() == a2.toLower(); }
inline bool operator!=(const QIByteArray &a1, const char *a2) { return a1.toLower() != QByteArray(a2).toLower(); }
inline bool operator!=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() != a2.toLower(); }
inline bool operator!=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() != a2.toLower(); }
inline bool operator!=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() != a2.toLower(); }
inline bool operator!=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() != a2.toLower(); }
inline bool operator<(const QIByteArray &a1, const char *a2) { return a1.toLower() < QByteArray(a2).toLower(); }
inline bool operator<(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() < a2.toLower(); }
inline bool operator<(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() < a2.toLower(); }
inline bool operator<(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() < a2.toLower(); }
inline bool operator<(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() < a2.toLower(); }
inline bool operator>(const QIByteArray &a1, const char *a2) { return a1.toLower() > QByteArray(a2).toLower(); }
inline bool operator>(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() > a2.toLower(); }
inline bool operator>(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() > a2.toLower(); }
inline bool operator>(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() > a2.toLower(); }
inline bool operator>(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() > a2.toLower(); }
inline bool operator<=(const QIByteArray &a1, const char *a2) { return a1.toLower() <= QByteArray(a2).toLower(); }
inline bool operator<=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() <= a2.toLower(); }
inline bool operator<=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() <= a2.toLower(); }
inline bool operator<=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() <= a2.toLower(); }
inline bool operator<=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() <= a2.toLower(); }
inline bool operator>=(const QIByteArray &a1, const char *a2) { return a1.toLower() >= QByteArray(a2).toLower(); }
inline bool operator>=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() >= a2.toLower(); }
inline bool operator>=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() >= a2.toLower(); }
inline bool operator>=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() >= a2.toLower(); }
inline bool operator>=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() >= a2.toLower(); }
#endif // QHTTPENGINE_QIBYTEARRAY_H

View File

@@ -10,7 +10,6 @@ set(SRC
qhttprange.cpp
qhttpserver.cpp
qhttpsocket.cpp
qibytearray.cpp
qiodevicecopier.cpp
qlocalfile.cpp
qobjecthandler.cpp

View File

@@ -1,71 +0,0 @@
/*
* Copyright (c) 2015 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QString>
#include "QHttpEngine/qibytearray.h"
QIByteArray::QIByteArray()
{}
QIByteArray::QIByteArray(const QByteArray &other)
: QByteArray(other)
{}
QIByteArray::QIByteArray(const char *data, int size)
: QByteArray(data, size)
{}
bool operator==(const QIByteArray &a1, const QIByteArray &a2)
{
return a1.toLower() == a2.toLower();
}
bool operator==(const QIByteArray &a1, const QString &a2)
{
return a1.toLower() == a2.toLower();
}
bool operator==(const QString &a1, const QIByteArray &a2)
{
return a2 == a1;
}
bool operator==(const QIByteArray &a1, const QByteArray &a2)
{
return a1.toLower() == a2.toLower();
}
bool operator==(const QByteArray &a1, const QIByteArray &a2)
{
return a2 == a1;
}
bool operator==(const QIByteArray &a1, const char *a2)
{
return a1.toLower() == QByteArray(a2).toLower();
}
bool operator==(const char *a1, const QIByteArray &a2)
{
return a2 == a1;
}

View File

@@ -28,33 +28,41 @@
const char *Value1 = "test";
const char *Value2 = "TEST";
// Helpful macros to cut down on the amount of duplicated code
#define TEST_OPERATOR(tn,t,on,o,v) void test##tn##on() \
{ \
QCOMPARE(QIByteArray(Value1) o static_cast<t>(Value2), v); \
QCOMPARE(static_cast<t>(Value1) o QIByteArray(Value1), v); \
}
#define TEST_TYPE(tn,t) \
TEST_OPERATOR(tn, t, Equals, ==, true) \
TEST_OPERATOR(tn, t, NotEquals, !=, false) \
TEST_OPERATOR(tn, t, Less, <, false) \
TEST_OPERATOR(tn, t, Greater, >, false) \
TEST_OPERATOR(tn, t, LessEqual, <=, true) \
TEST_OPERATOR(tn, t, GreaterEqual, >=, true)
class TestQIByteArray : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testQString();
void testQByteArray();
void testCharPtr();
TEST_TYPE(ConstChar, const char *)
TEST_TYPE(QByteArray, QByteArray)
TEST_TYPE(QIByteArray, QIByteArray)
TEST_TYPE(QString, QString)
void testContains();
};
void TestQIByteArray::testQString()
void TestQIByteArray::testContains()
{
QVERIFY(QIByteArray(Value1) == QString(Value2));
QVERIFY(QString(Value1) == QIByteArray(Value2));
}
QIByteArray v(Value1);
void TestQIByteArray::testQByteArray()
{
QVERIFY(QIByteArray(Value1) == QByteArray(Value2));
QVERIFY(QByteArray(Value1) == QIByteArray(Value2));
}
void TestQIByteArray::testCharPtr()
{
QVERIFY(QIByteArray(Value1) == Value2);
QVERIFY(Value1 == QIByteArray(Value2));
QVERIFY(v.contains('t'));
QVERIFY(v.contains(Value2));
QVERIFY(v.contains(QByteArray(Value2)));
}
QTEST_MAIN(TestQIByteArray)