1 /**
2 * Copyright 2021-2022 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "tools/common/string_util.h"
18 #include <algorithm>
19 #include <vector>
20 #include <string>
21 #include <regex>
22
23 namespace mindspore {
24 namespace lite {
EraseBlankSpaceAndLineBreak(std::string * input_string)25 bool EraseBlankSpaceAndLineBreak(std::string *input_string) {
26 if (input_string == nullptr) {
27 MS_LOG(ERROR) << "input_string is nullptr";
28 return false;
29 }
30 (void)input_string->erase(0, input_string->find_first_not_of(" "));
31 (void)input_string->erase(input_string->find_last_not_of(" ") + 1);
32 (void)input_string->erase(std::remove(input_string->begin(), input_string->end(), '\r'), input_string->end());
33 (void)input_string->erase(std::remove(input_string->begin(), input_string->end(), '\n'), input_string->end());
34 return true;
35 }
36
EraseQuotes(std::string * input_string)37 bool EraseQuotes(std::string *input_string) {
38 if (input_string == nullptr) {
39 MS_LOG(ERROR) << "input_string is nullptr";
40 return false;
41 }
42 if (!input_string->empty()) {
43 std::string::size_type pos = 0;
44 pos = input_string->find('\"', pos);
45 while (pos != std::string::npos) {
46 (void)input_string->erase(pos, 1);
47 pos = input_string->find('\"', pos);
48 }
49 }
50 return true;
51 }
52
FindAndReplaceAll(std::string * input_str,const std::string & search,const std::string & replace)53 bool FindAndReplaceAll(std::string *input_str, const std::string &search, const std::string &replace) {
54 if (input_str == nullptr) {
55 MS_LOG(ERROR) << "input_str is nullptr";
56 return false;
57 }
58 auto pos = input_str->find(search);
59 while (pos != std::string::npos) {
60 input_str->replace(pos, search.size(), replace);
61 pos = input_str->find(search, pos + replace.size());
62 }
63 return true;
64 }
65
SplitStringToVector(const std::string & raw_str,const char & delimiter)66 std::vector<std::string> SplitStringToVector(const std::string &raw_str, const char &delimiter) {
67 if (raw_str.empty()) {
68 MS_LOG(ERROR) << "input string is empty.";
69 return {};
70 }
71 std::vector<std::string> res;
72 std::string::size_type last_pos = 0;
73 auto cur_pos = raw_str.find(delimiter);
74 while (cur_pos != std::string::npos) {
75 res.push_back(raw_str.substr(last_pos, cur_pos - last_pos));
76 cur_pos++;
77 last_pos = cur_pos;
78 cur_pos = raw_str.find(delimiter, cur_pos);
79 }
80 if (last_pos < raw_str.size()) {
81 res.push_back(raw_str.substr(last_pos, raw_str.size() - last_pos + 1));
82 }
83 return res;
84 }
85
SplitStringToVector(const std::string & raw_str,const std::string & delimiter)86 std::vector<std::string> SplitStringToVector(const std::string &raw_str, const std::string &delimiter) {
87 size_t pos_start = 0;
88 size_t pos_end = 0;
89 size_t delim_len = delimiter.length();
90 std::string token;
91 std::vector<std::string> res;
92
93 while ((pos_end = raw_str.find(delimiter, pos_start)) != std::string::npos) {
94 token = raw_str.substr(pos_start, pos_end - pos_start);
95 pos_start = pos_end + delim_len;
96 res.push_back(token);
97 }
98 res.push_back(raw_str.substr(pos_start));
99 return res;
100 }
101
ConvertIntNum(const std::string & str,int * value)102 bool ConvertIntNum(const std::string &str, int *value) {
103 if (value == nullptr) {
104 MS_LOG(ERROR) << "value is nullptr";
105 return false;
106 }
107 char *ptr = nullptr;
108 constexpr int kBase = 10;
109 *value = strtol(str.c_str(), &ptr, kBase);
110 return ptr == (str.c_str() + str.size());
111 }
112
ConvertDoubleNum(const std::string & str,double * value)113 bool ConvertDoubleNum(const std::string &str, double *value) {
114 if (value == nullptr) {
115 MS_LOG(ERROR) << "value is nullptr";
116 return false;
117 }
118 char *endptr = nullptr;
119 *value = strtod(str.c_str(), &endptr);
120 return *str.c_str() != 0 && *endptr == 0;
121 }
122
ConvertBool(std::string str,bool * value)123 bool ConvertBool(std::string str, bool *value) {
124 if (value == nullptr) {
125 MS_LOG(ERROR) << "value is nullptr";
126 return false;
127 }
128 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
129 if (str == "true") {
130 *value = true;
131 return true;
132 } else if (str == "false") {
133 *value = false;
134 return true;
135 } else {
136 return false;
137 }
138 }
139
ConvertDoubleVector(const std::string & str,std::vector<double> * value)140 bool ConvertDoubleVector(const std::string &str, std::vector<double> *value) {
141 if (value == nullptr) {
142 MS_LOG(ERROR) << "value is nullptr";
143 return false;
144 }
145 std::regex r("^[[].+]$");
146 if (!std::regex_match(str, r)) {
147 MS_LOG(ERROR) << "the value should begin with [ and end with ]";
148 return false;
149 }
150 // remove []
151 std::string tmp_str = str.substr(1, str.size() - 2);
152 // split ,
153 std::vector<std::string> strings = SplitStringToVector(tmp_str, ',');
154 size_t size = strings.size();
155 value->resize(size);
156 // ConvertToFloatVec
157 for (size_t i = 0; i < size; i++) {
158 if (!ConvertDoubleNum(strings[i], &value->at(i))) {
159 MS_LOG(ERROR) << "Invalid num";
160 return false;
161 }
162 }
163 return true;
164 }
165
Hex2ByteArray(const std::string & hex_str,unsigned char * byte_array,size_t max_len)166 size_t Hex2ByteArray(const std::string &hex_str, unsigned char *byte_array, size_t max_len) {
167 std::regex r("[0-9a-fA-F]+");
168 if (!std::regex_match(hex_str, r)) {
169 MS_LOG(ERROR) << "Some characters of dec_key not in [0-9a-fA-F]";
170 return 0;
171 }
172 if (hex_str.size() % 2 == 1) { // Mod 2 determines whether it is odd
173 MS_LOG(ERROR) << "the hexadecimal dec_key length must be even";
174 return 0;
175 }
176 size_t byte_len = hex_str.size() / 2; // Two hexadecimal characters represent a byte
177 if (byte_len > max_len) {
178 MS_LOG(ERROR) << "the hexadecimal dec_key length exceeds the maximum limit: " << max_len;
179 return 0;
180 }
181 constexpr int32_t a_val = 10; // The value of 'A' in hexadecimal is 10
182 constexpr size_t half_byte_offset = 4;
183 for (size_t i = 0; i < byte_len; ++i) {
184 size_t p = i * 2; // The i-th byte is represented by the 2*i and 2*i+1 hexadecimal characters
185 if (hex_str[p] >= 'a' && hex_str[p] <= 'f') {
186 byte_array[i] = hex_str[p] - 'a' + a_val;
187 } else if (hex_str[p] >= 'A' && hex_str[p] <= 'F') {
188 byte_array[i] = hex_str[p] - 'A' + a_val;
189 } else {
190 byte_array[i] = hex_str[p] - '0';
191 }
192 if (hex_str[p + 1] >= 'a' && hex_str[p + 1] <= 'f') {
193 byte_array[i] = (byte_array[i] << half_byte_offset) | (hex_str[p + 1] - 'a' + a_val);
194 } else if (hex_str[p] >= 'A' && hex_str[p] <= 'F') {
195 byte_array[i] = (byte_array[i] << half_byte_offset) | (hex_str[p + 1] - 'A' + a_val);
196 } else {
197 byte_array[i] = (byte_array[i] << half_byte_offset) | (hex_str[p + 1] - '0');
198 }
199 }
200 return byte_len;
201 }
202
IsNumber(const std::string & item)203 bool IsNumber(const std::string &item) { return !item.empty() && std::all_of(item.begin(), item.end(), ::isdigit); }
204 } // namespace lite
205 } // namespace mindspore
206