1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_ASSEMBLER_UTILS_NUMBERS_UTILS_H
17 #define PANDA_ASSEMBLER_UTILS_NUMBERS_UTILS_H
18
19 #include "libpandabase/utils/bit_utils.h"
20
21 namespace ark::pandasm {
22
23 constexpr size_t HEX_BASE = 16;
24
25 constexpr size_t DEC_BASE = 10;
26
27 constexpr size_t OCT_BASE = 8;
28
29 constexpr size_t BIN_BASE = 2;
30
31 constexpr size_t MAX_DWORD = 65536;
32
ValidateXToken(std::string_view token,size_t shift)33 inline bool ValidateXToken(std::string_view token, size_t shift)
34 {
35 token.remove_prefix(shift);
36
37 for (auto i : token) {
38 if (!((i >= '0' && i <= '9') || (i >= 'A' && i <= 'F') || (i >= 'a' && i <= 'f'))) {
39 return false;
40 }
41 }
42
43 return true;
44 }
45
ValidateBToken(std::string_view token,size_t shift)46 inline bool ValidateBToken(std::string_view token, size_t shift)
47 {
48 token.remove_prefix(shift);
49 if (token.empty()) {
50 return false;
51 }
52 for (auto i : token) {
53 if (!(i == '0' || i == '1')) {
54 return false;
55 }
56 }
57
58 return true;
59 }
60
ValidateZeroToTenToken(std::string_view token)61 inline bool ValidateZeroToTenToken(std::string_view token)
62 {
63 token.remove_prefix(1);
64
65 for (auto i : token) {
66 if (!(i >= '0' && i <= '7')) {
67 return false;
68 }
69 }
70
71 return true;
72 }
73
74 bool ValidateInteger(std::string_view p);
75
76 int64_t IntegerNumber(std::string_view p);
77
78 bool ValidateFloat(std::string_view p);
79
FloatNumber(std::string_view p,bool is64bit)80 inline double FloatNumber(std::string_view p, bool is64bit)
81 {
82 constexpr size_t GENERAL_SHIFT = 2;
83 // expects a valid number
84 if (p.size() > GENERAL_SHIFT && p.substr(0, GENERAL_SHIFT) == "0x") { // hex literal
85 char *end = nullptr;
86 if (is64bit) {
87 return bit_cast<double>(strtoull(p.data(), &end, 0));
88 }
89 // CC-OFFNXT(G.FUU.01) false positive
90 return bit_cast<float>(static_cast<uint32_t>(strtoull(p.data(), &end, 0)));
91 }
92 return std::strtold(std::string(p.data(), p.length()).c_str(), nullptr);
93 }
94
ToNumber(std::string_view p)95 inline size_t ToNumber(std::string_view p)
96 {
97 size_t sum = 0;
98
99 for (char i : p) {
100 if (isdigit(i) != 0) {
101 sum = sum * DEC_BASE + static_cast<size_t>(i - '0');
102 } else {
103 return MAX_DWORD;
104 }
105 }
106
107 return sum;
108 }
109
110 } // namespace ark::pandasm
111
112 #endif // PANDA_ASSEMBLER_UTILS_NUMBERS_UTILS_H
113