• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef OHOS_HDI_STRING_H
10 #define OHOS_HDI_STRING_H
11 
12 #include <vector>
13 
14 namespace OHOS {
15 namespace HDI {
16 class String {
17 public:
String()18     inline String()
19     {}
20 
21     String(const char* string);
22 
23     String(const char* string, size_t length);
24 
25     String(const String& other);
26 
27     String(String && other);
28 
29     ~String();
30 
string()31     inline const char* string() const
32     {
33         return string_;
34     }
35 
36     inline operator const char* () const
37     {
38         return string_;
39     }
40 
IsNull()41     inline bool IsNull() const
42     {
43         return string_ == nullptr;
44     }
45 
IsEmpty()46     inline bool IsEmpty() const
47     {
48         return string_ == nullptr || string_[0] == '\0';
49     }
50 
51     int GetLength() const;
52 
53     char operator[](int index) const;
54 
55     bool Equals(const char* string) const;
56 
57     bool Equals(const String& other) const;
58 
59     int Compare(const String& other) const;
60 
61     int GetHashCode() const;
62 
63     int IndexOf(char c, int fromIndex = 0) const;
64 
65     int IndexOf(const char* string, int fromIndex = 0) const;
66 
67     int IndexOf(const String& other, int fromIndex = 0) const;
68 
69     int LastIndexOf(char c, int fromIndex = 0) const;
70 
71     int LastIndexOf(const char* string, int fromIndex = 0) const;
72 
73     int LastIndexOf(const String& other, int fromIndex = 0) const;
74 
75     bool StartsWith(const char* string) const;
76 
77     bool StartsWith(const String& other) const;
78 
79     bool EndsWith(char c) const;
80 
81     bool EndsWith(const char* string) const;
82 
83     bool EndsWith(const String& other) const;
84 
85     String ToLowerCase() const;
86 
87     String ToUpperCase() const;
88 
89     String Substring(int begin) const;
90 
91     String Substring(int begin, int end) const;
92 
93     String Replace(char oldChar, char newChar) const;
94 
95     String Replace(const char* target, const char* replacement) const;
96 
97     String Replace(const String& target, const String& replacement) const;
98 
99     String& Replace(int position, int len, const String& other);
100 
101     std::vector<String> Split(const String& separator) const;
102 
103     String& insert(int index, const String& other);
104 
105     String& operator=(const char* string);
106 
107     String& operator=(const String& other);
108 
109     String& operator=(String && other);
110 
111     String& operator+=(const char* string);
112 
113     String& operator+=(const String& other);
114 
115     static String Format(const char* format, ...);
116 
117     static const char* TAG;
118     static constexpr int MAX_SIZE = 262144; // 2^18
119 
120 private:
121     String(int size);
122 
123     int LastIndexOfInternal(const char* string, int fromIndex) const;
124 
125     char* string_ = nullptr;
126 };
127 
128 inline String operator+(const String& string1, const String& string2)
129 {
130     String newStr;
131     newStr += string1;
132     newStr += string2;
133     return newStr;
134 }
135 
136 struct StringHashFunc {
operatorStringHashFunc137     int operator()(const String& key) const
138     {
139         return key.GetHashCode();
140     }
141 };
142 
143 struct StringEqualFunc {
operatorStringEqualFunc144     bool operator()(const String& lhs, const String& rhs) const
145     {
146         return lhs.Equals(rhs);
147     }
148 };
149 } // namespace HDI
150 } // namespace OHOS
151 
152 #endif // OHOS_HDI_STRING_H