LCOV - code coverage report
Current view: top level - adump/common - str_utils.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 52.6 % 19 10
Test Date: 2026-08-31 10:09:28 Functions: 50.0 % 4 2

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #ifndef ADUMP_COMMON_STR_UTILS_H
      11              : #define ADUMP_COMMON_STR_UTILS_H
      12              : #include <string>
      13              : #include <set>
      14              : #include <vector>
      15              : #include <sstream>
      16              : #include <cstdint>
      17              : 
      18              : namespace Adx {
      19              : class StrUtils {
      20              : public:
      21              :     static std::string TrimLeft(const std::string& s);
      22              :     static std::string TrimRight(const std::string& s);
      23              :     static std::string Trim(const std::string& s);
      24              :     static std::string Replace(const std::string& s, const std::set<char>& oldChars, char newChar);
      25              :     static bool EndsWith(const std::string& s, const char* suffix);
      26              :     static bool StartsWith(const std::string& s, const char* prefix);
      27              : 
      28              :     template <typename T>
      29              :     static std::string ToString(const std::vector<T>& v);
      30              : 
      31              :     template <typename T>
      32              :     static bool ToInteger(const std::string& str, T& integer);
      33              :     static std::vector<std::string> Split(const std::string& str, const char* const delimiter);
      34              :     static std::string Format(const char* const fmt, ...);
      35              : };
      36              : 
      37              : template <typename T>
      38          346 : std::string StrUtils::ToString(const std::vector<T>& v)
      39              : {
      40          346 :     std::stringstream ss;
      41          346 :     std::string delimeter = "";
      42          346 :     ss << '[';
      43          753 :     for (const auto& e : v) {
      44           61 :         ss << delimeter << e;
      45           61 :         delimeter = ",";
      46              :     }
      47          346 :     ss << ']';
      48          692 :     return ss.str();
      49          346 : }
      50              : 
      51              : template <typename T>
      52              : struct ToIntegerTrait;
      53              : template <>
      54              : struct ToIntegerTrait<int32_t> {
      55            0 :     static inline int32_t Invoke(const std::string& s) { return std::stoi(s); }
      56              : };
      57              : template <>
      58              : struct ToIntegerTrait<uint32_t> {
      59              :     static inline uint32_t Invoke(const std::string& s) { return static_cast<uint32_t>(std::stoul(s)); }
      60              : };
      61              : 
      62              : template <typename T>
      63            0 : bool StrUtils::ToInteger(const std::string& str, T& integer)
      64              : {
      65            0 :     auto stox = ToIntegerTrait<T>::Invoke;
      66              :     try {
      67            0 :         integer = stox(str);
      68            0 :     } catch (std::invalid_argument&) {
      69            0 :         return false;
      70            0 :     } catch (std::out_of_range&) {
      71            0 :         return false;
      72              :     }
      73            0 :     return true;
      74              : }
      75              : } // namespace Adx
      76              : #endif // ADUMP_COMMON_STR_UTILS_H
        

Generated by: LCOV version 2.0-1