blob: 6abfb063415f67ad0d401ac7a1696100c8551de5 [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>
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:
39 String8();
40 String8(const String8& o);
41 explicit String8(const char* o);
42 explicit String8(const char* o, size_t numChars);
43
44 explicit String8(const String16& o);
45 explicit String8(const char16_t* o);
46 explicit String8(const char16_t* o, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090047 explicit String8(const char32_t* o);
48 explicit String8(const char32_t* o, size_t numChars);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049 ~String8();
50
51 inline const char* string() const;
52 inline size_t size() const;
53 inline size_t length() const;
54 inline size_t bytes() const;
Jeff Brown48da31b2010-09-12 17:55:08 -070055 inline bool isEmpty() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080056
57 inline const SharedBuffer* sharedBuffer() const;
58
Jeff Brown48da31b2010-09-12 17:55:08 -070059 void clear();
60
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061 void setTo(const String8& other);
62 status_t setTo(const char* other);
63 status_t setTo(const char* other, size_t numChars);
64 status_t setTo(const char16_t* other, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090065 status_t setTo(const char32_t* other,
66 size_t length);
67
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 status_t append(const String8& other);
69 status_t append(const char* other);
70 status_t append(const char* other, size_t numChars);
71
Jeff Brown38fb25b2010-08-11 14:46:32 -070072 status_t appendFormat(const char* fmt, ...)
73 __attribute__((format (printf, 2, 3)));
Jeff Brown647925d2010-11-10 16:03:06 -080074 status_t appendFormatV(const char* fmt, va_list args);
Jeff Brown35a154e2010-07-15 23:54:05 -070075
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090076 // Note that this function takes O(N) time to calculate the value.
77 // No cache value is stored.
78 size_t getUtf32Length() const;
79 int32_t getUtf32At(size_t index,
80 size_t *next_index) const;
Kenny Rootba0165b2010-11-09 14:37:23 -080081 void getUtf32(char32_t* dst) const;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090082
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080083 inline String8& operator=(const String8& other);
84 inline String8& operator=(const char* other);
85
86 inline String8& operator+=(const String8& other);
87 inline String8 operator+(const String8& other) const;
88
89 inline String8& operator+=(const char* other);
90 inline String8 operator+(const char* other) const;
91
92 inline int compare(const String8& other) const;
93
94 inline bool operator<(const String8& other) const;
95 inline bool operator<=(const String8& other) const;
96 inline bool operator==(const String8& other) const;
97 inline bool operator!=(const String8& other) const;
98 inline bool operator>=(const String8& other) const;
99 inline bool operator>(const String8& other) const;
100
101 inline bool operator<(const char* other) const;
102 inline bool operator<=(const char* other) const;
103 inline bool operator==(const char* other) const;
104 inline bool operator!=(const char* other) const;
105 inline bool operator>=(const char* other) const;
106 inline bool operator>(const char* other) const;
107
108 inline operator const char*() const;
109
110 char* lockBuffer(size_t size);
111 void unlockBuffer();
112 status_t unlockBuffer(size_t size);
113
114 // return the index of the first byte of other in this at or after
115 // start, or -1 if not found
116 ssize_t find(const char* other, size_t start = 0) const;
117
118 void toLower();
119 void toLower(size_t start, size_t numChars);
120 void toUpper();
121 void toUpper(size_t start, size_t numChars);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123 /*
124 * These methods operate on the string as if it were a path name.
125 */
126
127 /*
128 * Set the filename field to a specific value.
129 *
130 * Normalizes the filename, removing a trailing '/' if present.
131 */
132 void setPathName(const char* name);
133 void setPathName(const char* name, size_t numChars);
134
135 /*
136 * Get just the filename component.
137 *
138 * "/tmp/foo/bar.c" --> "bar.c"
139 */
140 String8 getPathLeaf(void) const;
141
142 /*
143 * Remove the last (file name) component, leaving just the directory
144 * name.
145 *
146 * "/tmp/foo/bar.c" --> "/tmp/foo"
147 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
148 * "bar.c" --> ""
149 */
150 String8 getPathDir(void) const;
151
152 /*
153 * Retrieve the front (root dir) component. Optionally also return the
154 * remaining components.
155 *
156 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
157 * "/tmp" --> "tmp" (remain = "")
158 * "bar.c" --> "bar.c" (remain = "")
159 */
160 String8 walkPath(String8* outRemains = NULL) const;
161
162 /*
163 * Return the filename extension. This is the last '.' and up to
164 * four characters that follow it. The '.' is included in case we
165 * decide to expand our definition of what constitutes an extension.
166 *
167 * "/tmp/foo/bar.c" --> ".c"
168 * "/tmp" --> ""
169 * "/tmp/foo.bar/baz" --> ""
170 * "foo.jpeg" --> ".jpeg"
171 * "foo." --> ""
172 */
173 String8 getPathExtension(void) const;
174
175 /*
176 * Return the path without the extension. Rules for what constitutes
177 * an extension are described in the comment for getPathExtension().
178 *
179 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
180 */
181 String8 getBasePath(void) const;
182
183 /*
184 * Add a component to the pathname. We guarantee that there is
185 * exactly one path separator between the old path and the new.
186 * If there is no existing name, we just copy the new name in.
187 *
188 * If leaf is a fully qualified path (i.e. starts with '/', it
189 * replaces whatever was there before.
190 */
191 String8& appendPath(const char* leaf);
192 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
193
194 /*
195 * Like appendPath(), but does not affect this string. Returns a new one instead.
196 */
197 String8 appendPathCopy(const char* leaf) const
198 { String8 p(*this); p.appendPath(leaf); return p; }
199 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
200
201 /*
202 * Converts all separators in this string to /, the default path separator.
203 *
204 * If the default OS separator is backslash, this converts all
205 * backslashes to slashes, in-place. Otherwise it does nothing.
206 * Returns self.
207 */
208 String8& convertToResPath();
209
210private:
211 status_t real_append(const char* other, size_t numChars);
212 char* find_extension(void) const;
213
214 const char* mString;
215};
216
217TextOutput& operator<<(TextOutput& to, const String16& val);
218
219// ---------------------------------------------------------------------------
220// No user servicable parts below.
221
222inline int compare_type(const String8& lhs, const String8& rhs)
223{
224 return lhs.compare(rhs);
225}
226
227inline int strictly_order_type(const String8& lhs, const String8& rhs)
228{
229 return compare_type(lhs, rhs) < 0;
230}
231
232inline const char* String8::string() const
233{
234 return mString;
235}
236
237inline size_t String8::length() const
238{
239 return SharedBuffer::sizeFromData(mString)-1;
240}
241
242inline size_t String8::size() const
243{
244 return length();
245}
246
Jeff Brown48da31b2010-09-12 17:55:08 -0700247inline bool String8::isEmpty() const
248{
249 return length() == 0;
250}
251
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800252inline size_t String8::bytes() const
253{
254 return SharedBuffer::sizeFromData(mString)-1;
255}
256
257inline const SharedBuffer* String8::sharedBuffer() const
258{
259 return SharedBuffer::bufferFromData(mString);
260}
261
262inline String8& String8::operator=(const String8& other)
263{
264 setTo(other);
265 return *this;
266}
267
268inline String8& String8::operator=(const char* other)
269{
270 setTo(other);
271 return *this;
272}
273
274inline String8& String8::operator+=(const String8& other)
275{
276 append(other);
277 return *this;
278}
279
280inline String8 String8::operator+(const String8& other) const
281{
Kenny Root23b4a092010-08-05 16:21:23 -0700282 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800283 tmp += other;
284 return tmp;
285}
286
287inline String8& String8::operator+=(const char* other)
288{
289 append(other);
290 return *this;
291}
292
293inline String8 String8::operator+(const char* other) const
294{
Kenny Root23b4a092010-08-05 16:21:23 -0700295 String8 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800296 tmp += other;
297 return tmp;
298}
299
300inline int String8::compare(const String8& other) const
301{
302 return strcmp(mString, other.mString);
303}
304
305inline bool String8::operator<(const String8& other) const
306{
307 return strcmp(mString, other.mString) < 0;
308}
309
310inline bool String8::operator<=(const String8& other) const
311{
312 return strcmp(mString, other.mString) <= 0;
313}
314
315inline bool String8::operator==(const String8& other) const
316{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700317 return strcmp(mString, other.mString) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800318}
319
320inline bool String8::operator!=(const String8& other) const
321{
322 return strcmp(mString, other.mString) != 0;
323}
324
325inline bool String8::operator>=(const String8& other) const
326{
327 return strcmp(mString, other.mString) >= 0;
328}
329
330inline bool String8::operator>(const String8& other) const
331{
332 return strcmp(mString, other.mString) > 0;
333}
334
335inline bool String8::operator<(const char* other) const
336{
337 return strcmp(mString, other) < 0;
338}
339
340inline bool String8::operator<=(const char* other) const
341{
342 return strcmp(mString, other) <= 0;
343}
344
345inline bool String8::operator==(const char* other) const
346{
347 return strcmp(mString, other) == 0;
348}
349
350inline bool String8::operator!=(const char* other) const
351{
352 return strcmp(mString, other) != 0;
353}
354
355inline bool String8::operator>=(const char* other) const
356{
357 return strcmp(mString, other) >= 0;
358}
359
360inline bool String8::operator>(const char* other) const
361{
362 return strcmp(mString, other) > 0;
363}
364
365inline String8::operator const char*() const
366{
367 return mString;
368}
369
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900370} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371
372// ---------------------------------------------------------------------------
373
374#endif // ANDROID_STRING8_H