1 /*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_STRING8_H
18 #define ANDROID_STRING8_H
19
20 #include <iostream>
21 #include <string>
22 #include <string_view>
23
24 #include <utils/Errors.h>
25 #include <utils/Unicode.h>
26 #include <utils/TypeHelpers.h>
27
28 #include <string.h> // for strcmp
29 #include <stdarg.h>
30
31 #if __cplusplus >= 202002L
32 #include <compare>
33 #endif
34
35 // ---------------------------------------------------------------------------
36
37 namespace android {
38
39 class String16;
40
41 // DO NOT USE: please use std::string
42
43 //! This is a string holding UTF-8 characters. Does not allow the value more
44 // than 0x10FFFF, which is not valid unicode codepoint.
45 class String8
46 {
47 public:
48 String8();
49 String8(const String8& o);
50 explicit String8(const char* o);
51 explicit String8(const char* o, size_t numChars);
52 explicit String8(std::string_view o);
53
54 explicit String8(const String16& o);
55 explicit String8(const char16_t* o);
56 explicit String8(const char16_t* o, size_t numChars);
57 explicit String8(const char32_t* o);
58 explicit String8(const char32_t* o, size_t numChars);
59 ~String8();
60
61 static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
62 static String8 formatV(const char* fmt, va_list args);
63
64 inline const char* c_str() const;
65
66 inline size_t size() const;
67 inline size_t bytes() const;
68 inline bool empty() const;
69
70 size_t length() const;
71
72 void clear();
73
74 void setTo(const String8& other);
75 status_t setTo(const char* other);
76 status_t setTo(const char* other, size_t numChars);
77 status_t setTo(const char16_t* other, size_t numChars);
78 status_t setTo(const char32_t* other,
79 size_t length);
80
81 status_t append(const String8& other);
82 status_t append(const char* other);
83 status_t append(const char* other, size_t numChars);
84
85 status_t appendFormat(const char* fmt, ...)
86 __attribute__((format (printf, 2, 3)));
87 status_t appendFormatV(const char* fmt, va_list args);
88
89 inline String8& operator=(const String8& other);
90 inline String8& operator=(const char* other);
91
92 inline String8& operator+=(const String8& other);
93 inline String8 operator+(const String8& other) const;
94
95 inline String8& operator+=(const char* other);
96 inline String8 operator+(const char* other) const;
97
98 inline int compare(const String8& other) const;
99
100 inline bool operator<(const String8& other) const;
101 inline bool operator<=(const String8& other) const;
102 inline bool operator==(const String8& other) const;
103 inline bool operator!=(const String8& other) const;
104 inline bool operator>=(const String8& other) const;
105 inline bool operator>(const String8& other) const;
106 #if __cplusplus >= 202002L
107 inline std::strong_ordering operator<=>(const String8& other) const;
108 #endif
109
110 inline bool operator<(const char* other) const;
111 inline bool operator<=(const char* other) const;
112 inline bool operator==(const char* other) const;
113 inline bool operator!=(const char* other) const;
114 inline bool operator>=(const char* other) const;
115 inline bool operator>(const char* other) const;
116 #if __cplusplus >= 202002L
117 inline std::strong_ordering operator<=>(const char* other) const;
118 #endif
119
120 inline operator const char*() const;
121
122 inline explicit operator std::string_view() const;
123
124 char* lockBuffer(size_t size);
125 void unlockBuffer();
126 status_t unlockBuffer(size_t size);
127
128 // return the index of the first byte of other in this at or after
129 // start, or -1 if not found
130 ssize_t find(const char* other, size_t start = 0) const;
131 inline ssize_t find(const String8& other, size_t start = 0) const;
132
133 // return true if this string contains the specified substring
134 inline bool contains(const char* other) const;
135 inline bool contains(const String8& other) const;
136
137 // removes all occurrence of the specified substring
138 // returns true if any were found and removed
139 bool removeAll(const char* other);
140 inline bool removeAll(const String8& other);
141
142 void toLower();
143
144 private:
145 String8 getPathDir(void) const;
146 String8 getPathExtension(void) const;
147
148 status_t real_append(const char* other, size_t numChars);
149
150 const char* mString;
151
152 // These symbols are for potential backward compatibility with prebuilts. To be removed.
153 #ifdef ENABLE_STRING8_OBSOLETE_METHODS
154 public:
155 #else
156 private:
157 #endif
158 inline const char* string() const;
159 inline bool isEmpty() const;
160 };
161
162 // String8 can be trivially moved using memcpy() because moving does not
163 // require any change to the underlying SharedBuffer contents or reference count.
164 ANDROID_TRIVIAL_MOVE_TRAIT(String8)
165
166 static inline std::ostream& operator<<(std::ostream& os, const String8& str) {
167 os << str.c_str();
168 return os;
169 }
170
171 // ---------------------------------------------------------------------------
172 // No user servicable parts below.
173
compare_type(const String8 & lhs,const String8 & rhs)174 inline int compare_type(const String8& lhs, const String8& rhs)
175 {
176 return lhs.compare(rhs);
177 }
178
strictly_order_type(const String8 & lhs,const String8 & rhs)179 inline int strictly_order_type(const String8& lhs, const String8& rhs)
180 {
181 return compare_type(lhs, rhs) < 0;
182 }
183
c_str()184 inline const char* String8::c_str() const
185 {
186 return mString;
187 }
string()188 inline const char* String8::string() const
189 {
190 return mString;
191 }
192
size()193 inline size_t String8::size() const
194 {
195 return length();
196 }
197
empty()198 inline bool String8::empty() const
199 {
200 return length() == 0;
201 }
202
isEmpty()203 inline bool String8::isEmpty() const
204 {
205 return length() == 0;
206 }
207
bytes()208 inline size_t String8::bytes() const
209 {
210 return length();
211 }
212
find(const String8 & other,size_t start)213 inline ssize_t String8::find(const String8& other, size_t start) const
214 {
215 return find(other.c_str(), start);
216 }
217
contains(const char * other)218 inline bool String8::contains(const char* other) const
219 {
220 return find(other) >= 0;
221 }
222
contains(const String8 & other)223 inline bool String8::contains(const String8& other) const
224 {
225 return contains(other.c_str());
226 }
227
removeAll(const String8 & other)228 inline bool String8::removeAll(const String8& other)
229 {
230 return removeAll(other.c_str());
231 }
232
233 inline String8& String8::operator=(const String8& other)
234 {
235 setTo(other);
236 return *this;
237 }
238
239 inline String8& String8::operator=(const char* other)
240 {
241 setTo(other);
242 return *this;
243 }
244
245 inline String8& String8::operator+=(const String8& other)
246 {
247 append(other);
248 return *this;
249 }
250
251 inline String8 String8::operator+(const String8& other) const
252 {
253 String8 tmp(*this);
254 tmp += other;
255 return tmp;
256 }
257
258 inline String8& String8::operator+=(const char* other)
259 {
260 append(other);
261 return *this;
262 }
263
264 inline String8 String8::operator+(const char* other) const
265 {
266 String8 tmp(*this);
267 tmp += other;
268 return tmp;
269 }
270
compare(const String8 & other)271 inline int String8::compare(const String8& other) const
272 {
273 return strcmp(mString, other.mString);
274 }
275
276 inline bool String8::operator<(const String8& other) const
277 {
278 return strcmp(mString, other.mString) < 0;
279 }
280
281 inline bool String8::operator<=(const String8& other) const
282 {
283 return strcmp(mString, other.mString) <= 0;
284 }
285
286 inline bool String8::operator==(const String8& other) const
287 {
288 return strcmp(mString, other.mString) == 0;
289 }
290
291 inline bool String8::operator!=(const String8& other) const
292 {
293 return strcmp(mString, other.mString) != 0;
294 }
295
296 inline bool String8::operator>=(const String8& other) const
297 {
298 return strcmp(mString, other.mString) >= 0;
299 }
300
301 inline bool String8::operator>(const String8& other) const
302 {
303 return strcmp(mString, other.mString) > 0;
304 }
305
306 #if __cplusplus >= 202002L
307 inline std::strong_ordering String8::operator<=>(const String8& other) const {
308 int result = strcmp(mString, other.mString);
309 if (result == 0) {
310 return std::strong_ordering::equal;
311 } else if (result < 0) {
312 return std::strong_ordering::less;
313 } else {
314 return std::strong_ordering::greater;
315 }
316 }
317 #endif
318
319 inline bool String8::operator<(const char* other) const
320 {
321 return strcmp(mString, other) < 0;
322 }
323
324 inline bool String8::operator<=(const char* other) const
325 {
326 return strcmp(mString, other) <= 0;
327 }
328
329 inline bool String8::operator==(const char* other) const
330 {
331 return strcmp(mString, other) == 0;
332 }
333
334 inline bool String8::operator!=(const char* other) const
335 {
336 return strcmp(mString, other) != 0;
337 }
338
339 inline bool String8::operator>=(const char* other) const
340 {
341 return strcmp(mString, other) >= 0;
342 }
343
344 inline bool String8::operator>(const char* other) const
345 {
346 return strcmp(mString, other) > 0;
347 }
348
349 #if __cplusplus >= 202002L
350 inline std::strong_ordering String8::operator<=>(const char* other) const {
351 int result = strcmp(mString, other);
352 if (result == 0) {
353 return std::strong_ordering::equal;
354 } else if (result < 0) {
355 return std::strong_ordering::less;
356 } else {
357 return std::strong_ordering::greater;
358 }
359 }
360 #endif
361
362 inline String8::operator const char*() const
363 {
364 return mString;
365 }
366
String8(std::string_view o)367 inline String8::String8(std::string_view o) : String8(o.data(), o.length()) { }
368
string_view()369 inline String8::operator std::string_view() const
370 {
371 return {mString, length()};
372 }
373
374 } // namespace android
375
376 // ---------------------------------------------------------------------------
377
378 #endif // ANDROID_STRING8_H
379