1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "string_util.h"
17 #include <fstream>
18 #include <iterator>
19 #include <ctime>
20 #include <sys/time.h>
21 #include "intell_voice_log.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "StringUtil"
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace IntellVoiceUtils {
30 static const char16_t DELIMITER_EQUAL_SIGN = '=';
31
32 // #ABC#WWW##XYZ#---->vector[0]: ABC, vector[1]: WWW, vector[2]: XYZ
Split(const string & str,const string & sep,vector<string> & res)33 void StringUtil::Split(const string &str, const string &sep, vector<string> &res)
34 {
35 res.clear();
36 string tmpStr = str;
37 unsigned int startPos = 0;
38 size_t findPos = tmpStr.find(sep, startPos);
39 while (findPos != string::npos) {
40 if (findPos > startPos) {
41 res.push_back(tmpStr.substr(static_cast<uint32_t>(startPos), static_cast<uint32_t>((findPos - startPos))));
42 }
43 startPos = findPos + sep.length();
44 findPos = tmpStr.find(sep, startPos);
45 }
46
47 if (startPos < tmpStr.length()) {
48 res.push_back(tmpStr.substr(startPos));
49 }
50 }
51
52 // the func is called for AcousticPruner and AcousticPrunerExtra file
StringSplit(string & str,const string & pattern)53 std::vector<string> StringUtil::StringSplit(string &str, const string &pattern)
54 {
55 string::size_type pos;
56 vector<string> result;
57 str += pattern;
58 unsigned int size = str.size();
59
60 for (unsigned int i = 0; i < size; i++) {
61 pos = str.find(pattern, i);
62 if (pos < size) {
63 string s = str.substr(i, pos - i);
64 result.push_back(s);
65 i = pos + pattern.size() - 1;
66 }
67 }
68
69 return result;
70 }
71
PrintVector(vector<T> & array,string & delimiter)72 template <typename T> string StringUtil::PrintVector(vector<T> &array, string &delimiter)
73 {
74 if (array.empty()) {
75 return "";
76 }
77
78 ostringstream oss;
79 copy(array.begin(), array.end() - 1, ostream_iterator<T>(oss, delimiter.c_str()));
80 oss << array.back();
81
82 return oss.str();
83 }
84
TrimSpecialChars(string & str)85 void StringUtil::TrimSpecialChars(string &str)
86 {
87 size_t sp = 0;
88
89 while (sp < str.size()) {
90 size_t ep = str.find_first_of(" \t\n\v\f\r,.:;!~@#$%^&*()`?/-+", sp);
91 if (ep != string::npos) {
92 str.erase(ep, 1);
93 sp = ep;
94 } else {
95 break;
96 }
97 }
98 }
99
CalSubStrNum(const string & str,const string & subStr)100 uint32_t StringUtil::CalSubStrNum(const string &str, const string &subStr)
101 {
102 if (subStr.size() == 0) {
103 return 0;
104 }
105 uint32_t count = 0;
106 string::size_type pos = 0;
107 pos = str.find(subStr, pos);
108 while (pos != string::npos) {
109 count++;
110 pos = pos + subStr.size();
111 pos = str.find(subStr, pos);
112 }
113
114 return count;
115 }
SplitLineToPair(const std::string & line,std::string & first,std::string & second)116 bool StringUtil::SplitLineToPair(const std::string &line, std::string &first, std::string &second)
117 {
118 if (line.empty()) {
119 INTELL_VOICE_LOG_ERROR("line is empty");
120 return false;
121 }
122 // pinyin:words
123 size_t pos = line.find(DELIMITER_EQUAL_SIGN);
124 // not find delimiter or it is the last char.
125 if (string::npos == pos || (line.length() - 1) == pos) {
126 return false;
127 }
128
129 first = line.substr(0, pos);
130 second = line.substr(pos + 1, string::npos);
131 // trim left and right spaces.
132 Trim(first);
133 Trim(second);
134
135 if (first.empty() || second.empty()) {
136 INTELL_VOICE_LOG_ERROR("line is invalid, first:%{public}s, second:%{public}s", first.c_str(), second.c_str());
137 return false;
138 }
139 return true;
140 }
141 }
142 }
143
144