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 : #include "string_utils.h"
12 :
13 : namespace acl {
14 96 : void StringUtils::Split(const std::string& str, const char_t delim, std::vector<std::string>& elems)
15 : {
16 96 : elems.clear();
17 96 : if (str.empty()) {
18 0 : elems.emplace_back("");
19 0 : return;
20 : }
21 :
22 96 : std::stringstream ss(str);
23 96 : std::string item;
24 :
25 385 : while (getline(ss, item, delim)) {
26 289 : elems.push_back(item);
27 : }
28 :
29 96 : const auto strSize = str.size();
30 96 : if ((strSize > 0U) && (str[strSize - 1U] == delim)) {
31 0 : elems.emplace_back("");
32 : }
33 96 : }
34 :
35 0 : bool StringUtils::IsDigit(const std::string& str)
36 : {
37 0 : if (str.empty()) {
38 0 : return false;
39 : }
40 :
41 0 : for (const char_t& c : str) {
42 0 : if (isdigit(static_cast<int32_t>(c)) == 0) {
43 0 : return false;
44 : }
45 : }
46 0 : return true;
47 : }
48 :
49 12 : std::string StringUtils::Trim(const std::string& str)
50 : {
51 12 : const size_t first = str.find_first_not_of(" \t\r\n");
52 12 : if (std::string::npos == first) {
53 0 : return str;
54 : }
55 12 : const size_t last = str.find_last_not_of(" \t\r\n");
56 12 : return str.substr(first, (last - first + 1UL));
57 : }
58 : } // namespace acl
|