/* * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INCLUDE_BASE_STRING_TO_NUMERICAL_H #define INCLUDE_BASE_STRING_TO_NUMERICAL_H #include #include #include #include #include #include #include "log.h" namespace SysTuning { namespace base { enum IntegerRadixType { INTEGER_RADIX_TYPE_DEC = 10, INTEGER_RADIX_TYPE_HEX = 16 }; inline std::string number(uint64_t value, int32_t base = INTEGER_RADIX_TYPE_DEC) { std::stringstream ss; if (base == INTEGER_RADIX_TYPE_DEC) { ss << std::dec << value; } else if (base == INTEGER_RADIX_TYPE_HEX) { ss << std::hex << value; } return ss.str(); } inline std::string ConvertTimestampToSecStr(uint64_t timestamp, uint8_t precision) { double seconds = static_cast(timestamp) / 1e9; std::stringstream ss; ss << std::fixed << std::setprecision(precision) << seconds; return ss.str(); } template std::optional StrToInt(const std::string &str, int32_t base = INTEGER_RADIX_TYPE_DEC) { if (!str.empty()) { char *endPtr = nullptr; T value; if constexpr (std::is_same_v) { value = static_cast(std::strtoul(str.c_str(), &endPtr, base)); } else if constexpr (std::is_same_v) { value = static_cast(std::strtol(str.c_str(), &endPtr, base)); } else if constexpr (std::is_same_v) { value = static_cast(std::strtoull(str.c_str(), &endPtr, base)); } else if constexpr (std::is_same_v) { value = static_cast(std::strtoll(str.c_str(), &endPtr, base)); } if (!*endPtr) { return std::make_optional(value); } } TS_LOGD("Illegal value: %s", str.c_str()); return std::nullopt; } inline std::optional StrToDouble(const std::string &str) { if (!str.empty()) { #ifdef WIN32_ char *end = nullptr; double value = std::strtod(str.c_str(), &end); #else double value = std::stod(str); #endif return std::make_optional(value); } return std::nullopt; } } // namespace base } // namespace SysTuning #endif // INCLUDE_TUNING_EXT_BASE_STRING_UTILS_H_