| 1 |
#ifndef QT_UTILITIES_COMPAT_H |
| 2 |
#define QT_UTILITIES_COMPAT_H |
| 3 |
|
| 4 |
#include "../global.h" |
| 5 |
|
| 6 |
#include <c++utilities/misc/traits.h> |
| 7 |
|
| 8 |
#include <QtGlobal> |
| 9 |
|
| 10 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 11 |
#define QT_UTILITIES_USE_Q_STRING_VIEW |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifdef QT_UTILITIES_USE_Q_STRING_VIEW |
| 17 |
#include <QStringView> |
| 18 |
#else |
| 19 |
#include <QStringRef> |
| 20 |
#endif |
| 21 |
|
| 22 |
namespace QtUtilities { |
| 23 |
|
| 24 |
using Utf16CharType = |
| 25 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 26 |
char16_t |
| 27 |
#else |
| 28 |
ushort |
| 29 |
#endif |
| 30 |
; |
| 31 |
|
| 32 |
using StringView = |
| 33 |
#ifdef QT_UTILITIES_USE_Q_STRING_VIEW |
| 34 |
QStringView |
| 35 |
#else |
| 36 |
QStringRef |
| 37 |
#endif |
| 38 |
; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
inline StringView makeStringView(const QString &str) |
| 44 |
{ |
| 45 |
return StringView( |
| 46 |
#ifndef QT_UTILITIES_USE_Q_STRING_VIEW |
| 47 |
& |
| 48 |
#endif |
| 49 |
str); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
template <typename PosType1, typename PosType2 = PosType1, |
| 56 |
CppUtilities::Traits::EnableIf<std::is_integral<PosType1>, std::is_signed<PosType1>, std::is_integral<PosType2>, std::is_signed<PosType2>> |
| 57 |
* = nullptr> |
| 58 |
inline StringView midRef(const QString &str, PosType1 pos, PosType2 n = -1) |
| 59 |
{ |
| 60 |
#ifdef QT_UTILITIES_USE_Q_STRING_VIEW |
| 61 |
return QStringView(str).mid(pos, n); |
| 62 |
#else |
| 63 |
return str.midRef(pos, n); |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template <class... SplitArgs> inline auto splitRef(const QString &str, SplitArgs &&...args) |
| 71 |
{ |
| 72 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 73 |
return QStringView(str).split(std::forward<SplitArgs>(args)...); |
| 74 |
#elif QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) |
| 75 |
return str.splitRef(std::forward<SplitArgs>(args)...); |
| 76 |
#else |
| 77 |
return str.split(std::forward<SplitArgs>(args)...); |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) |
| 84 |
QT_BEGIN_NAMESPACE |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
inline QString qEnvironmentVariable(const char *varName, const QString &defaultValue) |
| 89 |
{ |
| 90 |
const auto val = qgetenv(varName); |
| 91 |
return !val.isEmpty() ? QString::fromLocal8Bit(val) : defaultValue; |
| 92 |
} |
| 93 |
QT_END_NAMESPACE |
| 94 |
#endif |
| 95 |
|
| 96 |
#endif |