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.
41
String8(const String8& o);
42
explicit String8(const char* o);
43
explicit String8(const char* o, size_t numChars);
45
explicit String8(const String16& o);
46
explicit String8(const char16_t* o);
47
explicit String8(const char16_t* o, size_t numChars);
48
explicit String8(const char32_t* o);
49
explicit String8(const char32_t* o, size_t numChars);
52
static inline const String8 empty();
54
static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
55
static String8 formatV(const char* fmt, va_list args);
57
inline const char* string() const;
58
inline size_t size() const;
59
inline size_t length() const;
60
inline size_t bytes() const;
61
inline bool isEmpty() const;
63
inline const SharedBuffer* sharedBuffer() const;
67
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);
71
status_t setTo(const char32_t* other,
74
status_t append(const String8& other);
75
status_t append(const char* other);
76
status_t append(const char* other, size_t numChars);
78
status_t appendFormat(const char* fmt, ...)
79
__attribute__((format (printf, 2, 3)));
80
status_t appendFormatV(const char* fmt, va_list args);
82
// 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;
87
void getUtf32(char32_t* dst) const;
89
inline String8& operator=(const String8& other);
90
inline String8& operator=(const char* other);
92
inline String8& operator+=(const String8& other);
93
inline String8 operator+(const String8& other) const;
95
inline String8& operator+=(const char* other);
96
inline String8 operator+(const char* other) const;
98
inline int compare(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
inline bool operator>(const String8& 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
inline bool operator>(const char* other) const;
114
inline operator const char*() const;
116
char* lockBuffer(size_t size);
118
status_t unlockBuffer(size_t size);
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;
125
void toLower(size_t start, size_t numChars);
127
void toUpper(size_t start, size_t numChars);
130
* These methods operate on the string as if it were a path name.
134
* Set the filename field to a specific value.
136
* Normalizes the filename, removing a trailing '/' if present.
138
void setPathName(const char* name);
139
void setPathName(const char* name, size_t numChars);
142
* Get just the filename component.
144
* "/tmp/foo/bar.c" --> "bar.c"
146
String8 getPathLeaf(void) const;
149
* Remove the last (file name) component, leaving just the directory
152
* "/tmp/foo/bar.c" --> "/tmp/foo"
153
* "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
156
String8 getPathDir(void) const;
159
* Retrieve the front (root dir) component. Optionally also return the
160
* remaining components.
162
* "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
163
* "/tmp" --> "tmp" (remain = "")
164
* "bar.c" --> "bar.c" (remain = "")
166
String8 walkPath(String8* outRemains = NULL) const;
169
* Return the filename extension. This is the last '.' and any number
170
* of characters that follow it. The '.' is included in case we
171
* decide to expand our definition of what constitutes an extension.
173
* "/tmp/foo/bar.c" --> ".c"
175
* "/tmp/foo.bar/baz" --> ""
176
* "foo.jpeg" --> ".jpeg"
179
String8 getPathExtension(void) const;
182
* Return the path without the extension. Rules for what constitutes
183
* an extension are described in the comment for getPathExtension().
185
* "/tmp/foo/bar.c" --> "/tmp/foo/bar"
187
String8 getBasePath(void) const;
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.
194
* If leaf is a fully qualified path (i.e. starts with '/', it
195
* replaces whatever was there before.
197
String8& appendPath(const char* leaf);
198
String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
201
* Like appendPath(), but does not affect this string. Returns a new one instead.
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()); }
208
* Converts all separators in this string to /, the default path separator.
210
* If the default OS separator is backslash, this converts all
211
* backslashes to slashes, in-place. Otherwise it does nothing.
214
String8& convertToResPath();
217
status_t real_append(const char* other, size_t numChars);
218
char* find_extension(void) const;
223
// String8 can be trivially moved using memcpy() because moving does not
224
// require any change to the underlying SharedBuffer contents or reference count.
225
ANDROID_TRIVIAL_MOVE_TRAIT(String8)
227
TextOutput& operator<<(TextOutput& to, const String16& val);
229
// ---------------------------------------------------------------------------
230
// No user servicable parts below.
232
inline int compare_type(const String8& lhs, const String8& rhs)
234
return lhs.compare(rhs);
237
inline int strictly_order_type(const String8& lhs, const String8& rhs)
239
return compare_type(lhs, rhs) < 0;
242
inline const String8 String8::empty() {
246
inline const char* String8::string() const
251
inline size_t String8::length() const
253
return SharedBuffer::sizeFromData(mString)-1;
256
inline size_t String8::size() const
261
inline bool String8::isEmpty() const
263
return length() == 0;
266
inline size_t String8::bytes() const
268
return SharedBuffer::sizeFromData(mString)-1;
271
inline const SharedBuffer* String8::sharedBuffer() const
273
return SharedBuffer::bufferFromData(mString);
276
inline String8& String8::operator=(const String8& other)
282
inline String8& String8::operator=(const char* other)
288
inline String8& String8::operator+=(const String8& other)
294
inline String8 String8::operator+(const String8& other) const
301
inline String8& String8::operator+=(const char* other)
307
inline String8 String8::operator+(const char* other) const
314
inline int String8::compare(const String8& other) const
316
return strcmp(mString, other.mString);
319
inline bool String8::operator<(const String8& other) const
321
return strcmp(mString, other.mString) < 0;
324
inline bool String8::operator<=(const String8& other) const
326
return strcmp(mString, other.mString) <= 0;
329
inline bool String8::operator==(const String8& other) const
331
return strcmp(mString, other.mString) == 0;
334
inline bool String8::operator!=(const String8& other) const
336
return strcmp(mString, other.mString) != 0;
339
inline bool String8::operator>=(const String8& other) const
341
return strcmp(mString, other.mString) >= 0;
344
inline bool String8::operator>(const String8& other) const
346
return strcmp(mString, other.mString) > 0;
349
inline bool String8::operator<(const char* other) const
351
return strcmp(mString, other) < 0;
354
inline bool String8::operator<=(const char* other) const
356
return strcmp(mString, other) <= 0;
359
inline bool String8::operator==(const char* other) const
361
return strcmp(mString, other) == 0;
364
inline bool String8::operator!=(const char* other) const
366
return strcmp(mString, other) != 0;
369
inline bool String8::operator>=(const char* other) const
371
return strcmp(mString, other) >= 0;
374
inline bool String8::operator>(const char* other) const
376
return strcmp(mString, other) > 0;
379
inline String8::operator const char*() const
384
} // namespace android
386
// ---------------------------------------------------------------------------
388
#endif // ANDROID_STRING8_H