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