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 #ifndef STRING_UTIL_H
17 #define STRING_UTIL_H
18
19 #include <string>
20 #include <vector>
21 #include <sstream>
22 #include "nocopyable.h"
23 #include "securec.h"
24
25 namespace OHOS {
26 namespace IntellVoiceUtils {
27 class StringUtil {
28 public:
StringUtil()29 StringUtil() {}
~StringUtil()30 ~StringUtil() {}
31 static void Split(const std::string &str, const std::string &sep, std::vector<std::string> &res);
32 static std::vector<std::string> StringSplit(std::string &str, const std::string &pattern);
33 static void TrimL(std::string &str);
34 static void TrimR(std::string &str);
35 static void Trim(std::string &str);
36 template <typename T> static std::string PrintVector(std::vector<T> &array, std::string &delimiter);
37 static std::string Float2String(float arg, int32_t precision = 6);
38 static std::string Int2String(int32_t arg);
39 static void StringToLower(std::string &str);
40 static std::string StringToUpper(const std::string &str);
41 static std::string StringToUpperX(const std::string &str);
42 static void TrimSpecialChars(std::string &str);
43 static uint32_t CalSubStrNum(const std::string &str, const std::string &subStr);
44 static bool SplitLineToPair(const std::string &line, std::string &first, std::string &second);
45
46 private:
47 DISALLOW_COPY_AND_MOVE(StringUtil);
48 };
49
50 // remove space of left side, ascii
TrimL(std::string & str)51 inline void StringUtil::TrimL(std::string &str)
52 {
53 size_t p = str.find_first_not_of(" \t\n\v\f\r");
54 if (p == std::string::npos) {
55 str.clear();
56 } else {
57 str = str.substr(p);
58 }
59 }
60
61 // remove space of right side, ascii
TrimR(std::string & str)62 inline void StringUtil::TrimR(std::string &str)
63 {
64 size_t p = str.find_last_not_of(" \t\n\v\f\r");
65 if (p == std::string::npos) {
66 str.clear();
67 } else {
68 str.erase(p + 1);
69 }
70 }
71
72 // remove space of both side, ascii
Trim(std::string & str)73 inline void StringUtil::Trim(std::string &str)
74 {
75 TrimL(str);
76 TrimR(str);
77 }
78
Float2String(float arg,int32_t precision)79 inline std::string StringUtil::Float2String(float arg, int32_t precision)
80 {
81 // warning -- local variable 'score' declared not subsequently referenced is false alarm, ignore
82 char score[100] = {'\0'}; //lint !e529
83 if (sprintf_s(score, sizeof(score), "%.*f", precision, arg) == -1) {
84 return "";
85 }
86
87 return std::string(score);
88 }
89
Int2String(int32_t arg)90 inline std::string StringUtil::Int2String(int32_t arg)
91 {
92 static const int32_t RPESICION = 9;
93 std::stringstream ss;
94
95 ss.precision(RPESICION);
96 ss << arg;
97
98 return ss.str();
99 }
100
StringToLower(std::string & str)101 inline void StringUtil::StringToLower(std::string &str)
102 {
103 for (uint32_t k = 0; k < str.size(); k++) {
104 str[k] = static_cast<char>(tolower(str[k]));
105 }
106 }
107
StringToUpper(const std::string & str)108 inline std::string StringUtil::StringToUpper(const std::string &str)
109 {
110 std::string upstr = str;
111 for (uint32_t k = 0; k < str.size(); k++) {
112 upstr[k] = static_cast<char>(toupper(str[k]));
113 }
114
115 return upstr;
116 }
117
StringToUpperX(const std::string & str)118 inline std::string StringUtil::StringToUpperX(const std::string &str)
119 {
120 std::string upstr = str;
121 for (uint32_t i = 0; i < upstr.size(); i++) {
122 if (upstr[i] <= 'z' && upstr[i] >= 'a') {
123 upstr[i] -= 32; // 32 is the difference between lowercase and uppercase ASCII
124 }
125 }
126
127 return upstr;
128 }
129 }
130 }
131 #endif
132