1 /*
2  * Copyright (c) 2017 Nathan Osman
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #ifndef QHTTPENGINE_IBYTEARRAY_H
24 #define QHTTPENGINE_IBYTEARRAY_H
25 
26 #include <cctype>
27 
28 #include <QByteArray>
29 
30 #include "qhttpengine_export.h"
31 
32 namespace QHttpEngine
33 {
34 
35 /**
36  * @brief Case-insensitive subclass of QByteArray
37  *
38  * The IByteArray is identical to the QByteArray class in all aspects except
39  * that it performs comparisons in a case-insensitive manner.
40  */
41 class QHTTPENGINE_EXPORT IByteArray : public QByteArray
42 {
43 public:
44 
45     /// \{
IByteArray()46     IByteArray() {}
IByteArray(const QByteArray & other)47     IByteArray(const QByteArray &other) : QByteArray(other) {}
IByteArray(const IByteArray & other)48     IByteArray(const IByteArray &other) : QByteArray(other) {}
QByteArray(data,size)49     IByteArray(const char *data, int size = -1) : QByteArray(data, size) {}
50 
51     inline bool operator==(const QString &s2) const { return toLower() == s2.toLower(); }
52     inline bool operator!=(const QString &s2) const { return toLower() != s2.toLower(); }
53     inline bool operator<(const QString &s2) const { return toLower() < s2.toLower(); }
54     inline bool operator>(const QString &s2) const { return toLower() > s2.toLower(); }
55     inline bool operator<=(const QString &s2) const { return toLower() <= s2.toLower(); }
56     inline bool operator>=(const QString &s2) const { return toLower() >= s2.toLower(); }
57 
contains(char c)58     bool contains(char c) const { return toLower().contains(tolower(c)); }
contains(const char * c)59     bool contains(const char *c) const { return toLower().contains(QByteArray(c).toLower()); }
contains(const QByteArray & a)60     bool contains(const QByteArray &a) const { return toLower().contains(a.toLower()); }
61     /// \}
62 };
63 
64 inline bool operator==(const IByteArray &a1, const char *a2) { return a1.toLower() == QByteArray(a2).toLower(); }
65 inline bool operator==(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() == a2.toLower(); }
66 inline bool operator==(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() == a2.toLower(); }
67 inline bool operator==(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() == a2.toLower(); }
68 inline bool operator==(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() == a2.toLower(); }
69 
70 inline bool operator!=(const IByteArray &a1, const char *a2) { return a1.toLower() != QByteArray(a2).toLower(); }
71 inline bool operator!=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() != a2.toLower(); }
72 inline bool operator!=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() != a2.toLower(); }
73 inline bool operator!=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() != a2.toLower(); }
74 inline bool operator!=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() != a2.toLower(); }
75 
76 inline bool operator<(const IByteArray &a1, const char *a2) { return a1.toLower() < QByteArray(a2).toLower(); }
77 inline bool operator<(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() < a2.toLower(); }
78 inline bool operator<(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() < a2.toLower(); }
79 inline bool operator<(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() < a2.toLower(); }
80 inline bool operator<(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() < a2.toLower(); }
81 
82 inline bool operator>(const IByteArray &a1, const char *a2) { return a1.toLower() > QByteArray(a2).toLower(); }
83 inline bool operator>(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() > a2.toLower(); }
84 inline bool operator>(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() > a2.toLower(); }
85 inline bool operator>(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() > a2.toLower(); }
86 inline bool operator>(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() > a2.toLower(); }
87 
88 inline bool operator<=(const IByteArray &a1, const char *a2) { return a1.toLower() <= QByteArray(a2).toLower(); }
89 inline bool operator<=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() <= a2.toLower(); }
90 inline bool operator<=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() <= a2.toLower(); }
91 inline bool operator<=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() <= a2.toLower(); }
92 inline bool operator<=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() <= a2.toLower(); }
93 
94 inline bool operator>=(const IByteArray &a1, const char *a2) { return a1.toLower() >= QByteArray(a2).toLower(); }
95 inline bool operator>=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() >= a2.toLower(); }
96 inline bool operator>=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() >= a2.toLower(); }
97 inline bool operator>=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() >= a2.toLower(); }
98 inline bool operator>=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() >= a2.toLower(); }
99 
100 }
101 
102 #endif // QHTTPENGINE_IBYTEARRAY_H
103