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 static bool StringToInt(const std::string &str, int32_t &val);
46
47 private:
48 DISALLOW_COPY_AND_MOVE(StringUtil);
49 };
50
51 // remove space of left side, ascii
TrimL(std::string &str)52 inline void StringUtil::TrimL(std::string &str)
53 {
54 size_t p = str.find_first_not_of(" \t\n\v\f\r");
55 if (p == std::string::npos) {
56 str.clear();
57 } else {
58 str = str.substr(p);
59 }
60 }
61
62 // remove space of right side, ascii
TrimR(std::string &str)63 inline void StringUtil::TrimR(std::string &str)
64 {
65 size_t p = str.find_last_not_of(" \t\n\v\f\r");
66 if (p == std::string::npos) {
67 str.clear();
68 } else {
69 str.erase(p + 1);
70 }
71 }
72
73 // remove space of both side, ascii
Trim(std::string &str)74 inline void StringUtil::Trim(std::string &str)
75 {
76 TrimL(str);
77 TrimR(str);
78 }
79
Float2String(float arg, int32_t precision)80 inline std::string StringUtil::Float2String(float arg, int32_t precision)
81 {
82 // warning -- local variable 'score' declared not subsequently referenced is false alarm, ignore
83 char score[100] = {'\0'}; //lint !e529
84 if (sprintf_s(score, sizeof(score), "%.*f", precision, arg) == -1) {
85 return "";
86 }
87
88 return std::string(score);
89 }
90
Int2String(int32_t arg)91 inline std::string StringUtil::Int2String(int32_t arg)
92 {
93 static const int32_t RPESICION = 9;
94 std::stringstream ss;
95
96 ss.precision(RPESICION);
97 ss << arg;
98
99 return ss.str();
100 }
101
StringToLower(std::string &str)102 inline void StringUtil::StringToLower(std::string &str)
103 {
104 for (uint32_t k = 0; k < str.size(); k++) {
105 str[k] = static_cast<char>(tolower(str[k]));
106 }
107 }
108
StringToUpper(const std::string &str)109 inline std::string StringUtil::StringToUpper(const std::string &str)
110 {
111 std::string upstr = str;
112 for (uint32_t k = 0; k < str.size(); k++) {
113 upstr[k] = static_cast<char>(toupper(str[k]));
114 }
115
116 return upstr;
117 }
118
StringToUpperX(const std::string &str)119 inline std::string StringUtil::StringToUpperX(const std::string &str)
120 {
121 std::string upstr = str;
122 for (uint32_t i = 0; i < upstr.size(); i++) {
123 if (upstr[i] <= 'z' && upstr[i] >= 'a') {
124 upstr[i] -= 32; // 32 is the difference between lowercase and uppercase ASCII
125 }
126 }
127
128 return upstr;
129 }
130 }
131 }
132 #endif
133