• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIBPANDABASE_UTILS_UTILS_H
17 #define PANDA_LIBPANDABASE_UTILS_UTILS_H
18 
19 #include "libpandabase/macros.h"
20 
21 #include <limits>
22 
23 namespace ark {
24 // ----------------------------------------------------------------------------
25 // User-defined suffixes
26 constexpr int operator""_I(unsigned long long v)  // NOLINT(google-runtime-int)
27 {
28     if (v > static_cast<unsigned long long>(std::numeric_limits<int>::max())) {  // NOLINT(google-runtime-int)
29         UNREACHABLE_CONSTEXPR();
30     }
31     return static_cast<int>(v);
32 }
33 
34 constexpr double operator""_D(long double v)
35 {
36     if (v < static_cast<long double>(std::numeric_limits<double>::lowest()) ||
37         v > static_cast<long double>(std::numeric_limits<double>::max())) {
38         UNREACHABLE_CONSTEXPR();
39     }
40     return static_cast<double>(v);
41 }
42 
43 // General helper functions
44 
45 // Returns the value (0 .. 15) of a hexadecimal character c.
46 // If c is not a legal hexadecimal character, returns a value < 0.
47 // CC-OFFNXT(G.FUD.06) solid logic
HexValue(uint32_t c)48 inline uint32_t HexValue(uint32_t c)
49 {
50     constexpr uint32_t BASE16 = 16;
51     constexpr uint32_t BASE10 = 10;
52     constexpr uint32_t MASK = 0x20;
53 
54     c -= '0';
55     if (static_cast<unsigned>(c) < BASE10) {
56         return c;
57     }
58     // NOLINTNEXTLINE(hicpp-signed-bitwise)
59     c = (c | MASK) - ('a' - '0');
60     if (static_cast<unsigned>(c) < (BASE16 - BASE10)) {
61         return c + BASE10;
62     }
63     return -1;
64 }
65 
66 PANDA_PUBLIC_API uint32_t CountDigits(uint64_t v);
67 
68 }  // namespace ark
69 #endif  // PANDA_LIBPANDABASE_UTILS_UTILS_H
70