diff options
Diffstat (limited to 'src/corelib/text/qchar.h')
-rw-r--r-- | src/corelib/text/qchar.h | 156 |
1 files changed, 80 insertions, 76 deletions
diff --git a/src/corelib/text/qchar.h b/src/corelib/text/qchar.h index e9f0cf3346..b3fd794726 100644 --- a/src/corelib/text/qchar.h +++ b/src/corelib/text/qchar.h @@ -5,6 +5,7 @@ #define QCHAR_H #include <QtCore/qglobal.h> +#include <QtCore/qcompare.h> #include <functional> // for std::hash @@ -20,32 +21,27 @@ public: constexpr inline char toLatin1() const noexcept { return ch; } constexpr inline char16_t unicode() const noexcept { return char16_t(uchar(ch)); } - friend constexpr inline bool operator==(QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch == rhs.ch; } - friend constexpr inline bool operator!=(QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch != rhs.ch; } - friend constexpr inline bool operator<=(QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch <= rhs.ch; } - friend constexpr inline bool operator>=(QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch >= rhs.ch; } - friend constexpr inline bool operator< (QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch < rhs.ch; } - friend constexpr inline bool operator> (QLatin1Char lhs, QLatin1Char rhs) noexcept { return lhs.ch > rhs.ch; } - - friend constexpr inline bool operator==(char lhs, QLatin1Char rhs) noexcept { return lhs == rhs.toLatin1(); } - friend constexpr inline bool operator!=(char lhs, QLatin1Char rhs) noexcept { return lhs != rhs.toLatin1(); } - friend constexpr inline bool operator<=(char lhs, QLatin1Char rhs) noexcept { return lhs <= rhs.toLatin1(); } - friend constexpr inline bool operator>=(char lhs, QLatin1Char rhs) noexcept { return lhs >= rhs.toLatin1(); } - friend constexpr inline bool operator< (char lhs, QLatin1Char rhs) noexcept { return lhs < rhs.toLatin1(); } - friend constexpr inline bool operator> (char lhs, QLatin1Char rhs) noexcept { return lhs > rhs.toLatin1(); } - - friend constexpr inline bool operator==(QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() == rhs; } - friend constexpr inline bool operator!=(QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() != rhs; } - friend constexpr inline bool operator<=(QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() <= rhs; } - friend constexpr inline bool operator>=(QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() >= rhs; } - friend constexpr inline bool operator< (QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() < rhs; } - friend constexpr inline bool operator> (QLatin1Char lhs, char rhs) noexcept { return lhs.toLatin1() > rhs; } + friend constexpr bool + comparesEqual(const QLatin1Char &lhs, const QLatin1Char &rhs) noexcept + { return lhs.ch == rhs.ch; } + friend constexpr Qt::strong_ordering + compareThreeWay(const QLatin1Char &lhs, const QLatin1Char &rhs) noexcept + { return Qt::compareThreeWay(uchar(lhs.ch), uchar(rhs.ch)); } + Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QLatin1Char) + + friend constexpr bool comparesEqual(const QLatin1Char &lhs, char rhs) noexcept + { return lhs.toLatin1() == rhs; } + friend constexpr Qt::strong_ordering + compareThreeWay(const QLatin1Char &lhs, char rhs) noexcept + { return Qt::compareThreeWay(uchar(lhs.toLatin1()), uchar(rhs)); } + Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QLatin1Char, char) private: char ch; }; -class Q_CORE_EXPORT QChar { +#define QT_CHAR_FASTCALL QT7_ONLY(Q_CORE_EXPORT) QT_FASTCALL +class QT6_ONLY(Q_CORE_EXPORT) QChar { public: enum SpecialCharacter { Null = 0x0000, @@ -67,17 +63,15 @@ public: }; #ifdef QT_IMPLICIT_QCHAR_CONSTRUCTION -#define QCHAR_MAYBE_IMPLICIT Q_IMPLICIT -#else -#define QCHAR_MAYBE_IMPLICIT explicit +#error This macro has been removed in Qt 6.8. #endif constexpr Q_IMPLICIT QChar() noexcept : ucs(0) {} constexpr Q_IMPLICIT QChar(ushort rc) noexcept : ucs(rc) {} - constexpr QCHAR_MAYBE_IMPLICIT QChar(uchar c, uchar r) noexcept : ucs(char16_t((r << 8) | c)) {} + constexpr explicit QChar(uchar c, uchar r) noexcept : ucs(char16_t((r << 8) | c)) {} constexpr Q_IMPLICIT QChar(short rc) noexcept : ucs(char16_t(rc)) {} - constexpr QCHAR_MAYBE_IMPLICIT QChar(uint rc) noexcept : ucs((Q_ASSERT(rc <= 0xffff), char16_t(rc))) {} - constexpr QCHAR_MAYBE_IMPLICIT QChar(int rc) noexcept : QChar(uint(rc)) {} + constexpr explicit QChar(uint rc) noexcept : ucs((Q_ASSERT(rc <= 0xffff), char16_t(rc))) {} + constexpr explicit QChar(int rc) noexcept : QChar(uint(rc)) {} constexpr Q_IMPLICIT QChar(SpecialCharacter s) noexcept : ucs(char16_t(s)) {} constexpr Q_IMPLICIT QChar(QLatin1Char ch) noexcept : ucs(ch.unicode()) {} constexpr Q_IMPLICIT QChar(char16_t ch) noexcept : ucs(ch) {} @@ -89,12 +83,10 @@ public: // Always implicit -- allow for 'x' => QChar conversions QT_ASCII_CAST_WARN constexpr Q_IMPLICIT QChar(char c) noexcept : ucs(uchar(c)) { } #ifndef QT_RESTRICTED_CAST_FROM_ASCII - QT_ASCII_CAST_WARN constexpr QCHAR_MAYBE_IMPLICIT QChar(uchar c) noexcept : ucs(c) { } + QT_ASCII_CAST_WARN constexpr explicit QChar(uchar c) noexcept : ucs(c) { } #endif #endif -#undef QCHAR_MAYBE_IMPLICIT - static constexpr QChar fromUcs2(char16_t c) noexcept { return QChar{c}; } static constexpr inline auto fromUcs4(char32_t c) noexcept; @@ -431,6 +423,7 @@ public: Unicode_13_0, Unicode_14_0, Unicode_15_0, + Unicode_15_1, }; inline Category category() const noexcept { return QChar::category(ucs); } @@ -524,39 +517,39 @@ public: return char16_t(ucs4%0x400 + 0xdc00); } - static Category QT_FASTCALL category(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static Direction QT_FASTCALL direction(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static JoiningType QT_FASTCALL joiningType(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static unsigned char QT_FASTCALL combiningClass(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static Category QT_CHAR_FASTCALL category(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static Direction QT_CHAR_FASTCALL direction(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static JoiningType QT_CHAR_FASTCALL joiningType(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static unsigned char QT_CHAR_FASTCALL combiningClass(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static char32_t QT_FASTCALL mirroredChar(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL hasMirrored(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static char32_t QT_CHAR_FASTCALL mirroredChar(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL hasMirrored(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static QString QT_FASTCALL decomposition(char32_t ucs4); - static Decomposition QT_FASTCALL decompositionTag(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static QString QT_CHAR_FASTCALL decomposition(char32_t ucs4); + static Decomposition QT_CHAR_FASTCALL decompositionTag(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static int QT_FASTCALL digitValue(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static char32_t QT_FASTCALL toLower(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static char32_t QT_FASTCALL toUpper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static char32_t QT_FASTCALL toTitleCase(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static char32_t QT_FASTCALL toCaseFolded(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static int QT_CHAR_FASTCALL digitValue(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static char32_t QT_CHAR_FASTCALL toLower(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static char32_t QT_CHAR_FASTCALL toUpper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static char32_t QT_CHAR_FASTCALL toTitleCase(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static char32_t QT_CHAR_FASTCALL toCaseFolded(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static Script QT_FASTCALL script(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static Script QT_CHAR_FASTCALL script(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static UnicodeVersion QT_FASTCALL unicodeVersion(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static UnicodeVersion QT_CHAR_FASTCALL unicodeVersion(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static UnicodeVersion QT_FASTCALL currentUnicodeVersion() noexcept Q_DECL_CONST_FUNCTION; + static UnicodeVersion QT_CHAR_FASTCALL currentUnicodeVersion() noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isPrint(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isPrint(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; static constexpr inline bool isSpace(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION { // note that [0x09..0x0d] + 0x85 are exceptional Cc-s and must be handled explicitly return ucs4 == 0x20 || (ucs4 <= 0x0d && ucs4 >= 0x09) || (ucs4 > 127 && (ucs4 == 0x85 || ucs4 == 0xa0 || QChar::isSpace_helper(ucs4))); } - static bool QT_FASTCALL isMark(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isPunct(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isSymbol(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isMark(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isPunct(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isSymbol(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; static constexpr inline bool isLetter(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION { return (ucs4 >= 'A' && ucs4 <= 'z' && (ucs4 >= 'a' || ucs4 <= 'Z')) @@ -579,34 +572,44 @@ public: static constexpr inline bool isTitleCase(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION { return ucs4 > 127 && QChar::category(ucs4) == Letter_Titlecase; } - friend constexpr inline bool operator==(QChar c1, QChar c2) noexcept { return c1.ucs == c2.ucs; } - friend constexpr inline bool operator< (QChar c1, QChar c2) noexcept { return c1.ucs < c2.ucs; } - - friend constexpr inline bool operator!=(QChar c1, QChar c2) noexcept { return !operator==(c1, c2); } - friend constexpr inline bool operator>=(QChar c1, QChar c2) noexcept { return !operator< (c1, c2); } - friend constexpr inline bool operator> (QChar c1, QChar c2) noexcept { return operator< (c2, c1); } - friend constexpr inline bool operator<=(QChar c1, QChar c2) noexcept { return !operator< (c2, c1); } - - friend constexpr inline bool operator==(QChar lhs, std::nullptr_t) noexcept { return lhs.isNull(); } - friend constexpr inline bool operator< (QChar, std::nullptr_t) noexcept { return false; } - friend constexpr inline bool operator==(std::nullptr_t, QChar rhs) noexcept { return rhs.isNull(); } - friend constexpr inline bool operator< (std::nullptr_t, QChar rhs) noexcept { return !rhs.isNull(); } + friend constexpr bool comparesEqual(const QChar &lhs, const QChar &rhs) noexcept + { return lhs.ucs == rhs.ucs; } + friend constexpr Qt::strong_ordering + compareThreeWay(const QChar &lhs, const QChar &rhs) noexcept + { return Qt::compareThreeWay(lhs.ucs, rhs.ucs); } + Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QChar) - friend constexpr inline bool operator!=(QChar lhs, std::nullptr_t) noexcept { return !operator==(lhs, nullptr); } - friend constexpr inline bool operator>=(QChar lhs, std::nullptr_t) noexcept { return !operator< (lhs, nullptr); } - friend constexpr inline bool operator> (QChar lhs, std::nullptr_t) noexcept { return operator< (nullptr, lhs); } - friend constexpr inline bool operator<=(QChar lhs, std::nullptr_t) noexcept { return !operator< (nullptr, lhs); } - - friend constexpr inline bool operator!=(std::nullptr_t, QChar rhs) noexcept { return !operator==(nullptr, rhs); } - friend constexpr inline bool operator>=(std::nullptr_t, QChar rhs) noexcept { return !operator< (nullptr, rhs); } - friend constexpr inline bool operator> (std::nullptr_t, QChar rhs) noexcept { return operator< (rhs, nullptr); } - friend constexpr inline bool operator<=(std::nullptr_t, QChar rhs) noexcept { return !operator< (rhs, nullptr); } + friend constexpr bool comparesEqual(const QChar &lhs, std::nullptr_t) noexcept + { return lhs.isNull(); } + friend constexpr Qt::strong_ordering + compareThreeWay(const QChar &lhs, std::nullptr_t) noexcept + { return lhs.isNull() ? Qt::strong_ordering::equivalent : Qt::strong_ordering::greater; } + Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QChar, std::nullptr_t) private: - static bool QT_FASTCALL isSpace_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isLetter_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isNumber_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; - static bool QT_FASTCALL isLetterOrNumber_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isSpace_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isLetter_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isNumber_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + static bool QT_CHAR_FASTCALL isLetterOrNumber_helper(char32_t ucs4) noexcept Q_DECL_CONST_FUNCTION; + + // defined in qstring.cpp, because we need to go via QUtf8StringView + static bool QT_CHAR_FASTCALL + equal_helper(QChar lhs, const char *rhs) noexcept Q_DECL_CONST_FUNCTION; + static int QT_CHAR_FASTCALL + compare_helper(QChar lhs, const char *rhs) noexcept Q_DECL_CONST_FUNCTION; + +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + Q_WEAK_OVERLOAD + friend bool comparesEqual(const QChar &lhs, const char *rhs) noexcept + { return equal_helper(lhs, rhs); } + Q_WEAK_OVERLOAD + friend Qt::strong_ordering compareThreeWay(const QChar &lhs, const char *rhs) noexcept + { + const int res = compare_helper(lhs, rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QChar, const char *, Q_WEAK_OVERLOAD QT_ASCII_CAST_WARN) +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) #ifdef QT_NO_CAST_FROM_ASCII QChar(char c) = delete; @@ -615,6 +618,7 @@ private: char16_t ucs; }; +#undef QT_CHAR_FASTCALL Q_DECLARE_TYPEINFO(QChar, Q_PRIMITIVE_TYPE); @@ -627,7 +631,7 @@ namespace Qt { inline namespace Literals { inline namespace StringLiterals { -constexpr inline QLatin1Char operator"" _L1(char ch) noexcept +constexpr inline QLatin1Char operator""_L1(char ch) noexcept { return QLatin1Char(ch); } |