1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "utils_global.h"
29 
30 #ifdef QT_GUI_LIB
31 #include <QEnterEvent>
32 #endif
33 #include <QString>
34 
35 namespace Utils {
36 
37 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
38 // Keep the support code for lower Qt versions for sdktool
39 constexpr QString::SplitBehavior SkipEmptyParts = QString::SkipEmptyParts;
40 #else
41 constexpr Qt::SplitBehaviorFlags SkipEmptyParts = Qt::SkipEmptyParts;
42 #endif
43 
44 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45 using QHashValueType = uint;
46 #else
47 using QHashValueType = size_t;
48 #endif
49 
50 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
51 using QtSizeType = int;
52 #else
53 using QtSizeType = qsizetype;
54 #endif
55 
56 // StringView - either QStringRef or QStringView
57 // Can be used where it is not possible to completely switch to QStringView
58 // For example where QString::splitRef / QStringView::split is essential.
59 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
60 using StringView = QStringRef;
61 #else
62 using StringView = QStringView;
63 #endif
64 
make_stringview(const QString & s)65 inline StringView make_stringview(const QString &s)
66 {
67 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
68     return QStringRef(&s);
69 #else
70     return QStringView(s);
71 #endif
72 }
73 
74 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
75 // QStringView::mid in Qt5 does not do bounds checking, in Qt6 it does
midView(const QString & s,int offset,int length)76 inline QStringView midView(const QString &s, int offset, int length)
77 {
78 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
79     const int size = s.size();
80     if (offset > size)
81         return {};
82     if (offset < 0) {
83         if (length < 0 || length + offset >= size)
84             return QStringView(s);
85         if (length + offset <= 0)
86             return {};
87         return QStringView(s).left(length + offset);
88     } else if (length > size - offset)
89         return QStringView(s).mid(offset);
90     return QStringView(s).mid(offset, length);
91 #else
92     return QStringView(s).mid(offset, length);
93 #endif
94 }
95 #endif
96 
97 #ifdef QT_GUI_LIB
98 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
99 using EnterEvent = QEvent;
100 #else
101 using EnterEvent = QEnterEvent;
102 #endif
103 #endif
104 
105 } // namespace Utils
106