• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 INCLUDE_BASE_STRING_TO_NUMERICAL_H_
17 #define INCLUDE_BASE_STRING_TO_NUMERICAL_H_
18 
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 
23 namespace SysTuning {
24 namespace base {
25 enum IntegerRadixType {
26     INTEGER_RADIX_TYPE_DEC = 10,
27     INTEGER_RADIX_TYPE_HEX = 16
28 };
GetNameASCIISumNoNum(const std::string & str)29 inline uint16_t GetNameASCIISumNoNum(const std::string& str)
30 {
31     uint32_t sum = 0;
32     int len = str.length() - 1;
33     while (len >= 0) {
34         sum += std::isdigit(str.at(len)) ? 0 : str.at(len);
35         len--;
36     }
37     return sum % INTEGER_RADIX_TYPE_HEX;
38 }
39 inline std::optional<uint32_t> StrToUInt32(const std::string& str, int base = INTEGER_RADIX_TYPE_DEC)
40 {
41     if (!str.empty()) {
42         char* endPtr = nullptr;
43         auto value = static_cast<uint32_t>(std::strtoul(str.c_str(), &endPtr, base));
44         if (!*endPtr) {
45             return std::make_optional(value);
46         }
47     }
48 
49     return std::nullopt;
50 }
51 
52 inline std::string number(uint64_t value, int base = INTEGER_RADIX_TYPE_DEC)
53 {
54     std::stringstream ss;
55     if (base == INTEGER_RADIX_TYPE_DEC) {
56         ss << std::dec << value;
57     } else if (base == INTEGER_RADIX_TYPE_HEX) {
58         ss << std::hex << value;
59     }
60     return ss.str();
61 }
62 
63 inline std::optional<int32_t> StrToInt32(const std::string& str, int base = INTEGER_RADIX_TYPE_DEC)
64 {
65     if (!str.empty()) {
66         char* endPtr = nullptr;
67         auto value = static_cast<int32_t>(std::strtol(str.c_str(), &endPtr, base));
68         if (!*endPtr) {
69             return std::make_optional(value);
70         }
71     }
72 
73     return std::nullopt;
74 }
75 
76 inline std::optional<uint64_t> StrToUInt64(const std::string& str, int base = INTEGER_RADIX_TYPE_DEC)
77 {
78     if (!str.empty()) {
79         char* endPtr = nullptr;
80         auto value = static_cast<uint64_t>(std::strtoull(str.c_str(), &endPtr, base));
81         if (!*endPtr) {
82             return std::make_optional(value);
83         }
84     }
85 
86     return std::nullopt;
87 }
88 
89 inline std::optional<int64_t> StrToInt64(const std::string& str, int base = INTEGER_RADIX_TYPE_DEC)
90 {
91     if (!str.empty()) {
92         char* endPtr = nullptr;
93         int64_t value = static_cast<int64_t>(std::strtoll(str.c_str(), &endPtr, base));
94         if (!*endPtr) {
95             return std::make_optional(value);
96         }
97     }
98     return std::nullopt;
99 }
100 
StrToDouble(const std::string & str)101 inline std::optional<double> StrToDouble(const std::string& str)
102 {
103     if (!str.empty()) {
104         double value = std::stod(str);
105         return std::make_optional(value);
106     }
107 
108     return std::nullopt;
109 }
110 } // namespace base
111 } // namespace SysTuning
112 
113 #endif // INCLUDE_TUNING_EXT_BASE_STRING_UTILS_H_
114