blob: 1d12994d1cfb238f6c7308bb0ed207ed22170e4c [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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067 inline const char* string() const;
Samuel Tan9ac4e002016-02-16 14:20:05 -080068 static inline std::string std_string(const String8& str);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069 inline size_t size() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 inline size_t bytes() const;
Jeff Brown48da31b2010-09-12 17:55:08 -070071 inline bool isEmpty() const;
Samuel Tan95fd5272016-02-18 16:56:21 -080072
Sergio Girod2529f22015-09-23 16:22:59 +010073 size_t length() const;
Samuel Tan95fd5272016-02-18 16:56:21 -080074
Jeff Brown48da31b2010-09-12 17:55:08 -070075 void clear();
76
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 void setTo(const String8& other);
78 status_t setTo(const char* other);
79 status_t setTo(const char* other, size_t numChars);
80 status_t setTo(const char16_t* other, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090081 status_t setTo(const char32_t* other,
82 size_t length);
83
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080084 status_t append(const String8& other);
85 status_t append(const char* other);
86 status_t append(const char* other, size_t numChars);
87
Jeff Brown38fb25b2010-08-11 14:46:32 -070088 status_t appendFormat(const char* fmt, ...)
89 __attribute__((format (printf, 2, 3)));
Jeff Brown647925d2010-11-10 16:03:06 -080090 status_t appendFormatV(const char* fmt, va_list args);
Jeff Brown35a154e2010-07-15 23:54:05 -070091
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090092 // Note that this function takes O(N) time to calculate the value.
93 // No cache value is stored.
94 size_t getUtf32Length() const;
95 int32_t getUtf32At(size_t index,
96 size_t *next_index) const;
Kenny Rootba0165b2010-11-09 14:37:23 -080097 void getUtf32(char32_t* dst) const;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090098
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080099 inline String8& operator=(const String8& other);
100 inline String8& operator=(const char* other);
Samuel Tan95fd5272016-02-18 16:56:21 -0800101
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800102 inline String8& operator+=(const String8& other);
103 inline String8 operator+(const String8& other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800104
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800105 inline String8& operator+=(const char* other);
106 inline String8 operator+(const char* other) const;
107
108 inline int compare(const String8& other) const;
109
110 inline bool operator<(const String8& other) const;
111 inline bool operator<=(const String8& other) const;
112 inline bool operator==(const String8& other) const;
113 inline bool operator!=(const String8& other) const;
114 inline bool operator>=(const String8& other) const;
115 inline bool operator>(const String8& other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800116
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800117 inline bool operator<(const char* other) const;
118 inline bool operator<=(const char* other) const;
119 inline bool operator==(const char* other) const;
120 inline bool operator!=(const char* other) const;
121 inline bool operator>=(const char* other) const;
122 inline bool operator>(const char* other) const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800123
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800124 inline operator const char*() const;
Samuel Tan95fd5272016-02-18 16:56:21 -0800125
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800126 char* lockBuffer(size_t size);
127 void unlockBuffer();
128 status_t unlockBuffer(size_t size);
Samuel Tan95fd5272016-02-18 16:56:21 -0800129
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800130 // return the index of the first byte of other in this at or after
131 // start, or -1 if not found
132 ssize_t find(const char* other, size_t start = 0) const;
133
Jeff Brown5ee915a2014-06-06 19:30:15 -0700134 // return true if this string contains the specified substring
135 inline bool contains(const char* 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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141 void toLower();
142 void toLower(size_t start, size_t numChars);
143 void toUpper();
144 void toUpper(size_t start, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900145
Jeff Brown5ee915a2014-06-06 19:30:15 -0700146
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800147 /*
148 * These methods operate on the string as if it were a path name.
149 */
150
151 /*
152 * Set the filename field to a specific value.
153 *
154 * Normalizes the filename, removing a trailing '/' if present.
155 */
156 void setPathName(const char* name);
157 void setPathName(const char* name, size_t numChars);
158
159 /*
160 * Get just the filename component.
161 *
162 * "/tmp/foo/bar.c" --> "bar.c"
163 */
164 String8 getPathLeaf(void) const;
165
166 /*
167 * Remove the last (file name) component, leaving just the directory
168 * name.
169 *
170 * "/tmp/foo/bar.c" --> "/tmp/foo"
171 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
172 * "bar.c" --> ""
173 */
174 String8 getPathDir(void) const;
175
176 /*
177 * Retrieve the front (root dir) component. Optionally also return the
178 * remaining components.
179 *
180 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
181 * "/tmp" --> "tmp" (remain = "")
182 * "bar.c" --> "bar.c" (remain = "")
183 */
184 String8 walkPath(String8* outRemains = NULL) const;
185
186 /*
Glenn Kasten29d4d202011-03-14 11:32:29 -0700187 * Return the filename extension. This is the last '.' and any number
188 * of characters that follow it. The '.' is included in case we
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800189 * decide to expand our definition of what constitutes an extension.
190 *
191 * "/tmp/foo/bar.c" --> ".c"
192 * "/tmp" --> ""
193 * "/tmp/foo.bar/baz" --> ""
194 * "foo.jpeg" --> ".jpeg"
195 * "foo." --> ""
196 */
197 String8 getPathExtension(void) const;
198
199 /*
200 * Return the path without the extension. Rules for what constitutes
201 * an extension are described in the comment for getPathExtension().
202 *
203 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
204 */
205 String8 getBasePath(void) const;
206
207 /*
208 * Add a component to the pathname. We guarantee that there is
209 * exactly one path separator between the old path and the new.
210 * If there is no existing name, we just copy the new name in.
211 *
212 * If leaf is a fully qualified path (i.e. starts with '/', it
213 * replaces whatever was there before.
214 */
215 String8& appendPath(const char* leaf);
216 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
217
218 /*
219 * Like appendPath(), but does not affect this string. Returns a new one instead.
220 */
221 String8 appendPathCopy(const char* leaf) const
222 { String8 p(*this); p.appendPath(leaf); return p; }
223 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
224
225 /*
226 * Converts all separators in this string to /, the default path separator.
227 *
228 * If the default OS separator is backslash, this converts all
229 * backslashes to slashes, in-place. Otherwise it does nothing.
230 * Returns self.
231 */
232 String8& convertToResPath();
233
234private:
235 status_t real_append(const char* other, size_t numChars);
236 char* find_extension(void) const;
237
238 const char* mString;
239};
240
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700241// String8 can be trivially moved using memcpy() because moving does not
242// require any change to the underlying SharedBuffer contents or reference count.
243ANDROID_TRIVIAL_MOVE_TRAIT(String8)
244
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800245// ---------------------------------------------------------------------------
246// No user servicable parts below.
247
248inline int compare_type(const String8& lhs, const String8& rhs)
249{
250 return lhs.compare(rhs);
251}
252
253inline int strictly_order_type(const String8& lhs, const String8& rhs)
254{
255 return compare_type(lhs, rhs) < 0;
256}
257
Jeff Brown1d618d62010-12-02 13:50:46 -0800258inline const String8 String8::empty() {
259 return String8();
260}
261
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800262inline const char* String8::string() const
263{
264 return mString;
265}
266
Samuel Tan9ac4e002016-02-16 14:20:05 -0800267inline std::string String8::std_string(const String8& str)
268{
269 return std::string(str.string());
270}
271
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800272inline size_t String8::size() const
273{
274 return length();
275}
276
Jeff Brown48da31b2010-09-12 17:55:08 -0700277inline bool String8::isEmpty() const
278{
279 return length() == 0;
280}
281
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800282inline size_t String8::bytes() const
283{
Sergio Girod2529f22015-09-23 16:22:59 +0100284 return length();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800285}
286
Jeff Brown5ee915a2014-06-06 19:30:15 -0700287inline bool String8::contains(const char* other) const
288{
289 return find(other) >= 0;
290}
291
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800292inline String8& String8::operator=(const String8& other)
293{
294 setTo(other);
295 return *this;
296}
297
298inline String8& String8::operator=(const char* other)
299{
300 setTo(other);
301 return *this;
302}
303
304inline String8& String8::operator+=(const String8& other)
305{
306 append(other);
307 return *this;
308}
309
310inline String8 String8::operator+(const String8& other) const
311{
Kenny Root23b4a092010-08-05 16:21:23 -0700312 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800313 tmp += other;
314 return tmp;
315}
316
317inline String8& String8::operator+=(const char* other)
318{
319 append(other);
320 return *this;
321}
322
323inline String8 String8::operator+(const char* other) const
324{
Kenny Root23b4a092010-08-05 16:21:23 -0700325 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800326 tmp += other;
327 return tmp;
328}
329
330inline int String8::compare(const String8& other) const
331{
332 return strcmp(mString, other.mString);
333}
334
335inline bool String8::operator<(const String8& other) const
336{
337 return strcmp(mString, other.mString) < 0;
338}
339
340inline bool String8::operator<=(const String8& other) const
341{
342 return strcmp(mString, other.mString) <= 0;
343}
344
345inline bool String8::operator==(const String8& other) const
346{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700347 return strcmp(mString, other.mString) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800348}
349
350inline bool String8::operator!=(const String8& other) const
351{
352 return strcmp(mString, other.mString) != 0;
353}
354
355inline bool String8::operator>=(const String8& other) const
356{
357 return strcmp(mString, other.mString) >= 0;
358}
359
360inline bool String8::operator>(const String8& other) const
361{
362 return strcmp(mString, other.mString) > 0;
363}
364
365inline bool String8::operator<(const char* other) const
366{
367 return strcmp(mString, other) < 0;
368}
369
370inline bool String8::operator<=(const char* other) const
371{
372 return strcmp(mString, other) <= 0;
373}
374
375inline bool String8::operator==(const char* other) const
376{
377 return strcmp(mString, other) == 0;
378}
379
380inline bool String8::operator!=(const char* other) const
381{
382 return strcmp(mString, other) != 0;
383}
384
385inline bool String8::operator>=(const char* other) const
386{
387 return strcmp(mString, other) >= 0;
388}
389
390inline bool String8::operator>(const char* other) const
391{
392 return strcmp(mString, other) > 0;
393}
394
395inline String8::operator const char*() const
396{
397 return mString;
398}
399
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900400} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800401
402// ---------------------------------------------------------------------------
403
404#endif // ANDROID_STRING8_H