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
24QT_BEGIN_NAMESPACE
25
26namespace QtMiscUtils {
27constexpr inline char toHexUpper(char32_t value) noexcept
28{
29 return "0123456789ABCDEF"[value & 0xF];
30}
31
32constexpr 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
44constexpr 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
52constexpr 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
62constexpr 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
72constexpr inline bool isAsciiUpper(char32_t c) noexcept
73{
74 return c >= 'A' && c <= 'Z';
75}
76
77constexpr inline bool isAsciiLower(char32_t c) noexcept
78{
79 return c >= 'a' && c <= 'z';
80}
81
82constexpr inline bool isAsciiLetterOrNumber(char32_t c) noexcept
83{
84 return isAsciiDigit(c) || isAsciiLower(c) || isAsciiUpper(c);
85}
86
87constexpr inline char toAsciiLower(char ch) noexcept
88{
89 return isAsciiUpper(c: ch) ? ch - 'A' + 'a' : ch;
90}
91
92constexpr inline char toAsciiUpper(char ch) noexcept
93{
94 return isAsciiLower(c: ch) ? ch - 'a' + 'A' : ch;
95}
96
97constexpr 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
104constexpr inline int isAsciiPrintable(char32_t ch) noexcept
105{
106 return ch >= ' ' && ch < 0x7f;
107}
108
109constexpr 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// We typically need an extra bit for qNextPowerOfTwo when determining the next allocation size.
119constexpr qsizetype MaxAllocSize = (std::numeric_limits<qsizetype>::max)();
120
121struct CalculateGrowingBlockSizeResult
122{
123 qsizetype size;
124 qsizetype elementCount;
125};
126
127// Implemented in qarraydata.cpp:
128qsizetype Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
129qCalculateBlockSize(qsizetype elementCount, qsizetype elementSize, qsizetype headerSize = 0) noexcept;
130CalculateGrowingBlockSizeResult Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
131qCalculateGrowingBlockSize(qsizetype elementCount, qsizetype elementSize, qsizetype headerSize = 0) noexcept ;
132
133QT_END_NAMESPACE
134
135#endif // QTOOLS_P_H
136

source code of qtbase/src/corelib/tools/qtools_p.h