LCOV - code coverage report
Current view: top level - legacy/ascend950/common/utils - string_util.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 71.4 % 49 35
Test Date: 2026-08-04 10:52:23 Functions: 74.0 % 373 276

            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              : 
      11              : #ifndef HCCLV2_STRING_UTIL_H
      12              : #define HCCLV2_STRING_UTIL_H
      13              : 
      14              : #include <string>
      15              : #include <sstream>
      16              : #include <iomanip>
      17              : #include <vector>
      18              : #include "securec.h"
      19              : #include "log.h"
      20              : 
      21              : namespace Hccl {
      22              : 
      23        61483 : template <typename... Args> inline std::string StringFormat(const char *format, Args... args)
      24              : {
      25              :     using namespace std;
      26        61483 :     constexpr size_t bufSize = BUFSIZ;
      27              :     char             buffer[bufSize];
      28        61483 :     int result = snprintf_s(&buffer[0], bufSize, bufSize, format, args...);
      29        61483 :      if (result < 0) {
      30            0 :         HCCL_ERROR("[StringFormat] data snprintf_s failed.");
      31            0 :         return "";
      32              :     }
      33        61483 :     size_t actualSize = static_cast<size_t>(result);
      34        61483 :     if (actualSize + 1 > bufSize) {
      35            0 :         actualSize++;
      36            0 :         std::vector<char> newbuffer(actualSize);
      37            0 :         auto ret = snprintf_s(newbuffer.data(), actualSize, actualSize, format, args...);
      38            0 :         if(ret != EOK){
      39            0 :             HCCL_ERROR("[StringFormat] data snprintf_s failed.");
      40            0 :             return "";
      41              :         }
      42            0 :         return newbuffer.data();
      43            0 :     }
      44       122966 :     return buffer;
      45              : }
      46              : 
      47          524 : template <typename I> std::string Dec2hex(I i)
      48              : {
      49              :     static_assert(std::is_integral<I>::value, "type I is not a integral");
      50          524 :     std::stringstream ss;
      51          524 :     ss << std::hex << "0x" << i;
      52         1048 :     return ss.str();
      53          524 : }
      54              : 
      55            1 : inline std::vector<std::string> SplitString(const std::string &str, const char c)
      56              : {
      57            1 :     std::string::size_type startPos = 0;
      58            1 :     std::string::size_type foundPos = str.find(c);
      59              : 
      60            1 :     std::vector<std::string> strVector;
      61            1 :     while (foundPos != std::string::npos) {
      62            0 :         strVector.push_back(str.substr(startPos, foundPos - startPos));
      63            0 :         startPos = foundPos + 1;
      64            0 :         foundPos = str.find(c, startPos);
      65              :     }
      66            1 :     if (startPos != str.length()) {
      67            1 :         strVector.push_back(str.substr(startPos));
      68              :     }
      69            1 :     return strVector;
      70            0 : }
      71              : 
      72           50 : template <class T> T String2T(const std::string &s)
      73              : {
      74              :     // T must support >>
      75              :     T                  t;
      76           50 :     std::istringstream ist(s);
      77           50 :     ist >> t;
      78           50 :     return t;
      79           50 : }
      80              : 
      81          347 : inline std::string Bytes2hex(const void *data, int size)
      82              : {
      83          347 :     constexpr int hexWidth = 2;
      84          347 :     std::stringstream ss;
      85          347 :     ss << std::hex << std::setfill('0');
      86          347 :     const auto *tmp = static_cast<const unsigned char *>(data);
      87         7349 :     for (int i = 0; i < size; i++) {
      88         7002 :         ss << std::setw(hexWidth) << static_cast<int>(*tmp);
      89         7002 :         tmp++;
      90              :     }
      91          694 :     return ss.str();
      92          347 : }
      93              : 
      94              : } // namespace Hccl
      95              : 
      96              : #endif // HCCLV2_STRING_UTIL_H
        

Generated by: LCOV version 2.0-1