blob: 335e7f1bf50353487a84fb4c9a675ef4ba2bcdf1 [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/SharedBuffer.h>
22#include <utils/Unicode.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070023#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Kenny Rootba0165b2010-11-09 14:37:23 -080025#include <string.h> // for strcmp
Jeff Brown647925d2010-11-10 16:03:06 -080026#include <stdarg.h>
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090027
28// ---------------------------------------------------------------------------
29
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080030namespace android {
31
Kenny Rootba0165b2010-11-09 14:37:23 -080032class String16;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033class TextOutput;
34
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090035//! This is a string holding UTF-8 characters. Does not allow the value more
36// than 0x10FFFF, which is not valid unicode codepoint.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080037class String8
38{
39public:
40 String8();
41 String8(const String8& o);
42 explicit String8(const char* o);
43 explicit String8(const char* o, size_t numChars);
44
45 explicit String8(const String16& o);
46 explicit String8(const char16_t* o);
47 explicit String8(const char16_t* o, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090048 explicit String8(const char32_t* o);
49 explicit String8(const char32_t* o, size_t numChars);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050 ~String8();
Jeff Brown1d618d62010-12-02 13:50:46 -080051
52 static inline const String8 empty();
53
54 static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
55 static String8 formatV(const char* fmt, va_list args);
56
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080057 inline const char* string() const;
58 inline size_t size() const;
59 inline size_t length() const;
60 inline size_t bytes() const;
Jeff Brown48da31b2010-09-12 17:55:08 -070061 inline bool isEmpty() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062
63 inline const SharedBuffer* sharedBuffer() const;
64
Jeff Brown48da31b2010-09-12 17:55:08 -070065 void clear();
66
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067 void setTo(const String8& other);
68 status_t setTo(const char* other);
69 status_t setTo(const char* other, size_t numChars);
70 status_t setTo(const char16_t* other, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090071 status_t setTo(const char32_t* other,
72 size_t length);
73
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 status_t append(const String8& other);
75 status_t append(const char* other);
76 status_t append(const char* other, size_t numChars);
77
Jeff Brown38fb25b2010-08-11 14:46:32 -070078 status_t appendFormat(const char* fmt, ...)
79 __attribute__((format (printf, 2, 3)));
Jeff Brown647925d2010-11-10 16:03:06 -080080 status_t appendFormatV(const char* fmt, va_list args);
Jeff Brown35a154e2010-07-15 23:54:05 -070081
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090082 // Note that this function takes O(N) time to calculate the value.
83 // No cache value is stored.
84 size_t getUtf32Length() const;
85 int32_t getUtf32At(size_t index,
86 size_t *next_index) const;
Kenny Rootba0165b2010-11-09 14:37:23 -080087 void getUtf32(char32_t* dst) const;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090088
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080089 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
107 inline bool operator<(const char* other) const;
108 inline bool operator<=(const char* other) const;
109 inline bool operator==(const char* other) const;
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
114 inline operator const char*() const;
115
116 char* lockBuffer(size_t size);
117 void unlockBuffer();
118 status_t unlockBuffer(size_t size);
119
120 // return the index of the first byte of other in this at or after
121 // start, or -1 if not found
122 ssize_t find(const char* other, size_t start = 0) const;
123
124 void toLower();
125 void toLower(size_t start, size_t numChars);
126 void toUpper();
127 void toUpper(size_t start, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900128
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800129 /*
130 * These methods operate on the string as if it were a path name.
131 */
132
133 /*
134 * Set the filename field to a specific value.
135 *
136 * Normalizes the filename, removing a trailing '/' if present.
137 */
138 void setPathName(const char* name);
139 void setPathName(const char* name, size_t numChars);
140
141 /*
142 * Get just the filename component.
143 *
144 * "/tmp/foo/bar.c" --> "bar.c"
145 */
146 String8 getPathLeaf(void) const;
147
148 /*
149 * Remove the last (file name) component, leaving just the directory
150 * name.
151 *
152 * "/tmp/foo/bar.c" --> "/tmp/foo"
153 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
154 * "bar.c" --> ""
155 */
156 String8 getPathDir(void) const;
157
158 /*
159 * Retrieve the front (root dir) component. Optionally also return the
160 * remaining components.
161 *
162 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
163 * "/tmp" --> "tmp" (remain = "")
164 * "bar.c" --> "bar.c" (remain = "")
165 */
166 String8 walkPath(String8* outRemains = NULL) const;
167
168 /*
Glenn Kasten29d4d202011-03-14 11:32:29 -0700169 * Return the filename extension. This is the last '.' and any number
170 * of characters that follow it. The '.' is included in case we
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800171 * decide to expand our definition of what constitutes an extension.
172 *
173 * "/tmp/foo/bar.c" --> ".c"
174 * "/tmp" --> ""
175 * "/tmp/foo.bar/baz" --> ""
176 * "foo.jpeg" --> ".jpeg"
177 * "foo." --> ""
178 */
179 String8 getPathExtension(void) const;
180
181 /*
182 * Return the path without the extension. Rules for what constitutes
183 * an extension are described in the comment for getPathExtension().
184 *
185 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
186 */
187 String8 getBasePath(void) const;
188
189 /*
190 * Add a component to the pathname. We guarantee that there is
191 * exactly one path separator between the old path and the new.
192 * If there is no existing name, we just copy the new name in.
193 *
194 * If leaf is a fully qualified path (i.e. starts with '/', it
195 * replaces whatever was there before.
196 */
197 String8& appendPath(const char* leaf);
198 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
199
200 /*
201 * Like appendPath(), but does not affect this string. Returns a new one instead.
202 */
203 String8 appendPathCopy(const char* leaf) const
204 { String8 p(*this); p.appendPath(leaf); return p; }
205 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
206
207 /*
208 * Converts all separators in this string to /, the default path separator.
209 *
210 * If the default OS separator is backslash, this converts all
211 * backslashes to slashes, in-place. Otherwise it does nothing.
212 * Returns self.
213 */
214 String8& convertToResPath();
215
216private:
217 status_t real_append(const char* other, size_t numChars);
218 char* find_extension(void) const;
219
220 const char* mString;
221};
222
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700223// String8 can be trivially moved using memcpy() because moving does not
224// require any change to the underlying SharedBuffer contents or reference count.
225ANDROID_TRIVIAL_MOVE_TRAIT(String8)
226
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800227TextOutput& operator<<(TextOutput& to, const String16& val);
228
229// ---------------------------------------------------------------------------
230// No user servicable parts below.
231
232inline int compare_type(const String8& lhs, const String8& rhs)
233{
234 return lhs.compare(rhs);
235}
236
237inline int strictly_order_type(const String8& lhs, const String8& rhs)
238{
239 return compare_type(lhs, rhs) < 0;
240}
241
Jeff Brown1d618d62010-12-02 13:50:46 -0800242inline const String8 String8::empty() {
243 return String8();
244}
245
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800246inline const char* String8::string() const
247{
248 return mString;
249}
250
251inline size_t String8::length() const
252{
253 return SharedBuffer::sizeFromData(mString)-1;
254}
255
256inline size_t String8::size() const
257{
258 return length();
259}
260
Jeff Brown48da31b2010-09-12 17:55:08 -0700261inline bool String8::isEmpty() const
262{
263 return length() == 0;
264}
265
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800266inline size_t String8::bytes() const
267{
268 return SharedBuffer::sizeFromData(mString)-1;
269}
270
271inline const SharedBuffer* String8::sharedBuffer() const
272{
273 return SharedBuffer::bufferFromData(mString);
274}
275
276inline String8& String8::operator=(const String8& other)
277{
278 setTo(other);
279 return *this;
280}
281
282inline String8& String8::operator=(const char* other)
283{
284 setTo(other);
285 return *this;
286}
287
288inline String8& String8::operator+=(const String8& other)
289{
290 append(other);
291 return *this;
292}
293
294inline String8 String8::operator+(const String8& other) const
295{
Kenny Root23b4a092010-08-05 16:21:23 -0700296 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800297 tmp += other;
298 return tmp;
299}
300
301inline String8& String8::operator+=(const char* other)
302{
303 append(other);
304 return *this;
305}
306
307inline String8 String8::operator+(const char* other) const
308{
Kenny Root23b4a092010-08-05 16:21:23 -0700309 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800310 tmp += other;
311 return tmp;
312}
313
314inline int String8::compare(const String8& other) const
315{
316 return strcmp(mString, other.mString);
317}
318
319inline bool String8::operator<(const String8& other) const
320{
321 return strcmp(mString, other.mString) < 0;
322}
323
324inline bool String8::operator<=(const String8& other) const
325{
326 return strcmp(mString, other.mString) <= 0;
327}
328
329inline bool String8::operator==(const String8& other) const
330{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700331 return strcmp(mString, other.mString) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800332}
333
334inline bool String8::operator!=(const String8& other) const
335{
336 return strcmp(mString, other.mString) != 0;
337}
338
339inline bool String8::operator>=(const String8& other) const
340{
341 return strcmp(mString, other.mString) >= 0;
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 char* other) const
350{
351 return strcmp(mString, other) < 0;
352}
353
354inline bool String8::operator<=(const char* other) const
355{
356 return strcmp(mString, other) <= 0;
357}
358
359inline bool String8::operator==(const char* other) const
360{
361 return strcmp(mString, other) == 0;
362}
363
364inline bool String8::operator!=(const char* other) const
365{
366 return strcmp(mString, other) != 0;
367}
368
369inline bool String8::operator>=(const char* other) const
370{
371 return strcmp(mString, other) >= 0;
372}
373
374inline bool String8::operator>(const char* other) const
375{
376 return strcmp(mString, other) > 0;
377}
378
379inline String8::operator const char*() const
380{
381 return mString;
382}
383
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900384} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800385
386// ---------------------------------------------------------------------------
387
388#endif // ANDROID_STRING8_H