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 44 : void StringUtils::Split(const std::string &str, const char_t delim, std::vector<std::string> &elems) 15 : { 16 44 : elems.clear(); 17 44 : if (str.empty()) { 18 0 : elems.emplace_back(""); 19 0 : return; 20 : } 21 : 22 88 : std::stringstream ss(str); 23 88 : std::string item; 24 : 25 177 : while (getline(ss, item, delim)) { 26 133 : elems.push_back(item); 27 : } 28 : 29 44 : const auto strSize = str.size(); 30 44 : if ((strSize > 0U) && (str[strSize - 1U] == delim)) { 31 0 : elems.emplace_back(""); 32 : } 33 : } 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 11 : std::string StringUtils::Trim(const std::string& str) { 50 11 : size_t first = str.find_first_not_of(" \t\r\n"); 51 11 : if (std::string::npos == first) { 52 0 : return str; 53 : } 54 11 : size_t last = str.find_last_not_of(" \t\r\n"); 55 11 : return str.substr(first, (last - first + 1)); 56 : } 57 : } // namespace acl