| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTOOLS_P_H |
| 5 | #define QTOOLS_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "QtCore/private/qglobal_p.h" |
| 19 | |
| 20 | #include <chrono> |
| 21 | #include <limits.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | namespace QtMiscUtils { |
| 27 | constexpr inline char toHexUpper(char32_t value) noexcept |
| 28 | { |
| 29 | return "0123456789ABCDEF" [value & 0xF]; |
| 30 | } |
| 31 | |
| 32 | constexpr inline char toHexLower(char32_t value) noexcept |
| 33 | { |
| 34 | return "0123456789abcdef" [value & 0xF]; |
| 35 | } |
| 36 | |
| 37 | [[nodiscard]] constexpr inline bool isHexDigit(char32_t c) noexcept |
| 38 | { |
| 39 | return (c >= '0' && c <= '9') |
| 40 | || (c >= 'A' && c <= 'F') |
| 41 | || (c >= 'a' && c <= 'f'); |
| 42 | } |
| 43 | |
| 44 | constexpr inline int fromHex(char32_t c) noexcept |
| 45 | { |
| 46 | return ((c >= '0') && (c <= '9')) ? int(c - '0') : |
| 47 | ((c >= 'A') && (c <= 'F')) ? int(c - 'A' + 10) : |
| 48 | ((c >= 'a') && (c <= 'f')) ? int(c - 'a' + 10) : |
| 49 | /* otherwise */ -1; |
| 50 | } |
| 51 | |
| 52 | constexpr inline char toOct(char32_t value) noexcept |
| 53 | { |
| 54 | return char('0' + (value & 0x7)); |
| 55 | } |
| 56 | |
| 57 | [[nodiscard]] constexpr inline bool isOctalDigit(char32_t c) noexcept |
| 58 | { |
| 59 | return c >= '0' && c <= '7'; |
| 60 | } |
| 61 | |
| 62 | constexpr inline int fromOct(char32_t c) noexcept |
| 63 | { |
| 64 | return isOctalDigit(c) ? int(c - '0') : -1; |
| 65 | } |
| 66 | |
| 67 | [[nodiscard]] constexpr inline bool isAsciiDigit(char32_t c) noexcept |
| 68 | { |
| 69 | return c >= '0' && c <= '9'; |
| 70 | } |
| 71 | |
| 72 | constexpr inline bool isAsciiUpper(char32_t c) noexcept |
| 73 | { |
| 74 | return c >= 'A' && c <= 'Z'; |
| 75 | } |
| 76 | |
| 77 | constexpr inline bool isAsciiLower(char32_t c) noexcept |
| 78 | { |
| 79 | return c >= 'a' && c <= 'z'; |
| 80 | } |
| 81 | |
| 82 | constexpr inline bool isAsciiLetterOrNumber(char32_t c) noexcept |
| 83 | { |
| 84 | return isAsciiDigit(c) || isAsciiLower(c) || isAsciiUpper(c); |
| 85 | } |
| 86 | |
| 87 | constexpr inline char toAsciiLower(char ch) noexcept |
| 88 | { |
| 89 | return isAsciiUpper(c: ch) ? ch - 'A' + 'a' : ch; |
| 90 | } |
| 91 | |
| 92 | constexpr inline char toAsciiUpper(char ch) noexcept |
| 93 | { |
| 94 | return isAsciiLower(c: ch) ? ch - 'a' + 'A' : ch; |
| 95 | } |
| 96 | |
| 97 | constexpr inline int caseCompareAscii(char lhs, char rhs) noexcept |
| 98 | { |
| 99 | const char lhsLower = QtMiscUtils::toAsciiLower(ch: lhs); |
| 100 | const char rhsLower = QtMiscUtils::toAsciiLower(ch: rhs); |
| 101 | return int(uchar(lhsLower)) - int(uchar(rhsLower)); |
| 102 | } |
| 103 | |
| 104 | constexpr inline int isAsciiPrintable(char32_t ch) noexcept |
| 105 | { |
| 106 | return ch >= ' ' && ch < 0x7f; |
| 107 | } |
| 108 | |
| 109 | constexpr inline int qt_lencmp(qsizetype lhs, qsizetype rhs) noexcept |
| 110 | { |
| 111 | return lhs == rhs ? 0 : |
| 112 | lhs > rhs ? 1 : |
| 113 | /* else */ -1 ; |
| 114 | } |
| 115 | |
| 116 | } // namespace QtMiscUtils |
| 117 | |
| 118 | struct CalculateGrowingBlockSizeResult |
| 119 | { |
| 120 | qsizetype size; |
| 121 | qsizetype elementCount; |
| 122 | }; |
| 123 | |
| 124 | // Implemented in qarraydata.cpp: |
| 125 | qsizetype Q_CORE_EXPORT Q_DECL_CONST_FUNCTION |
| 126 | qCalculateBlockSize(qsizetype elementCount, qsizetype elementSize, qsizetype = 0) noexcept; |
| 127 | CalculateGrowingBlockSizeResult Q_CORE_EXPORT Q_DECL_CONST_FUNCTION |
| 128 | qCalculateGrowingBlockSize(qsizetype elementCount, qsizetype elementSize, qsizetype = 0) noexcept ; |
| 129 | |
| 130 | QT_END_NAMESPACE |
| 131 | |
| 132 | #endif // QTOOLS_P_H |
| 133 | |