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