LCOV - code coverage report
Current view: top level - adump/common - str_utils.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 50.0 % 20 10
Test Date: 2026-07-28 10:54:24 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          323 : template <typename T> std::string StrUtils::ToString(const std::vector<T> &v)
      38              : {
      39          323 :     std::stringstream ss;
      40          323 :     std::string delimeter = "";
      41          323 :     ss << '[';
      42          348 :     for (const auto &e : v) {
      43           25 :         ss << delimeter << e;
      44           25 :         delimeter = ",";
      45              :     }
      46          323 :     ss << ']';
      47          646 :     return ss.str();
      48          323 : }
      49              : 
      50              : template <typename T> struct ToIntegerTrait;
      51              : template <> struct ToIntegerTrait<int32_t> {
      52            0 :     static inline int32_t Invoke(const std::string &s)
      53              :     {
      54            0 :         return std::stoi(s);
      55              :     }
      56              : };
      57              : template <> struct ToIntegerTrait<uint32_t> {
      58              :     static inline uint32_t Invoke(const std::string &s)
      59              :     {
      60              :         return static_cast<uint32_t>(std::stoul(s));
      61              :     }
      62              : };
      63              : 
      64              : template <typename T>
      65            0 : bool StrUtils::ToInteger(const std::string &str, T &integer)
      66              : {
      67            0 :     auto stox = ToIntegerTrait<T>::Invoke;
      68              :     try {
      69            0 :         integer = stox(str);
      70            0 :     } catch (std::invalid_argument &) {
      71            0 :         return false;
      72            0 :     } catch (std::out_of_range &) {
      73            0 :         return false;
      74              :     }
      75            0 :     return true;
      76              : }
      77              : } // namespace Adx
      78              : #endif // ADUMP_COMMON_STR_UTILS_H
        

Generated by: LCOV version 2.0-1