Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4stringtoarrayindex_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QV4STRINGTOARRAYINDEX_P_H
6#define QV4STRINGTOARRAYINDEX_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qnumeric_p.h>
20#include <QtCore/qstring.h>
21#include <limits>
22
23QT_BEGIN_NAMESPACE
24
25namespace QV4 {
26
27inline uint charToUInt(const QChar *ch) { return ch->unicode(); }
28inline uint charToUInt(const char *ch) { return static_cast<unsigned char>(*ch); }
29
30template <typename T>
31uint stringToArrayIndex(const T *ch, const T *end)
32{
33 if (ch == end)
34 return std::numeric_limits<uint>::max();
35 uint i = charToUInt(ch) - '0';
36 if (i > 9)
37 return std::numeric_limits<uint>::max();
38 ++ch;
39 // reject "01", "001", ...
40 if (i == 0 && ch != end)
41 return std::numeric_limits<uint>::max();
42
43 while (ch < end) {
44 uint x = charToUInt(ch) - '0';
45 if (x > 9)
46 return std::numeric_limits<uint>::max();
47 if (qMulOverflow(i, uint(10), &i) || qAddOverflow(i, x, &i)) // i = i * 10 + x
48 return std::numeric_limits<uint>::max();
49 ++ch;
50 }
51 return i;
52}
53
54inline uint stringToArrayIndex(const QString &str)
55{
56 return stringToArrayIndex(str.constData(), str.constData() + str.size());
57}
58
59} // namespace QV4
60
61QT_END_NAMESPACE
62
63#endif // QV4STRINGTOARRAYINDEX_P_H
Definition qjsvalue.h:24
uint stringToArrayIndex(const QString &str)
uint stringToArrayIndex(const T *ch, const T *end)
uint charToUInt(const char *ch)
uint charToUInt(const QChar *ch)