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