2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_STRING8_H
18 #define ANDROID_STRING8_H
20 #include <utils/Errors.h>
21 #include <utils/SharedBuffer.h>
22 #include <utils/Unicode.h>
23 #include <utils/TypeHelpers.h>
25 #include <string.h> // for strcmp
28 // ---------------------------------------------------------------------------
35 //! This is a string holding UTF-8 characters. Does not allow the value more
36 // than 0x10FFFF, which is not valid unicode codepoint.
40 /* use String8(StaticLinkage) if you're statically linking against
41 * libutils and declaring an empty static String8, e.g.:
43 * static String8 sAStaticEmptyString(String8::kEmptyString);
44 * static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
46 enum StaticLinkage { kEmptyString };
49 explicit String8(StaticLinkage);
50 String8(const String8& o);
51 explicit String8(const char* o);
52 explicit String8(const char* o, size_t numChars);
54 explicit String8(const String16& o);
55 explicit String8(const char16_t* o);
56 explicit String8(const char16_t* o, size_t numChars);
57 explicit String8(const char32_t* o);
58 explicit String8(const char32_t* o, size_t numChars);
61 static inline const String8 empty();
63 static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
64 static String8 formatV(const char* fmt, va_list args);
66 inline const char* string() const;
67 inline size_t size() const;
68 inline size_t length() const;
69 inline size_t bytes() const;
70 inline bool isEmpty() const;
72 inline const SharedBuffer* sharedBuffer() const;
76 void setTo(const String8& other);
77 status_t setTo(const char* other);
78 status_t setTo(const char* other, size_t numChars);
79 status_t setTo(const char16_t* other, size_t numChars);
80 status_t setTo(const char32_t* other,
83 status_t append(const String8& other);
84 status_t append(const char* other);
85 status_t append(const char* other, size_t numChars);
87 status_t appendFormat(const char* fmt, ...)
88 __attribute__((format (printf, 2, 3)));
89 status_t appendFormatV(const char* fmt, va_list args);
91 // Note that this function takes O(N) time to calculate the value.
92 // No cache value is stored.
93 size_t getUtf32Length() const;
94 int32_t getUtf32At(size_t index,
95 size_t *next_index) const;
96 void getUtf32(char32_t* dst) const;
98 inline String8& operator=(const String8& other);
99 inline String8& operator=(const char* other);
101 inline String8& operator+=(const String8& other);
102 inline String8 operator+(const String8& other) const;
104 inline String8& operator+=(const char* other);
105 inline String8 operator+(const char* other) const;
107 inline int compare(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 inline bool operator>=(const String8& other) const;
114 inline bool operator>(const String8& 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 inline bool operator>=(const char* other) const;
121 inline bool operator>(const char* other) const;
123 inline operator const char*() const;
125 char* lockBuffer(size_t size);
127 status_t unlockBuffer(size_t size);
129 // return the index of the first byte of other in this at or after
130 // start, or -1 if not found
131 ssize_t find(const char* other, size_t start = 0) const;
134 void toLower(size_t start, size_t numChars);
136 void toUpper(size_t start, size_t numChars);
139 * These methods operate on the string as if it were a path name.
143 * Set the filename field to a specific value.
145 * Normalizes the filename, removing a trailing '/' if present.
147 void setPathName(const char* name);
148 void setPathName(const char* name, size_t numChars);
151 * Get just the filename component.
153 * "/tmp/foo/bar.c" --> "bar.c"
155 String8 getPathLeaf(void) const;
158 * Remove the last (file name) component, leaving just the directory
161 * "/tmp/foo/bar.c" --> "/tmp/foo"
162 * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
165 String8 getPathDir(void) const;
168 * Retrieve the front (root dir) component. Optionally also return the
169 * remaining components.
171 * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
172 * "/tmp" --> "tmp" (remain = "")
173 * "bar.c" --> "bar.c" (remain = "")
175 String8 walkPath(String8* outRemains = NULL) const;
178 * Return the filename extension. This is the last '.' and any number
179 * of characters that follow it. The '.' is included in case we
180 * decide to expand our definition of what constitutes an extension.
182 * "/tmp/foo/bar.c" --> ".c"
184 * "/tmp/foo.bar/baz" --> ""
185 * "foo.jpeg" --> ".jpeg"
188 String8 getPathExtension(void) const;
191 * Return the path without the extension. Rules for what constitutes
192 * an extension are described in the comment for getPathExtension().
194 * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
196 String8 getBasePath(void) const;
199 * Add a component to the pathname. We guarantee that there is
200 * exactly one path separator between the old path and the new.
201 * If there is no existing name, we just copy the new name in.
203 * If leaf is a fully qualified path (i.e. starts with '/', it
204 * replaces whatever was there before.
206 String8& appendPath(const char* leaf);
207 String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
210 * Like appendPath(), but does not affect this string. Returns a new one instead.
212 String8 appendPathCopy(const char* leaf) const
213 { String8 p(*this); p.appendPath(leaf); return p; }
214 String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
217 * Converts all separators in this string to /, the default path separator.
219 * If the default OS separator is backslash, this converts all
220 * backslashes to slashes, in-place. Otherwise it does nothing.
223 String8& convertToResPath();
226 status_t real_append(const char* other, size_t numChars);
227 char* find_extension(void) const;
232 // String8 can be trivially moved using memcpy() because moving does not
233 // require any change to the underlying SharedBuffer contents or reference count.
234 ANDROID_TRIVIAL_MOVE_TRAIT(String8)
236 // ---------------------------------------------------------------------------
237 // No user servicable parts below.
239 inline int compare_type(const String8& lhs, const String8& rhs)
241 return lhs.compare(rhs);
244 inline int strictly_order_type(const String8& lhs, const String8& rhs)
246 return compare_type(lhs, rhs) < 0;
249 inline const String8 String8::empty() {
253 inline const char* String8::string() const
258 inline size_t String8::length() const
260 return SharedBuffer::sizeFromData(mString)-1;
263 inline size_t String8::size() const
268 inline bool String8::isEmpty() const
270 return length() == 0;
273 inline size_t String8::bytes() const
275 return SharedBuffer::sizeFromData(mString)-1;
278 inline const SharedBuffer* String8::sharedBuffer() const
280 return SharedBuffer::bufferFromData(mString);
283 inline String8& String8::operator=(const String8& other)
289 inline String8& String8::operator=(const char* other)
295 inline String8& String8::operator+=(const String8& other)
301 inline String8 String8::operator+(const String8& other) const
308 inline String8& String8::operator+=(const char* other)
314 inline String8 String8::operator+(const char* other) const
321 inline int String8::compare(const String8& other) const
323 return strcmp(mString, other.mString);
326 inline bool String8::operator<(const String8& other) const
328 return strcmp(mString, other.mString) < 0;
331 inline bool String8::operator<=(const String8& other) const
333 return strcmp(mString, other.mString) <= 0;
336 inline bool String8::operator==(const String8& other) const
338 return strcmp(mString, other.mString) == 0;
341 inline bool String8::operator!=(const String8& other) const
343 return strcmp(mString, other.mString) != 0;
346 inline bool String8::operator>=(const String8& other) const
348 return strcmp(mString, other.mString) >= 0;
351 inline bool String8::operator>(const String8& other) const
353 return strcmp(mString, other.mString) > 0;
356 inline bool String8::operator<(const char* other) const
358 return strcmp(mString, other) < 0;
361 inline bool String8::operator<=(const char* other) const
363 return strcmp(mString, other) <= 0;
366 inline bool String8::operator==(const char* other) const
368 return strcmp(mString, other) == 0;
371 inline bool String8::operator!=(const char* other) const
373 return strcmp(mString, other) != 0;
376 inline bool String8::operator>=(const char* other) const
378 return strcmp(mString, other) >= 0;
381 inline bool String8::operator>(const char* other) const
383 return strcmp(mString, other) > 0;
386 inline String8::operator const char*() const
391 } // namespace android
393 // ---------------------------------------------------------------------------
395 #endif // ANDROID_STRING8_H