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