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