* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef INC_COMMON_STRING_UTIL_H_
#define INC_COMMON_STRING_UTIL_H_
#include <cctype>
#include <securec.h>
#include <mutex>
#include <algorithm>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
#include "graph/types.h"
#include "external/ge_common/ge_error_codes.h"
namespace ge {
class GE_FUNC_VISIBILITY StringUtils {
public:
static std::string &Ltrim(std::string &s) {
#if __cplusplus >= 201103L
(void)s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](const int32_t c) { return std::isspace(c) == 0; }));
#else
(void)s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int32_t, int32_t>(std::isspace))));
#endif
return s;
}
static std::string &Rtrim(std::string &s) {
#if __cplusplus >= 201103L
(void)s.erase(std::find_if(s.rbegin(), s.rend(),
[](const int32_t c) { return std::isspace(c) == 0; }).base(), s.end());
#else
(void)s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int32_t, int32_t>(std::isspace))).base(), s.end());
#endif
return s;
}
static std::string &Trim(std::string &s) { return Ltrim(Rtrim(s)); }
static std::vector<std::string, std::allocator<std::string>> Split(const std::string &str, const char_t delim) {
std::vector<std::string, std::allocator<std::string>> elems;
if (str.empty()) {
(void) elems.emplace_back("");
return elems;
}
std::stringstream ss(str);
std::string item;
while (getline(ss, item, delim)) {
(void) elems.push_back(item);
}
const auto str_size = str.size();
if ((str_size > 0U) && (str[str_size - 1U] == delim)) {
(void) elems.emplace_back("");
}
return elems;
}
template<typename Iterator>
static std::string Join(Iterator begin, Iterator end, const std::string &separator) {
if (begin == end) {
return "";
}
std::stringstream str_stream;
str_stream << *begin;
for (Iterator it = std::next(begin); it != end; ++it) {
str_stream << separator << *it;
}
return str_stream.str();
}
static std::string GetFileName(const std::string &s) {
if (s.empty()) {
return "";
}
const std::vector<std::string> files = StringUtils::Split(s, '/');
return files.empty() ? "" : files[files.size() - 1U];
}
static std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value) {
std::string::size_type cur_pos = 0U;
const std::string::size_type old_length = old_value.length();
const std::string::size_type new_length = new_value.length();
for (; cur_pos != std::string::npos; cur_pos += new_length) {
cur_pos = str.find(old_value, cur_pos);
if (cur_pos != std::string::npos) {
(void)str.replace(cur_pos, old_length, new_value);
} else {
break;
}
}
return str;
}
static bool StartWith(const std::string &str, const std::string &str_x) {
return ((str.size() >= str_x.size()) && (str.compare(0U, str_x.size(), str_x) == 0));
}
static std::string FormatString(const char_t * const format, ...) {
const uint32_t MAX_BUFFER_LEN = 1024U;
va_list args;
va_start(args, format);
char_t buffer[MAX_BUFFER_LEN] = {};
const int32_t ret = vsnprintf_s(&buffer[0], MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1U, format, args);
va_end(args);
return (ret > 0) ? buffer : "";
}
static bool IsSignedInt32(const std::string &str) {
if (str.empty()) {
return false;
}
try {
(void)std::stoi(str);
} catch (...) {
return false;
}
for (size_t i = 1; i < str.size(); i++) {
if (isdigit(static_cast<int32_t>(str.at(i))) == 0) {
return false;
}
}
return true;
}
static void ReplaceSubstring(std::string &str, const std::string &to_replace, const std::string &replace_with) {
if (to_replace.empty()) {
return;
}
std::string::size_type pos = str.find(to_replace);
while (pos != std::string::npos) {
(void) str.replace(pos, to_replace.length(), replace_with);
pos += replace_with.length();
pos = str.find(to_replace, pos);
}
}
};
}
#endif