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