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             void                toLower(size_t start, size_t numChars);
134             void                toUpper();
135             void                toUpper(size_t start, size_t numChars);
136 
137 
138     /*
139      * These methods operate on the string as if it were a path name.
140      */
141 
142     /*
143      * Set the filename field to a specific value.
144      *
145      * Normalizes the filename, removing a trailing '/' if present.
146      */
147     void setPathName(const char* name);
148     void setPathName(const char* name, size_t numChars);
149 
150     /*
151      * Get just the filename component.
152      *
153      * "/tmp/foo/bar.c" --> "bar.c"
154      */
155     String8 getPathLeaf(void) const;
156 
157     /*
158      * Remove the last (file name) component, leaving just the directory
159      * name.
160      *
161      * "/tmp/foo/bar.c" --> "/tmp/foo"
162      * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
163      * "bar.c" --> ""
164      */
165     String8 getPathDir(void) const;
166 
167     /*
168      * Retrieve the front (root dir) component.  Optionally also return the
169      * remaining components.
170      *
171      * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
172      * "/tmp" --> "tmp" (remain = "")
173      * "bar.c" --> "bar.c" (remain = "")
174      */
175     String8 walkPath(String8* outRemains = nullptr) const;
176 
177     /*
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.
181      *
182      * "/tmp/foo/bar.c" --> ".c"
183      * "/tmp" --> ""
184      * "/tmp/foo.bar/baz" --> ""
185      * "foo.jpeg" --> ".jpeg"
186      * "foo." --> ""
187      */
188     String8 getPathExtension(void) const;
189 
190     /*
191      * Return the path without the extension.  Rules for what constitutes
192      * an extension are described in the comment for getPathExtension().
193      *
194      * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
195      */
196     String8 getBasePath(void) const;
197 
198     /*
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.
202      *
203      * If leaf is a fully qualified path (i.e. starts with '/', it
204      * replaces whatever was there before.
205      */
206     String8& appendPath(const char* leaf);
appendPath(const String8 & leaf)207     String8& appendPath(const String8& leaf)  { return appendPath(leaf.string()); }
208 
209     /*
210      * Like appendPath(), but does not affect this string.  Returns a new one instead.
211      */
appendPathCopy(const char * leaf)212     String8 appendPathCopy(const char* leaf) const
213                                              { String8 p(*this); p.appendPath(leaf); return p; }
appendPathCopy(const String8 & leaf)214     String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
215 
216     /*
217      * Converts all separators in this string to /, the default path separator.
218      *
219      * If the default OS separator is backslash, this converts all
220      * backslashes to slashes, in-place. Otherwise it does nothing.
221      * Returns self.
222      */
223     String8& convertToResPath();
224 
225 private:
226             status_t            real_append(const char* other, size_t numChars);
227             char*               find_extension(void) const;
228 
229             const char* mString;
230 };
231 
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)
235 
236 static inline std::ostream& operator<<(std::ostream& os, const String8& str) {
237     os << str.c_str();
238     return os;
239 }
240 
241 // ---------------------------------------------------------------------------
242 // No user servicable parts below.
243 
compare_type(const String8 & lhs,const String8 & rhs)244 inline int compare_type(const String8& lhs, const String8& rhs)
245 {
246     return lhs.compare(rhs);
247 }
248 
strictly_order_type(const String8 & lhs,const String8 & rhs)249 inline int strictly_order_type(const String8& lhs, const String8& rhs)
250 {
251     return compare_type(lhs, rhs) < 0;
252 }
253 
empty()254 inline const String8 String8::empty() {
255     return String8();
256 }
257 
c_str()258 inline const char* String8::c_str() const
259 {
260     return mString;
261 }
string()262 inline const char* String8::string() const
263 {
264     return mString;
265 }
266 
std_string(const String8 & str)267 inline std::string String8::std_string(const String8& str)
268 {
269     return std::string(str.string());
270 }
271 
size()272 inline size_t String8::size() const
273 {
274     return length();
275 }
276 
isEmpty()277 inline bool String8::isEmpty() const
278 {
279     return length() == 0;
280 }
281 
bytes()282 inline size_t String8::bytes() const
283 {
284     return length();
285 }
286 
contains(const char * other)287 inline bool String8::contains(const char* other) const
288 {
289     return find(other) >= 0;
290 }
291 
292 inline String8& String8::operator=(const String8& other)
293 {
294     setTo(other);
295     return *this;
296 }
297 
298 inline String8& String8::operator=(const char* other)
299 {
300     setTo(other);
301     return *this;
302 }
303 
304 inline String8& String8::operator+=(const String8& other)
305 {
306     append(other);
307     return *this;
308 }
309 
310 inline String8 String8::operator+(const String8& other) const
311 {
312     String8 tmp(*this);
313     tmp += other;
314     return tmp;
315 }
316 
317 inline String8& String8::operator+=(const char* other)
318 {
319     append(other);
320     return *this;
321 }
322 
323 inline String8 String8::operator+(const char* other) const
324 {
325     String8 tmp(*this);
326     tmp += other;
327     return tmp;
328 }
329 
compare(const String8 & other)330 inline int String8::compare(const String8& other) const
331 {
332     return strcmp(mString, other.mString);
333 }
334 
335 inline bool String8::operator<(const String8& other) const
336 {
337     return strcmp(mString, other.mString) < 0;
338 }
339 
340 inline bool String8::operator<=(const String8& other) const
341 {
342     return strcmp(mString, other.mString) <= 0;
343 }
344 
345 inline bool String8::operator==(const String8& other) const
346 {
347     return strcmp(mString, other.mString) == 0;
348 }
349 
350 inline bool String8::operator!=(const String8& other) const
351 {
352     return strcmp(mString, other.mString) != 0;
353 }
354 
355 inline bool String8::operator>=(const String8& other) const
356 {
357     return strcmp(mString, other.mString) >= 0;
358 }
359 
360 inline bool String8::operator>(const String8& other) const
361 {
362     return strcmp(mString, other.mString) > 0;
363 }
364 
365 inline bool String8::operator<(const char* other) const
366 {
367     return strcmp(mString, other) < 0;
368 }
369 
370 inline bool String8::operator<=(const char* other) const
371 {
372     return strcmp(mString, other) <= 0;
373 }
374 
375 inline bool String8::operator==(const char* other) const
376 {
377     return strcmp(mString, other) == 0;
378 }
379 
380 inline bool String8::operator!=(const char* other) const
381 {
382     return strcmp(mString, other) != 0;
383 }
384 
385 inline bool String8::operator>=(const char* other) const
386 {
387     return strcmp(mString, other) >= 0;
388 }
389 
390 inline bool String8::operator>(const char* other) const
391 {
392     return strcmp(mString, other) > 0;
393 }
394 
395 inline String8::operator const char*() const
396 {
397     return mString;
398 }
399 
400 }  // namespace android
401 
402 // ---------------------------------------------------------------------------
403 
404 #endif // ANDROID_STRING8_H
405