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