blob: 2a75b988688f4adde88be5cb70ab718a1ee7a6a7 [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
20#include <utils/Errors.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080021#include <utils/Unicode.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070022#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023
Kenny Rootba0165b2010-11-09 14:37:23 -080024#include <string.h> // for strcmp
Jeff Brown647925d2010-11-10 16:03:06 -080025#include <stdarg.h>
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090026
27// ---------------------------------------------------------------------------
28
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029namespace android {
30
Kenny Rootba0165b2010-11-09 14:37:23 -080031class String16;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032class TextOutput;
33
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090034//! This is a string holding UTF-8 characters. Does not allow the value more
35// than 0x10FFFF, which is not valid unicode codepoint.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036class String8
37{
38public:
Mathias Agopian4485d0d2013-05-08 16:04:13 -070039 /* use String8(StaticLinkage) if you're statically linking against
40 * libutils and declaring an empty static String8, e.g.:
41 *
42 * static String8 sAStaticEmptyString(String8::kEmptyString);
43 * static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
44 */
45 enum StaticLinkage { kEmptyString };
46
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080047 String8();
Mathias Agopian4485d0d2013-05-08 16:04:13 -070048 explicit String8(StaticLinkage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049 String8(const String8& o);
50 explicit String8(const char* o);
51 explicit String8(const char* o, size_t numChars);
52
53 explicit String8(const String16& o);
54 explicit String8(const char16_t* o);
55 explicit String8(const char16_t* o, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090056 explicit String8(const char32_t* o);
57 explicit String8(const char32_t* o, size_t numChars);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080058 ~String8();
Jeff Brown1d618d62010-12-02 13:50:46 -080059
60 static inline const String8 empty();
61
62 static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
63 static String8 formatV(const char* fmt, va_list args);
64
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 inline const char* string() const;
66 inline size_t size() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067 inline size_t bytes() const;
Jeff Brown48da31b2010-09-12 17:55:08 -070068 inline bool isEmpty() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069
Sergio Girod2529f22015-09-23 16:22:59 +010070 size_t length() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080071
Jeff Brown48da31b2010-09-12 17:55:08 -070072 void clear();
73
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 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);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090078 status_t setTo(const char32_t* other,
79 size_t length);
80
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080081 status_t append(const String8& other);
82 status_t append(const char* other);
83 status_t append(const char* other, size_t numChars);
84
Jeff Brown38fb25b2010-08-11 14:46:32 -070085 status_t appendFormat(const char* fmt, ...)
86 __attribute__((format (printf, 2, 3)));
Jeff Brown647925d2010-11-10 16:03:06 -080087 status_t appendFormatV(const char* fmt, va_list args);
Jeff Brown35a154e2010-07-15 23:54:05 -070088
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090089 // Note that this function takes O(N) time to calculate the value.
90 // No cache value is stored.
91 size_t getUtf32Length() const;
92 int32_t getUtf32At(size_t index,
93 size_t *next_index) const;
Kenny Rootba0165b2010-11-09 14:37:23 -080094 void getUtf32(char32_t* dst) const;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090095
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080096 inline String8& operator=(const String8& other);
97 inline String8& operator=(const char* other);
98
99 inline String8& operator+=(const String8& other);
100 inline String8 operator+(const String8& other) const;
101
102 inline String8& operator+=(const char* other);
103 inline String8 operator+(const char* other) const;
104
105 inline int compare(const String8& other) const;
106
107 inline bool operator<(const String8& other) const;
108 inline bool operator<=(const String8& other) const;
109 inline bool operator==(const String8& other) const;
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
114 inline bool operator<(const char* other) const;
115 inline bool operator<=(const char* other) const;
116 inline bool operator==(const char* other) const;
117 inline bool operator!=(const char* other) const;
118 inline bool operator>=(const char* other) const;
119 inline bool operator>(const char* other) const;
120
121 inline operator const char*() const;
122
123 char* lockBuffer(size_t size);
124 void unlockBuffer();
125 status_t unlockBuffer(size_t size);
126
127 // return the index of the first byte of other in this at or after
128 // start, or -1 if not found
129 ssize_t find(const char* other, size_t start = 0) const;
130
Jeff Brown5ee915a2014-06-06 19:30:15 -0700131 // return true if this string contains the specified substring
132 inline bool contains(const char* other) const;
133
134 // removes all occurrence of the specified substring
135 // returns true if any were found and removed
136 bool removeAll(const char* other);
137
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 void toLower();
139 void toLower(size_t start, size_t numChars);
140 void toUpper();
141 void toUpper(size_t start, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900142
Jeff Brown5ee915a2014-06-06 19:30:15 -0700143
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800144 /*
145 * These methods operate on the string as if it were a path name.
146 */
147
148 /*
149 * Set the filename field to a specific value.
150 *
151 * Normalizes the filename, removing a trailing '/' if present.
152 */
153 void setPathName(const char* name);
154 void setPathName(const char* name, size_t numChars);
155
156 /*
157 * Get just the filename component.
158 *
159 * "/tmp/foo/bar.c" --> "bar.c"
160 */
161 String8 getPathLeaf(void) const;
162
163 /*
164 * Remove the last (file name) component, leaving just the directory
165 * name.
166 *
167 * "/tmp/foo/bar.c" --> "/tmp/foo"
168 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
169 * "bar.c" --> ""
170 */
171 String8 getPathDir(void) const;
172
173 /*
174 * Retrieve the front (root dir) component. Optionally also return the
175 * remaining components.
176 *
177 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
178 * "/tmp" --> "tmp" (remain = "")
179 * "bar.c" --> "bar.c" (remain = "")
180 */
181 String8 walkPath(String8* outRemains = NULL) const;
182
183 /*
Glenn Kasten29d4d202011-03-14 11:32:29 -0700184 * Return the filename extension. This is the last '.' and any number
185 * of characters that follow it. The '.' is included in case we
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800186 * decide to expand our definition of what constitutes an extension.
187 *
188 * "/tmp/foo/bar.c" --> ".c"
189 * "/tmp" --> ""
190 * "/tmp/foo.bar/baz" --> ""
191 * "foo.jpeg" --> ".jpeg"
192 * "foo." --> ""
193 */
194 String8 getPathExtension(void) const;
195
196 /*
197 * Return the path without the extension. Rules for what constitutes
198 * an extension are described in the comment for getPathExtension().
199 *
200 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
201 */
202 String8 getBasePath(void) const;
203
204 /*
205 * Add a component to the pathname. We guarantee that there is
206 * exactly one path separator between the old path and the new.
207 * If there is no existing name, we just copy the new name in.
208 *
209 * If leaf is a fully qualified path (i.e. starts with '/', it
210 * replaces whatever was there before.
211 */
212 String8& appendPath(const char* leaf);
213 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
214
215 /*
216 * Like appendPath(), but does not affect this string. Returns a new one instead.
217 */
218 String8 appendPathCopy(const char* leaf) const
219 { String8 p(*this); p.appendPath(leaf); return p; }
220 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
221
222 /*
223 * Converts all separators in this string to /, the default path separator.
224 *
225 * If the default OS separator is backslash, this converts all
226 * backslashes to slashes, in-place. Otherwise it does nothing.
227 * Returns self.
228 */
229 String8& convertToResPath();
230
231private:
232 status_t real_append(const char* other, size_t numChars);
233 char* find_extension(void) const;
234
235 const char* mString;
236};
237
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700238// String8 can be trivially moved using memcpy() because moving does not
239// require any change to the underlying SharedBuffer contents or reference count.
240ANDROID_TRIVIAL_MOVE_TRAIT(String8)
241
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800242// ---------------------------------------------------------------------------
243// No user servicable parts below.
244
245inline int compare_type(const String8& lhs, const String8& rhs)
246{
247 return lhs.compare(rhs);
248}
249
250inline int strictly_order_type(const String8& lhs, const String8& rhs)
251{
252 return compare_type(lhs, rhs) < 0;
253}
254
Jeff Brown1d618d62010-12-02 13:50:46 -0800255inline const String8 String8::empty() {
256 return String8();
257}
258
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800259inline const char* String8::string() const
260{
261 return mString;
262}
263
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800264inline size_t String8::size() const
265{
266 return length();
267}
268
Jeff Brown48da31b2010-09-12 17:55:08 -0700269inline bool String8::isEmpty() const
270{
271 return length() == 0;
272}
273
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800274inline size_t String8::bytes() const
275{
Sergio Girod2529f22015-09-23 16:22:59 +0100276 return length();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800277}
278
Jeff Brown5ee915a2014-06-06 19:30:15 -0700279inline bool String8::contains(const char* other) const
280{
281 return find(other) >= 0;
282}
283
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800284inline String8& String8::operator=(const String8& other)
285{
286 setTo(other);
287 return *this;
288}
289
290inline String8& String8::operator=(const char* other)
291{
292 setTo(other);
293 return *this;
294}
295
296inline String8& String8::operator+=(const String8& other)
297{
298 append(other);
299 return *this;
300}
301
302inline String8 String8::operator+(const String8& other) const
303{
Kenny Root23b4a092010-08-05 16:21:23 -0700304 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800305 tmp += other;
306 return tmp;
307}
308
309inline String8& String8::operator+=(const char* other)
310{
311 append(other);
312 return *this;
313}
314
315inline String8 String8::operator+(const char* other) const
316{
Kenny Root23b4a092010-08-05 16:21:23 -0700317 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800318 tmp += other;
319 return tmp;
320}
321
322inline int String8::compare(const String8& other) const
323{
324 return strcmp(mString, other.mString);
325}
326
327inline bool String8::operator<(const String8& other) const
328{
329 return strcmp(mString, other.mString) < 0;
330}
331
332inline bool String8::operator<=(const String8& other) const
333{
334 return strcmp(mString, other.mString) <= 0;
335}
336
337inline bool String8::operator==(const String8& other) const
338{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700339 return strcmp(mString, other.mString) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800340}
341
342inline bool String8::operator!=(const String8& other) const
343{
344 return strcmp(mString, other.mString) != 0;
345}
346
347inline bool String8::operator>=(const String8& other) const
348{
349 return strcmp(mString, other.mString) >= 0;
350}
351
352inline bool String8::operator>(const String8& other) const
353{
354 return strcmp(mString, other.mString) > 0;
355}
356
357inline bool String8::operator<(const char* other) const
358{
359 return strcmp(mString, other) < 0;
360}
361
362inline bool String8::operator<=(const char* other) const
363{
364 return strcmp(mString, other) <= 0;
365}
366
367inline bool String8::operator==(const char* other) const
368{
369 return strcmp(mString, other) == 0;
370}
371
372inline bool String8::operator!=(const char* other) const
373{
374 return strcmp(mString, other) != 0;
375}
376
377inline bool String8::operator>=(const char* other) const
378{
379 return strcmp(mString, other) >= 0;
380}
381
382inline bool String8::operator>(const char* other) const
383{
384 return strcmp(mString, other) > 0;
385}
386
387inline String8::operator const char*() const
388{
389 return mString;
390}
391
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900392} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800393
394// ---------------------------------------------------------------------------
395
396#endif // ANDROID_STRING8_H