• 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 <iomanip>
20 #include <iostream>
21 #include <optional>
22 #include <sstream>
23 #include <string>
24 #include <typeinfo>
25 #include "log.h"
26 namespace SysTuning {
27 namespace base {
28 enum IntegerRadixType { INTEGER_RADIX_TYPE_DEC = 10, INTEGER_RADIX_TYPE_HEX = 16 };
GetNameASCIISumNoNum(const std::string & str)29 inline uint16_t GetNameASCIISumNoNum(const std::string& str)
30 {
31     uint32_t sum = 0;
32     int32_t 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 
40 inline std::string number(uint64_t value, int32_t base = INTEGER_RADIX_TYPE_DEC)
41 {
42     std::stringstream ss;
43     if (base == INTEGER_RADIX_TYPE_DEC) {
44         ss << std::dec << value;
45     } else if (base == INTEGER_RADIX_TYPE_HEX) {
46         ss << std::hex << value;
47     }
48     return ss.str();
49 }
50 
ConvertTimestampToSecStr(uint64_t timestamp,uint8_t precision)51 inline std::string ConvertTimestampToSecStr(uint64_t timestamp, uint8_t precision)
52 {
53     double seconds = static_cast<double>(timestamp) / 1e9;
54     std::stringstream ss;
55     ss << std::fixed << std::setprecision(precision) << seconds;
56     return ss.str();
57 }
58 
59 template <typename T>
60 std::optional<T> StrToInt(const std::string& str, int32_t base = INTEGER_RADIX_TYPE_DEC)
61 {
62     if (!str.empty()) {
63         char* endPtr = nullptr;
64         T value;
65         if constexpr (std::is_same_v<T, uint32_t>) {
66             value = static_cast<T>(std::strtoul(str.c_str(), &endPtr, base));
67         } else if constexpr (std::is_same_v<T, int32_t>) {
68             value = static_cast<T>(std::strtol(str.c_str(), &endPtr, base));
69         } else if constexpr (std::is_same_v<T, uint64_t>) {
70             value = static_cast<T>(std::strtoull(str.c_str(), &endPtr, base));
71         } else if constexpr (std::is_same_v<T, int64_t>) {
72             value = static_cast<T>(std::strtoll(str.c_str(), &endPtr, base));
73         }
74         if (!*endPtr) {
75             return std::make_optional(value);
76         }
77     }
78     TS_LOGD("Illegal value: %s", str.c_str());
79     return std::nullopt;
80 }
81 
StrToDouble(const std::string & str)82 inline std::optional<double> StrToDouble(const std::string& str)
83 {
84     if (!str.empty()) {
85 #ifdef WIN32_
86         char* end = nullptr;
87         double value = std::strtod(str.c_str(), &end);
88 #else
89         double value = std::stod(str);
90 #endif
91         return std::make_optional(value);
92     }
93     return std::nullopt;
94 }
95 } // namespace base
96 } // namespace SysTuning
97 
98 #endif // INCLUDE_TUNING_EXT_BASE_STRING_UTILS_H_
99