LCOV - code coverage report
Current view: top level - adump/common - str_utils.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 78.4 % 51 40
Test Date: 2026-08-31 10:09:28 Functions: 90.9 % 11 10

            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              : #include <cstdarg>
      11              : #include <algorithm>
      12              : #include <cstring>
      13              : #include <stdexcept>
      14              : #include "securec.h"
      15              : #include "str_utils.h"
      16              : 
      17              : namespace Adx {
      18              : constexpr size_t const LIMIT_PER_MESSAGE = 1024U;
      19              : 
      20         1420 : std::string StrUtils::TrimLeft(const std::string& s)
      21              : {
      22         1420 :     std::string rs = s;
      23         2666 :     (void)rs.erase(rs.begin(), std::find_if(rs.begin(), rs.end(), [](uint8_t ch) { return !std::isspace(ch); }));
      24         1420 :     return rs;
      25            0 : }
      26              : 
      27         1420 : std::string StrUtils::TrimRight(const std::string& s)
      28              : {
      29         1420 :     std::string rs = s;
      30         2663 :     (void)rs.erase(std::find_if(rs.rbegin(), rs.rend(), [](uint8_t ch) { return !std::isspace(ch); }).base(), rs.end());
      31         1420 :     return rs;
      32            0 : }
      33              : 
      34         1420 : std::string StrUtils::Trim(const std::string& s)
      35              : {
      36         1420 :     std::string tmp = TrimLeft(s);
      37         2840 :     return TrimRight(tmp);
      38         1420 : }
      39              : 
      40           23 : std::string StrUtils::Replace(const std::string& s, const std::set<char>& oldChars, char newChar)
      41              : {
      42          569 :     auto replace = [&oldChars, newChar](char& ch) {
      43          569 :         if (oldChars.find(ch) != oldChars.cend()) {
      44            0 :             ch = newChar;
      45              :         }
      46          569 :     };
      47           23 :     std::string rs = s;
      48           23 :     (void)std::for_each(rs.begin(), rs.end(), replace);
      49           23 :     return rs;
      50            0 : }
      51              : 
      52           79 : bool StrUtils::EndsWith(const std::string& s, const char* suffix)
      53              : {
      54           79 :     if (suffix == nullptr) {
      55            0 :         return false;
      56              :     }
      57           79 :     const size_t suffixLen = std::strlen(suffix);
      58           79 :     return s.size() >= suffixLen && s.compare(s.size() - suffixLen, suffixLen, suffix) == 0;
      59              : }
      60              : 
      61            0 : bool StrUtils::StartsWith(const std::string& s, const char* prefix)
      62              : {
      63            0 :     if (prefix == nullptr) {
      64            0 :         return false;
      65              :     }
      66            0 :     const size_t prefixLen = std::strlen(prefix);
      67            0 :     return s.size() >= prefixLen && s.compare(0, prefixLen, prefix) == 0;
      68              : }
      69              : 
      70           24 : std::vector<std::string> StrUtils::Split(const std::string& str, const char* const delimiter)
      71              : {
      72           24 :     std::vector<std::string> resVec;
      73           24 :     if (str.empty()) {
      74            3 :         return resVec;
      75              :     }
      76              : 
      77           21 :     if (delimiter == nullptr) {
      78            0 :         return resVec;
      79              :     }
      80              : 
      81           21 :     std::string token;
      82           21 :     std::istringstream tokenStream(str);
      83          105 :     while (std::getline(tokenStream, token, *delimiter)) {
      84           84 :         resVec.emplace_back(token);
      85              :     }
      86           21 :     return resVec;
      87           21 : }
      88          328 : std::string StrUtils::Format(const char* const fmt, ...)
      89              : {
      90              :     va_list ap;
      91          328 :     va_start(ap, fmt);
      92          328 :     char formatStr[LIMIT_PER_MESSAGE] = {};
      93          328 :     const int32_t ret = vsnprintf_s(
      94              :         formatStr, static_cast<size_t>(LIMIT_PER_MESSAGE), static_cast<size_t>(LIMIT_PER_MESSAGE - 1U), fmt, ap);
      95          328 :     va_end(ap);
      96          656 :     return ret == -1 ? "" : formatStr;
      97              : }
      98              : } // namespace Adx
        

Generated by: LCOV version 2.0-1