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 PRINT_UTIL_H
17 #define PRINT_UTIL_H
18
19 #include <algorithm>
20 #include <iostream>
21 #include <sstream>
22 #include <list>
23 #include <vector>
24 #include <string>
25
26 #include "print_log.h"
27
28 namespace OHOS::Print {
29 class PrintUtil {
30 public:
31 static std::string ParseListToString(const std::vector<std::string> &list);
32
33 static std::string SplitStr(const std::string& str, char delimiter, int index);
34
35 static std::string ToUpper(const std::string& str);
36
37 static bool CheckContains(const std::string& str, const std::string& content);
38 };
39
ParseListToString(const std::vector<std::string> & list)40 inline std::string PrintUtil::ParseListToString(const std::vector<std::string> &list)
41 {
42 std::string str;
43 if (!list.empty()) {
44 uint32_t count = 1;
45 uint32_t size = list.size();
46 for (auto val: list) {
47 str += val;
48 if (count < size) {
49 str += ",";
50 }
51 count ++;
52 }
53 }
54 return str;
55 }
56
SplitStr(const std::string & str,char delimiter,int index)57 inline std::string PrintUtil::SplitStr(const std::string& str, char delimiter, int index)
58 {
59 if (!str.empty()) {
60 std::string token;
61 std::istringstream tokenStream(str);
62 int count = 0;
63 while (std::getline(tokenStream, token, delimiter)) {
64 if (count == index) {
65 return token;
66 }
67 count ++;
68 }
69 }
70 return str;
71 }
72
ToUpper(const std::string & val)73 inline std::string PrintUtil::ToUpper(const std::string& val)
74 {
75 std::string str = val;
76 if (!str.empty()) {
77 std::transform(str.begin(), str.end(), str.begin(), toupper);
78 }
79 return str;
80 }
81
CheckContains(const std::string & str,const std::string & content)82 inline bool PrintUtil::CheckContains(const std::string& str, const std::string& content)
83 {
84 if (str.empty() || content.empty()) {
85 return false;
86 }
87
88 return str.find(content) != std::string::npos;
89 }
90 } // namespace OHOS::Print
91
92 #endif // PRINT_UTIL_H