1 /**
2 * Copyright 2021 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
22 namespace mindspore {
23 namespace lite {
EraseBlankSpaceAndLineBreak(std::string * input_string)24 int EraseBlankSpaceAndLineBreak(std::string *input_string) {
25 if (input_string == nullptr) {
26 MS_LOG(ERROR) << "input_string is nullptr";
27 return false;
28 }
29 input_string->erase(std::remove(input_string->begin(), input_string->end(), ' '), input_string->end());
30 input_string->erase(std::remove(input_string->begin(), input_string->end(), '\r'), input_string->end());
31 input_string->erase(std::remove(input_string->begin(), input_string->end(), '\n'), input_string->end());
32 return true;
33 }
34
EraseQuotes(std::string * input_string)35 int EraseQuotes(std::string *input_string) {
36 if (input_string == nullptr) {
37 MS_LOG(ERROR) << "input_string is nullptr";
38 return false;
39 }
40 if (!input_string->empty()) {
41 std::string::size_type pos = 0;
42 pos = input_string->find('\"', pos);
43 while (pos != std::string::npos) {
44 input_string->erase(pos, 1);
45 pos = input_string->find('\"', pos);
46 }
47 }
48 return true;
49 }
50
SplitStringToVector(const std::string & raw_str,const char & delimiter)51 std::vector<std::string> SplitStringToVector(const std::string &raw_str, const char &delimiter) {
52 if (raw_str.empty()) {
53 MS_LOG(ERROR) << "input string is empty.";
54 return {};
55 }
56 std::vector<std::string> res;
57 std::string::size_type last_pos = 0;
58 auto cur_pos = raw_str.find(delimiter);
59 while (cur_pos != std::string::npos) {
60 res.push_back(raw_str.substr(last_pos, cur_pos - last_pos));
61 cur_pos++;
62 last_pos = cur_pos;
63 cur_pos = raw_str.find(delimiter, cur_pos);
64 }
65 if (last_pos < raw_str.size()) {
66 res.push_back(raw_str.substr(last_pos, raw_str.size() - last_pos + 1));
67 }
68 return res;
69 }
70
ConvertIntNum(const std::string & str,int * value)71 bool ConvertIntNum(const std::string &str, int *value) {
72 if (value == nullptr) {
73 MS_LOG(ERROR) << "value is nullptr";
74 return false;
75 }
76 char *ptr = nullptr;
77 constexpr int kBase = 10;
78 *value = strtol(str.c_str(), &ptr, kBase);
79 return ptr == (str.c_str() + str.size());
80 }
81
ConvertDoubleNum(const std::string & str,double * value)82 bool ConvertDoubleNum(const std::string &str, double *value) {
83 if (value == nullptr) {
84 MS_LOG(ERROR) << "value is nullptr";
85 return false;
86 }
87 char *endptr = nullptr;
88 *value = strtod(str.c_str(), &endptr);
89 return *str.c_str() != 0 && *endptr == 0;
90 }
91
ConvertBool(std::string str,bool * value)92 bool ConvertBool(std::string str, bool *value) {
93 if (value == nullptr) {
94 MS_LOG(ERROR) << "value is nullptr";
95 return false;
96 }
97 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
98 if (str == "true") {
99 *value = true;
100 return true;
101 } else if (str == "false") {
102 *value = false;
103 return true;
104 } else {
105 return false;
106 }
107 }
108
ConvertDoubleVector(const std::string & str,std::vector<double> * value)109 bool ConvertDoubleVector(const std::string &str, std::vector<double> *value) {
110 if (value == nullptr) {
111 MS_LOG(ERROR) << "value is nullptr";
112 return false;
113 }
114 // remove []
115 std::string tmp_str = str.substr(1, str.size() - 2);
116 // split ,
117 std::vector<std::string> strings = SplitStringToVector(tmp_str, ',');
118 size_t size = strings.size();
119 value->resize(size);
120 // ConvertToFloatVec
121 for (size_t i = 0; i < size; i++) {
122 if (!ConvertDoubleNum(strings[i], &value->at(i))) {
123 MS_LOG(ERROR) << "Invalid num";
124 return false;
125 }
126 }
127 return true;
128 }
129 } // namespace lite
130 } // namespace mindspore
131