• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 KV_DATASERVICE_CONSTANT_H
17 #define KV_DATASERVICE_CONSTANT_H
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <algorithm>
22 #include <cctype>
23 #include <locale>
24 #include <string>
25 #include <vector>
26 #include "visibility.h"
27 
28 namespace OHOS {
29 namespace DistributedData {
30 class Constant {
31 public:
32     // concatenate strings and return a composition string.
33     API_EXPORT static std::string Concatenate(std::initializer_list<std::string> stringList);
34 
35     API_EXPORT static std::string Join(
36         const std::string &prefix, const std::string &separator, std::initializer_list<std::string> params);
37 
38     API_EXPORT static bool IsBackground(pid_t pid);
39 
40     API_EXPORT static bool Equal(bool first, bool second);
41 
42     API_EXPORT static bool NotEqual(bool first, bool second);
43 
44     // delete left bland in s by reference.
45     template<typename T>
46     static void LeftTrim(T &s);
47 
48     // delete right bland in s by reference.
49     template<typename T>
50     static void RightTrim(T &s);
51 
52     // delete both left and right bland in s by reference.
53     template<typename T>
54     static void Trim(T &s);
55 
56     // delete left bland in s by reference, not change raw string.
57     template<typename T>
58     static T LeftTrimCopy(T s);
59 
60     // delete right bland in s by reference, not change raw string.
61     template<typename T>
62     static T RightTrimCopy(T s);
63 
64     // delete both left and right bland in s by reference, not change raw string.
65     template<typename T>
66     static T TrimCopy(T s);
67 
68     API_EXPORT static constexpr const char *KEY_SEPARATOR = "###";
69 };
70 
71 // trim from start (in place)
72 template<typename T>
LeftTrim(T & s)73 void Constant::LeftTrim(T &s)
74 {
75     s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
76 }
77 
78 // trim from end (in place)
79 template<typename T>
RightTrim(T & s)80 void Constant::RightTrim(T &s)
81 {
82     s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
83 }
84 
85 // trim from both ends (in place)
86 template<typename T>
Trim(T & s)87 void Constant::Trim(T &s)
88 {
89     LeftTrim(s);
90     RightTrim(s);
91 }
92 
93 // trim from start (copying)
94 template<typename T>
LeftTrimCopy(T s)95 T Constant::LeftTrimCopy(T s)
96 {
97     LeftTrim(s);
98     return s;
99 }
100 
101 // trim from end (copying)
102 template<typename T>
RightTrimCopy(T s)103 T Constant::RightTrimCopy(T s)
104 {
105     RightTrim(s);
106     return s;
107 }
108 
109 // trim from both ends (copying)
110 template<typename T>
TrimCopy(T s)111 T Constant::TrimCopy(T s)
112 {
113     Trim(s);
114     return s;
115 }
116 }  // namespace DistributedKv
117 }  // namespace OHOS
118 #endif // KV_DATASERVICE_CONSTANT_H
119