blob: 1f3e5d8658d4ece8af19817bab1e06c5e3dc785d [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
Samuel Tan9ac4e002016-02-16 14:20:05 -080020#include <string> // for std::string
21
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <utils/Errors.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080023#include <utils/Unicode.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070024#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
Kenny Rootba0165b2010-11-09 14:37:23 -080026#include <string.h> // for strcmp
Jeff Brown647925d2010-11-10 16:03:06 -080027#include <stdarg.h>
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090028
29// ---------------------------------------------------------------------------
30
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080031namespace android {
32
Kenny Rootba0165b2010-11-09 14:37:23 -080033class String16;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034class TextOutput;
35
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090036//! This is a string holding UTF-8 characters. Does not allow the value more
37// than 0x10FFFF, which is not valid unicode codepoint.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038class String8
39{
40public:
Mathias Agopian4485d0d2013-05-08 16:04:13 -070041 /* use String8(StaticLinkage) if you're statically linking against
42 * libutils and declaring an empty static String8, e.g.:
43 *
44 * static String8 sAStaticEmptyString(String8::kEmptyString);
45 * static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
46 */
47 enum StaticLinkage { kEmptyString };
48
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049 String8();
Mathias Agopian4485d0d2013-05-08 16:04:13 -070050 explicit String8(StaticLinkage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051 String8(const String8& o);
52 explicit String8(const char* o);
53 explicit String8(const char* o, size_t numChars);
Samuel Tan95fd5272016-02-18 16:56:21 -080054
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 explicit String8(const String16& o);
56 explicit String8(const char16_t* o);
57 explicit String8(const char16_t* o, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090058 explicit String8(const char32_t* o);
59 explicit String8(const char32_t* o, size_t numChars);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080060 ~String8();
Jeff Brown1d618d62010-12-02 13:50:46 -080061
62 static inline const String8 empty();
63
64 static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
65 static String8 formatV(const char* fmt, va_list args);
66
Steven Moreland2aac3352017-03-10 22:31:08 -080067 inline const char* c_str() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 inline const char* string() const;
Steven Moreland2aac3352017-03-10 22:31:08 -080069
Steven Moreland2aac3352017-03-10 22:31:08 -080070private:
Samuel Tan9ac4e002016-02-16 14:20:05 -080071 static inline std::string std_string(const String8& str);
Steven Moreland2aac3352017-03-10 22:31:08 -080072public:
73
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 inline size_t size() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 inline size_t bytes() const;
Jeff Brown48da31b2010-09-12 17:55:08 -070076 inline bool isEmpty() const;
Samuel Tan95fd5272016-02-18 16:56:21 -080077
Sergio Girod2529f22015-09-23 16:22:59 +010078 size_t length() const;
Samuel Tan95fd5272016-02-18 16:56:21 -080079
Jeff Brown48da31b2010-09-12 17:55:08 -070080 void clear();
81
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080082 void setTo(const String8& other);
83 status_t setTo(const char* other);
84 status_t setTo(const char* other, size_t numChars);
85 status_t setTo(const char16_t* other, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090086 status_t setTo(const char32_t* other,
87 size_t length);
88
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080089 status_t append(const String8& other);
90 status_t append(const char* other);
91 status_t append(const char* other, size_t numChars);
92
Jeff Brown38fb25b2010-08-11 14:46:32 -070093 status_t appendFormat(const char* fmt, ...)
94 __attribute__((format (printf, 2, 3)));
Jeff Brown647925d2010-11-10 16:03:06 -080095 status_t appendFormatV(const char* fmt, va_list args);
Jeff Brown35a154e2010-07-15 23:54:05 -070096
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090097 // Note that this function takes O(N) time to calculate the value.
98 // No cache value is stored.
99 size_t getUtf32Length() const;
100 int32_t getUtf32At(size_t index,
101 size_t *next_index) const;
Kenny Rootba0165b2010-11-09 14:37:23 -0800102 void getUtf32(char32_t* dst) const;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900103
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800104 inline String8& operator=(const String8& other);
105 inline String8& operator=(const char* other);
Samuel Tan95fd5272016-02-18 16:56:21 -0800106
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800107 inline String8& operator+=(const String8& other);
108 inline String8 operator+(const String8& other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800109
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800110 inline String8& operator+=(const char* other);
111 inline String8 operator+(const char* other) const;
112
113 inline int compare(const String8& other) const;
114
115 inline bool operator<(const String8& other) const;
116 inline bool operator<=(const String8& other) const;
117 inline bool operator==(const String8& other) const;
118 inline bool operator!=(const String8& other) const;
119 inline bool operator>=(const String8& other) const;
120 inline bool operator>(const String8& other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800121
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800122 inline bool operator<(const char* other) const;
123 inline bool operator<=(const char* other) const;
124 inline bool operator==(const char* other) const;
125 inline bool operator!=(const char* other) const;
126 inline bool operator>=(const char* other) const;
127 inline bool operator>(const char* other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800128
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800129 inline operator const char*() const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800130
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 char* lockBuffer(size_t size);
132 void unlockBuffer();
133 status_t unlockBuffer(size_t size);
Samuel Tan95fd5272016-02-18 16:56:21 -0800134
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800135 // return the index of the first byte of other in this at or after
136 // start, or -1 if not found
137 ssize_t find(const char* other, size_t start = 0) const;
138
Jeff Brown5ee915a2014-06-06 19:30:15 -0700139 // return true if this string contains the specified substring
140 inline bool contains(const char* other) const;
141
142 // removes all occurrence of the specified substring
143 // returns true if any were found and removed
144 bool removeAll(const char* other);
145
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800146 void toLower();
147 void toLower(size_t start, size_t numChars);
148 void toUpper();
149 void toUpper(size_t start, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900150
Jeff Brown5ee915a2014-06-06 19:30:15 -0700151
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800152 /*
153 * These methods operate on the string as if it were a path name.
154 */
155
156 /*
157 * Set the filename field to a specific value.
158 *
159 * Normalizes the filename, removing a trailing '/' if present.
160 */
161 void setPathName(const char* name);
162 void setPathName(const char* name, size_t numChars);
163
164 /*
165 * Get just the filename component.
166 *
167 * "/tmp/foo/bar.c" --> "bar.c"
168 */
169 String8 getPathLeaf(void) const;
170
171 /*
172 * Remove the last (file name) component, leaving just the directory
173 * name.
174 *
175 * "/tmp/foo/bar.c" --> "/tmp/foo"
176 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
177 * "bar.c" --> ""
178 */
179 String8 getPathDir(void) const;
180
181 /*
182 * Retrieve the front (root dir) component. Optionally also return the
183 * remaining components.
184 *
185 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
186 * "/tmp" --> "tmp" (remain = "")
187 * "bar.c" --> "bar.c" (remain = "")
188 */
189 String8 walkPath(String8* outRemains = NULL) const;
190
191 /*
Glenn Kasten29d4d202011-03-14 11:32:29 -0700192 * Return the filename extension. This is the last '.' and any number
193 * of characters that follow it. The '.' is included in case we
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800194 * decide to expand our definition of what constitutes an extension.
195 *
196 * "/tmp/foo/bar.c" --> ".c"
197 * "/tmp" --> ""
198 * "/tmp/foo.bar/baz" --> ""
199 * "foo.jpeg" --> ".jpeg"
200 * "foo." --> ""
201 */
202 String8 getPathExtension(void) const;
203
204 /*
205 * Return the path without the extension. Rules for what constitutes
206 * an extension are described in the comment for getPathExtension().
207 *
208 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
209 */
210 String8 getBasePath(void) const;
211
212 /*
213 * Add a component to the pathname. We guarantee that there is
214 * exactly one path separator between the old path and the new.
215 * If there is no existing name, we just copy the new name in.
216 *
217 * If leaf is a fully qualified path (i.e. starts with '/', it
218 * replaces whatever was there before.
219 */
220 String8& appendPath(const char* leaf);
221 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
222
223 /*
224 * Like appendPath(), but does not affect this string. Returns a new one instead.
225 */
226 String8 appendPathCopy(const char* leaf) const
227 { String8 p(*this); p.appendPath(leaf); return p; }
228 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
229
230 /*
231 * Converts all separators in this string to /, the default path separator.
232 *
233 * If the default OS separator is backslash, this converts all
234 * backslashes to slashes, in-place. Otherwise it does nothing.
235 * Returns self.
236 */
237 String8& convertToResPath();
238
239private:
240 status_t real_append(const char* other, size_t numChars);
241 char* find_extension(void) const;
242
243 const char* mString;
244};
245
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700246// String8 can be trivially moved using memcpy() because moving does not
247// require any change to the underlying SharedBuffer contents or reference count.
248ANDROID_TRIVIAL_MOVE_TRAIT(String8)
249
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800250// ---------------------------------------------------------------------------
251// No user servicable parts below.
252
253inline int compare_type(const String8& lhs, const String8& rhs)
254{
255 return lhs.compare(rhs);
256}
257
258inline int strictly_order_type(const String8& lhs, const String8& rhs)
259{
260 return compare_type(lhs, rhs) < 0;
261}
262
Jeff Brown1d618d62010-12-02 13:50:46 -0800263inline const String8 String8::empty() {
264 return String8();
265}
266
Steven Moreland2aac3352017-03-10 22:31:08 -0800267inline const char* String8::c_str() const
268{
269 return mString;
270}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800271inline const char* String8::string() const
272{
273 return mString;
274}
275
Samuel Tan9ac4e002016-02-16 14:20:05 -0800276inline std::string String8::std_string(const String8& str)
277{
278 return std::string(str.string());
279}
280
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800281inline size_t String8::size() const
282{
283 return length();
284}
285
Jeff Brown48da31b2010-09-12 17:55:08 -0700286inline bool String8::isEmpty() const
287{
288 return length() == 0;
289}
290
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800291inline size_t String8::bytes() const
292{
Sergio Girod2529f22015-09-23 16:22:59 +0100293 return length();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800294}
295
Jeff Brown5ee915a2014-06-06 19:30:15 -0700296inline bool String8::contains(const char* other) const
297{
298 return find(other) >= 0;
299}
300
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800301inline String8& String8::operator=(const String8& other)
302{
303 setTo(other);
304 return *this;
305}
306
307inline String8& String8::operator=(const char* other)
308{
309 setTo(other);
310 return *this;
311}
312
313inline String8& String8::operator+=(const String8& other)
314{
315 append(other);
316 return *this;
317}
318
319inline String8 String8::operator+(const String8& other) const
320{
Kenny Root23b4a092010-08-05 16:21:23 -0700321 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800322 tmp += other;
323 return tmp;
324}
325
326inline String8& String8::operator+=(const char* other)
327{
328 append(other);
329 return *this;
330}
331
332inline String8 String8::operator+(const char* other) const
333{
Kenny Root23b4a092010-08-05 16:21:23 -0700334 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800335 tmp += other;
336 return tmp;
337}
338
339inline int String8::compare(const String8& other) const
340{
341 return strcmp(mString, other.mString);
342}
343
344inline bool String8::operator<(const String8& other) const
345{
346 return strcmp(mString, other.mString) < 0;
347}
348
349inline bool String8::operator<=(const String8& other) const
350{
351 return strcmp(mString, other.mString) <= 0;
352}
353
354inline bool String8::operator==(const String8& other) const
355{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700356 return strcmp(mString, other.mString) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800357}
358
359inline bool String8::operator!=(const String8& other) const
360{
361 return strcmp(mString, other.mString) != 0;
362}
363
364inline bool String8::operator>=(const String8& other) const
365{
366 return strcmp(mString, other.mString) >= 0;
367}
368
369inline bool String8::operator>(const String8& other) const
370{
371 return strcmp(mString, other.mString) > 0;
372}
373
374inline bool String8::operator<(const char* other) const
375{
376 return strcmp(mString, other) < 0;
377}
378
379inline bool String8::operator<=(const char* other) const
380{
381 return strcmp(mString, other) <= 0;
382}
383
384inline bool String8::operator==(const char* other) const
385{
386 return strcmp(mString, other) == 0;
387}
388
389inline bool String8::operator!=(const char* other) const
390{
391 return strcmp(mString, other) != 0;
392}
393
394inline bool String8::operator>=(const char* other) const
395{
396 return strcmp(mString, other) >= 0;
397}
398
399inline bool String8::operator>(const char* other) const
400{
401 return strcmp(mString, other) > 0;
402}
403
404inline String8::operator const char*() const
405{
406 return mString;
407}
408
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900409} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800410
411// ---------------------------------------------------------------------------
412
413#endif // ANDROID_STRING8_H