The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 20 | #include <string> // for std::string |
| 21 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 23 | #include <utils/Unicode.h> |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 24 | #include <utils/TypeHelpers.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 26 | #include <string.h> // for strcmp |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 27 | #include <stdarg.h> |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 33 | class String16; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | class TextOutput; |
| 35 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 36 | //! This is a string holding UTF-8 characters. Does not allow the value more |
| 37 | // than 0x10FFFF, which is not valid unicode codepoint. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | class String8 |
| 39 | { |
| 40 | public: |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 41 | /* use String8(StaticLinkage) if you're statically linking against |
| 42 | * libutils and declaring an empty static String8, e.g.: |
| 43 | * |
| 44 | * static String8 sAStaticEmptyString(String8::kEmptyString); |
| 45 | * static String8 sAnotherStaticEmptyString(sAStaticEmptyString); |
| 46 | */ |
| 47 | enum StaticLinkage { kEmptyString }; |
| 48 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | String8(); |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 50 | explicit String8(StaticLinkage); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | String8(const String8& o); |
| 52 | explicit String8(const char* o); |
| 53 | explicit String8(const char* o, size_t numChars); |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 54 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | explicit String8(const String16& o); |
| 56 | explicit String8(const char16_t* o); |
| 57 | explicit String8(const char16_t* o, size_t numChars); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 58 | explicit String8(const char32_t* o); |
| 59 | explicit String8(const char32_t* o, size_t numChars); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | ~String8(); |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 61 | |
| 62 | static inline const String8 empty(); |
| 63 | |
| 64 | static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2))); |
| 65 | static String8 formatV(const char* fmt, va_list args); |
| 66 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | inline const char* string() const; |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 68 | static inline std::string std_string(const String8& str); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | inline size_t size() const; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | inline size_t bytes() const; |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 71 | inline bool isEmpty() const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 72 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 73 | size_t length() const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 74 | |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 75 | void clear(); |
| 76 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | void setTo(const String8& other); |
| 78 | status_t setTo(const char* other); |
| 79 | status_t setTo(const char* other, size_t numChars); |
| 80 | status_t setTo(const char16_t* other, size_t numChars); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 81 | status_t setTo(const char32_t* other, |
| 82 | size_t length); |
| 83 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | status_t append(const String8& other); |
| 85 | status_t append(const char* other); |
| 86 | status_t append(const char* other, size_t numChars); |
| 87 | |
Jeff Brown | 38fb25b | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 88 | status_t appendFormat(const char* fmt, ...) |
| 89 | __attribute__((format (printf, 2, 3))); |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 90 | status_t appendFormatV(const char* fmt, va_list args); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 91 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 92 | // Note that this function takes O(N) time to calculate the value. |
| 93 | // No cache value is stored. |
| 94 | size_t getUtf32Length() const; |
| 95 | int32_t getUtf32At(size_t index, |
| 96 | size_t *next_index) const; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 97 | void getUtf32(char32_t* dst) const; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 98 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | inline String8& operator=(const String8& other); |
| 100 | inline String8& operator=(const char* other); |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 101 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | inline String8& operator+=(const String8& other); |
| 103 | inline String8 operator+(const String8& other) const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | inline String8& operator+=(const char* other); |
| 106 | inline String8 operator+(const char* other) const; |
| 107 | |
| 108 | inline int compare(const String8& other) const; |
| 109 | |
| 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 | inline bool operator>(const String8& other) const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 116 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | inline bool operator>(const char* other) const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | inline operator const char*() const; |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 125 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | char* lockBuffer(size_t size); |
| 127 | void unlockBuffer(); |
| 128 | status_t unlockBuffer(size_t size); |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 129 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | // return the index of the first byte of other in this at or after |
| 131 | // start, or -1 if not found |
| 132 | ssize_t find(const char* other, size_t start = 0) const; |
| 133 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 134 | // return true if this string contains the specified substring |
| 135 | inline bool contains(const char* other) const; |
| 136 | |
| 137 | // removes all occurrence of the specified substring |
| 138 | // returns true if any were found and removed |
| 139 | bool removeAll(const char* other); |
| 140 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | void toLower(); |
| 142 | void toLower(size_t start, size_t numChars); |
| 143 | void toUpper(); |
| 144 | void toUpper(size_t start, size_t numChars); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 145 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 146 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | /* |
| 148 | * These methods operate on the string as if it were a path name. |
| 149 | */ |
| 150 | |
| 151 | /* |
| 152 | * Set the filename field to a specific value. |
| 153 | * |
| 154 | * Normalizes the filename, removing a trailing '/' if present. |
| 155 | */ |
| 156 | void setPathName(const char* name); |
| 157 | void setPathName(const char* name, size_t numChars); |
| 158 | |
| 159 | /* |
| 160 | * Get just the filename component. |
| 161 | * |
| 162 | * "/tmp/foo/bar.c" --> "bar.c" |
| 163 | */ |
| 164 | String8 getPathLeaf(void) const; |
| 165 | |
| 166 | /* |
| 167 | * Remove the last (file name) component, leaving just the directory |
| 168 | * name. |
| 169 | * |
| 170 | * "/tmp/foo/bar.c" --> "/tmp/foo" |
| 171 | * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX |
| 172 | * "bar.c" --> "" |
| 173 | */ |
| 174 | String8 getPathDir(void) const; |
| 175 | |
| 176 | /* |
| 177 | * Retrieve the front (root dir) component. Optionally also return the |
| 178 | * remaining components. |
| 179 | * |
| 180 | * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") |
| 181 | * "/tmp" --> "tmp" (remain = "") |
| 182 | * "bar.c" --> "bar.c" (remain = "") |
| 183 | */ |
| 184 | String8 walkPath(String8* outRemains = NULL) const; |
| 185 | |
| 186 | /* |
Glenn Kasten | 29d4d20 | 2011-03-14 11:32:29 -0700 | [diff] [blame] | 187 | * Return the filename extension. This is the last '.' and any number |
| 188 | * of characters that follow it. The '.' is included in case we |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | * decide to expand our definition of what constitutes an extension. |
| 190 | * |
| 191 | * "/tmp/foo/bar.c" --> ".c" |
| 192 | * "/tmp" --> "" |
| 193 | * "/tmp/foo.bar/baz" --> "" |
| 194 | * "foo.jpeg" --> ".jpeg" |
| 195 | * "foo." --> "" |
| 196 | */ |
| 197 | String8 getPathExtension(void) const; |
| 198 | |
| 199 | /* |
| 200 | * Return the path without the extension. Rules for what constitutes |
| 201 | * an extension are described in the comment for getPathExtension(). |
| 202 | * |
| 203 | * "/tmp/foo/bar.c" --> "/tmp/foo/bar" |
| 204 | */ |
| 205 | String8 getBasePath(void) const; |
| 206 | |
| 207 | /* |
| 208 | * Add a component to the pathname. We guarantee that there is |
| 209 | * exactly one path separator between the old path and the new. |
| 210 | * If there is no existing name, we just copy the new name in. |
| 211 | * |
| 212 | * If leaf is a fully qualified path (i.e. starts with '/', it |
| 213 | * replaces whatever was there before. |
| 214 | */ |
| 215 | String8& appendPath(const char* leaf); |
| 216 | String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } |
| 217 | |
| 218 | /* |
| 219 | * Like appendPath(), but does not affect this string. Returns a new one instead. |
| 220 | */ |
| 221 | String8 appendPathCopy(const char* leaf) const |
| 222 | { String8 p(*this); p.appendPath(leaf); return p; } |
| 223 | String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } |
| 224 | |
| 225 | /* |
| 226 | * Converts all separators in this string to /, the default path separator. |
| 227 | * |
| 228 | * If the default OS separator is backslash, this converts all |
| 229 | * backslashes to slashes, in-place. Otherwise it does nothing. |
| 230 | * Returns self. |
| 231 | */ |
| 232 | String8& convertToResPath(); |
| 233 | |
| 234 | private: |
| 235 | status_t real_append(const char* other, size_t numChars); |
| 236 | char* find_extension(void) const; |
| 237 | |
| 238 | const char* mString; |
| 239 | }; |
| 240 | |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 241 | // String8 can be trivially moved using memcpy() because moving does not |
| 242 | // require any change to the underlying SharedBuffer contents or reference count. |
| 243 | ANDROID_TRIVIAL_MOVE_TRAIT(String8) |
| 244 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | // --------------------------------------------------------------------------- |
| 246 | // No user servicable parts below. |
| 247 | |
| 248 | inline int compare_type(const String8& lhs, const String8& rhs) |
| 249 | { |
| 250 | return lhs.compare(rhs); |
| 251 | } |
| 252 | |
| 253 | inline int strictly_order_type(const String8& lhs, const String8& rhs) |
| 254 | { |
| 255 | return compare_type(lhs, rhs) < 0; |
| 256 | } |
| 257 | |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 258 | inline const String8 String8::empty() { |
| 259 | return String8(); |
| 260 | } |
| 261 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | inline const char* String8::string() const |
| 263 | { |
| 264 | return mString; |
| 265 | } |
| 266 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 267 | inline std::string String8::std_string(const String8& str) |
| 268 | { |
| 269 | return std::string(str.string()); |
| 270 | } |
| 271 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | inline size_t String8::size() const |
| 273 | { |
| 274 | return length(); |
| 275 | } |
| 276 | |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 277 | inline bool String8::isEmpty() const |
| 278 | { |
| 279 | return length() == 0; |
| 280 | } |
| 281 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | inline size_t String8::bytes() const |
| 283 | { |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 284 | return length(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 287 | inline bool String8::contains(const char* other) const |
| 288 | { |
| 289 | return find(other) >= 0; |
| 290 | } |
| 291 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | { |
Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 312 | String8 tmp(*this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | { |
Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 325 | String8 tmp(*this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | tmp += other; |
| 327 | return tmp; |
| 328 | } |
| 329 | |
| 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 | { |
Brad Fitzpatrick | 9d589aa | 2010-10-20 17:06:28 -0700 | [diff] [blame] | 347 | return strcmp(mString, other.mString) == 0; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 400 | } // namespace android |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | |
| 402 | // --------------------------------------------------------------------------- |
| 403 | |
| 404 | #endif // ANDROID_STRING8_H |