1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ci#ifndef INCLUDE_BASE_STRING_TO_NUMERICAL_H
17fb726d48Sopenharmony_ci#define INCLUDE_BASE_STRING_TO_NUMERICAL_H
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ci#include <iomanip>
20fb726d48Sopenharmony_ci#include <iostream>
21fb726d48Sopenharmony_ci#include <optional>
22fb726d48Sopenharmony_ci#include <sstream>
23fb726d48Sopenharmony_ci#include <string>
24fb726d48Sopenharmony_ci#include <typeinfo>
25fb726d48Sopenharmony_ci#include "log.h"
26fb726d48Sopenharmony_cinamespace SysTuning {
27fb726d48Sopenharmony_cinamespace base {
28fb726d48Sopenharmony_cienum IntegerRadixType { INTEGER_RADIX_TYPE_DEC = 10, INTEGER_RADIX_TYPE_HEX = 16 };
29fb726d48Sopenharmony_ciinline std::string number(uint64_t value, int32_t base = INTEGER_RADIX_TYPE_DEC)
30fb726d48Sopenharmony_ci{
31fb726d48Sopenharmony_ci    std::stringstream ss;
32fb726d48Sopenharmony_ci    if (base == INTEGER_RADIX_TYPE_DEC) {
33fb726d48Sopenharmony_ci        ss << std::dec << value;
34fb726d48Sopenharmony_ci    } else if (base == INTEGER_RADIX_TYPE_HEX) {
35fb726d48Sopenharmony_ci        ss << std::hex << value;
36fb726d48Sopenharmony_ci    }
37fb726d48Sopenharmony_ci    return ss.str();
38fb726d48Sopenharmony_ci}
39fb726d48Sopenharmony_ci
40fb726d48Sopenharmony_ciinline std::string ConvertTimestampToSecStr(uint64_t timestamp, uint8_t precision)
41fb726d48Sopenharmony_ci{
42fb726d48Sopenharmony_ci    double seconds = static_cast<double>(timestamp) / 1e9;
43fb726d48Sopenharmony_ci    std::stringstream ss;
44fb726d48Sopenharmony_ci    ss << std::fixed << std::setprecision(precision) << seconds;
45fb726d48Sopenharmony_ci    return ss.str();
46fb726d48Sopenharmony_ci}
47fb726d48Sopenharmony_ci
48fb726d48Sopenharmony_citemplate <typename T>
49fb726d48Sopenharmony_cistd::optional<T> StrToInt(const std::string &str, int32_t base = INTEGER_RADIX_TYPE_DEC)
50fb726d48Sopenharmony_ci{
51fb726d48Sopenharmony_ci    if (!str.empty()) {
52fb726d48Sopenharmony_ci        char *endPtr = nullptr;
53fb726d48Sopenharmony_ci        T value;
54fb726d48Sopenharmony_ci        if constexpr (std::is_same_v<T, uint32_t>) {
55fb726d48Sopenharmony_ci            value = static_cast<T>(std::strtoul(str.c_str(), &endPtr, base));
56fb726d48Sopenharmony_ci        } else if constexpr (std::is_same_v<T, int32_t>) {
57fb726d48Sopenharmony_ci            value = static_cast<T>(std::strtol(str.c_str(), &endPtr, base));
58fb726d48Sopenharmony_ci        } else if constexpr (std::is_same_v<T, uint64_t>) {
59fb726d48Sopenharmony_ci            value = static_cast<T>(std::strtoull(str.c_str(), &endPtr, base));
60fb726d48Sopenharmony_ci        } else if constexpr (std::is_same_v<T, int64_t>) {
61fb726d48Sopenharmony_ci            value = static_cast<T>(std::strtoll(str.c_str(), &endPtr, base));
62fb726d48Sopenharmony_ci        }
63fb726d48Sopenharmony_ci        if (!*endPtr) {
64fb726d48Sopenharmony_ci            return std::make_optional(value);
65fb726d48Sopenharmony_ci        }
66fb726d48Sopenharmony_ci    }
67fb726d48Sopenharmony_ci    TS_LOGD("Illegal value: %s", str.c_str());
68fb726d48Sopenharmony_ci    return std::nullopt;
69fb726d48Sopenharmony_ci}
70fb726d48Sopenharmony_ci
71fb726d48Sopenharmony_ciinline std::optional<double> StrToDouble(const std::string &str)
72fb726d48Sopenharmony_ci{
73fb726d48Sopenharmony_ci    if (!str.empty()) {
74fb726d48Sopenharmony_ci#ifdef WIN32_
75fb726d48Sopenharmony_ci        char *end = nullptr;
76fb726d48Sopenharmony_ci        double value = std::strtod(str.c_str(), &end);
77fb726d48Sopenharmony_ci#else
78fb726d48Sopenharmony_ci        double value = std::stod(str);
79fb726d48Sopenharmony_ci#endif
80fb726d48Sopenharmony_ci        return std::make_optional(value);
81fb726d48Sopenharmony_ci    }
82fb726d48Sopenharmony_ci    return std::nullopt;
83fb726d48Sopenharmony_ci}
84fb726d48Sopenharmony_ci} // namespace base
85fb726d48Sopenharmony_ci} // namespace SysTuning
86fb726d48Sopenharmony_ci
87fb726d48Sopenharmony_ci#endif // INCLUDE_TUNING_EXT_BASE_STRING_UTILS_H_
88