The Android Open Source Project | edbf3b6 | 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 | |
| 20 | #include <utils/Errors.h> |
Kenny Root | c412dcb | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 21 | #include <utils/SharedBuffer.h> |
| 22 | #include <utils/Unicode.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Kenny Root | c412dcb | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 24 | #include <string.h> // for strcmp |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame^] | 25 | #include <stdarg.h> |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Kenny Root | c412dcb | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 31 | class String16; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | class TextOutput; |
| 33 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 34 | //! This is a string holding UTF-8 characters. Does not allow the value more |
| 35 | // than 0x10FFFF, which is not valid unicode codepoint. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | class String8 |
| 37 | { |
| 38 | public: |
| 39 | String8(); |
| 40 | String8(const String8& o); |
| 41 | explicit String8(const char* o); |
| 42 | explicit String8(const char* o, size_t numChars); |
| 43 | |
| 44 | explicit String8(const String16& o); |
| 45 | explicit String8(const char16_t* o); |
| 46 | explicit String8(const char16_t* o, size_t numChars); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 47 | explicit String8(const char32_t* o); |
| 48 | explicit String8(const char32_t* o, size_t numChars); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | ~String8(); |
| 50 | |
| 51 | inline const char* string() const; |
| 52 | inline size_t size() const; |
| 53 | inline size_t length() const; |
| 54 | inline size_t bytes() const; |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 55 | inline bool isEmpty() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | inline const SharedBuffer* sharedBuffer() const; |
| 58 | |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 59 | void clear(); |
| 60 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | void setTo(const String8& other); |
| 62 | status_t setTo(const char* other); |
| 63 | status_t setTo(const char* other, size_t numChars); |
| 64 | status_t setTo(const char16_t* other, size_t numChars); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 65 | status_t setTo(const char32_t* other, |
| 66 | size_t length); |
| 67 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | status_t append(const String8& other); |
| 69 | status_t append(const char* other); |
| 70 | status_t append(const char* other, size_t numChars); |
| 71 | |
Jeff Brown | 3f9ff20 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 72 | status_t appendFormat(const char* fmt, ...) |
| 73 | __attribute__((format (printf, 2, 3))); |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame^] | 74 | status_t appendFormatV(const char* fmt, va_list args); |
Jeff Brown | 0a128e3 | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 75 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 76 | // Note that this function takes O(N) time to calculate the value. |
| 77 | // No cache value is stored. |
| 78 | size_t getUtf32Length() const; |
| 79 | int32_t getUtf32At(size_t index, |
| 80 | size_t *next_index) const; |
Kenny Root | c412dcb | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 81 | void getUtf32(char32_t* dst) const; |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 82 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | inline String8& operator=(const String8& other); |
| 84 | inline String8& operator=(const char* other); |
| 85 | |
| 86 | inline String8& operator+=(const String8& other); |
| 87 | inline String8 operator+(const String8& other) const; |
| 88 | |
| 89 | inline String8& operator+=(const char* other); |
| 90 | inline String8 operator+(const char* other) const; |
| 91 | |
| 92 | inline int compare(const String8& other) const; |
| 93 | |
| 94 | inline bool operator<(const String8& other) const; |
| 95 | inline bool operator<=(const String8& other) const; |
| 96 | inline bool operator==(const String8& other) const; |
| 97 | inline bool operator!=(const String8& other) const; |
| 98 | inline bool operator>=(const String8& other) const; |
| 99 | inline bool operator>(const String8& other) const; |
| 100 | |
| 101 | inline bool operator<(const char* other) const; |
| 102 | inline bool operator<=(const char* other) const; |
| 103 | inline bool operator==(const char* other) const; |
| 104 | inline bool operator!=(const char* other) const; |
| 105 | inline bool operator>=(const char* other) const; |
| 106 | inline bool operator>(const char* other) const; |
| 107 | |
| 108 | inline operator const char*() const; |
| 109 | |
| 110 | char* lockBuffer(size_t size); |
| 111 | void unlockBuffer(); |
| 112 | status_t unlockBuffer(size_t size); |
| 113 | |
| 114 | // return the index of the first byte of other in this at or after |
| 115 | // start, or -1 if not found |
| 116 | ssize_t find(const char* other, size_t start = 0) const; |
| 117 | |
| 118 | void toLower(); |
| 119 | void toLower(size_t start, size_t numChars); |
| 120 | void toUpper(); |
| 121 | void toUpper(size_t start, size_t numChars); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 122 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | /* |
| 124 | * These methods operate on the string as if it were a path name. |
| 125 | */ |
| 126 | |
| 127 | /* |
| 128 | * Set the filename field to a specific value. |
| 129 | * |
| 130 | * Normalizes the filename, removing a trailing '/' if present. |
| 131 | */ |
| 132 | void setPathName(const char* name); |
| 133 | void setPathName(const char* name, size_t numChars); |
| 134 | |
| 135 | /* |
| 136 | * Get just the filename component. |
| 137 | * |
| 138 | * "/tmp/foo/bar.c" --> "bar.c" |
| 139 | */ |
| 140 | String8 getPathLeaf(void) const; |
| 141 | |
| 142 | /* |
| 143 | * Remove the last (file name) component, leaving just the directory |
| 144 | * name. |
| 145 | * |
| 146 | * "/tmp/foo/bar.c" --> "/tmp/foo" |
| 147 | * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX |
| 148 | * "bar.c" --> "" |
| 149 | */ |
| 150 | String8 getPathDir(void) const; |
| 151 | |
| 152 | /* |
| 153 | * Retrieve the front (root dir) component. Optionally also return the |
| 154 | * remaining components. |
| 155 | * |
| 156 | * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") |
| 157 | * "/tmp" --> "tmp" (remain = "") |
| 158 | * "bar.c" --> "bar.c" (remain = "") |
| 159 | */ |
| 160 | String8 walkPath(String8* outRemains = NULL) const; |
| 161 | |
| 162 | /* |
| 163 | * Return the filename extension. This is the last '.' and up to |
| 164 | * four characters that follow it. The '.' is included in case we |
| 165 | * decide to expand our definition of what constitutes an extension. |
| 166 | * |
| 167 | * "/tmp/foo/bar.c" --> ".c" |
| 168 | * "/tmp" --> "" |
| 169 | * "/tmp/foo.bar/baz" --> "" |
| 170 | * "foo.jpeg" --> ".jpeg" |
| 171 | * "foo." --> "" |
| 172 | */ |
| 173 | String8 getPathExtension(void) const; |
| 174 | |
| 175 | /* |
| 176 | * Return the path without the extension. Rules for what constitutes |
| 177 | * an extension are described in the comment for getPathExtension(). |
| 178 | * |
| 179 | * "/tmp/foo/bar.c" --> "/tmp/foo/bar" |
| 180 | */ |
| 181 | String8 getBasePath(void) const; |
| 182 | |
| 183 | /* |
| 184 | * Add a component to the pathname. We guarantee that there is |
| 185 | * exactly one path separator between the old path and the new. |
| 186 | * If there is no existing name, we just copy the new name in. |
| 187 | * |
| 188 | * If leaf is a fully qualified path (i.e. starts with '/', it |
| 189 | * replaces whatever was there before. |
| 190 | */ |
| 191 | String8& appendPath(const char* leaf); |
| 192 | String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } |
| 193 | |
| 194 | /* |
| 195 | * Like appendPath(), but does not affect this string. Returns a new one instead. |
| 196 | */ |
| 197 | String8 appendPathCopy(const char* leaf) const |
| 198 | { String8 p(*this); p.appendPath(leaf); return p; } |
| 199 | String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } |
| 200 | |
| 201 | /* |
| 202 | * Converts all separators in this string to /, the default path separator. |
| 203 | * |
| 204 | * If the default OS separator is backslash, this converts all |
| 205 | * backslashes to slashes, in-place. Otherwise it does nothing. |
| 206 | * Returns self. |
| 207 | */ |
| 208 | String8& convertToResPath(); |
| 209 | |
| 210 | private: |
| 211 | status_t real_append(const char* other, size_t numChars); |
| 212 | char* find_extension(void) const; |
| 213 | |
| 214 | const char* mString; |
| 215 | }; |
| 216 | |
| 217 | TextOutput& operator<<(TextOutput& to, const String16& val); |
| 218 | |
| 219 | // --------------------------------------------------------------------------- |
| 220 | // No user servicable parts below. |
| 221 | |
| 222 | inline int compare_type(const String8& lhs, const String8& rhs) |
| 223 | { |
| 224 | return lhs.compare(rhs); |
| 225 | } |
| 226 | |
| 227 | inline int strictly_order_type(const String8& lhs, const String8& rhs) |
| 228 | { |
| 229 | return compare_type(lhs, rhs) < 0; |
| 230 | } |
| 231 | |
| 232 | inline const char* String8::string() const |
| 233 | { |
| 234 | return mString; |
| 235 | } |
| 236 | |
| 237 | inline size_t String8::length() const |
| 238 | { |
| 239 | return SharedBuffer::sizeFromData(mString)-1; |
| 240 | } |
| 241 | |
| 242 | inline size_t String8::size() const |
| 243 | { |
| 244 | return length(); |
| 245 | } |
| 246 | |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 247 | inline bool String8::isEmpty() const |
| 248 | { |
| 249 | return length() == 0; |
| 250 | } |
| 251 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | inline size_t String8::bytes() const |
| 253 | { |
| 254 | return SharedBuffer::sizeFromData(mString)-1; |
| 255 | } |
| 256 | |
| 257 | inline const SharedBuffer* String8::sharedBuffer() const |
| 258 | { |
| 259 | return SharedBuffer::bufferFromData(mString); |
| 260 | } |
| 261 | |
| 262 | inline String8& String8::operator=(const String8& other) |
| 263 | { |
| 264 | setTo(other); |
| 265 | return *this; |
| 266 | } |
| 267 | |
| 268 | inline String8& String8::operator=(const char* other) |
| 269 | { |
| 270 | setTo(other); |
| 271 | return *this; |
| 272 | } |
| 273 | |
| 274 | inline String8& String8::operator+=(const String8& other) |
| 275 | { |
| 276 | append(other); |
| 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | inline String8 String8::operator+(const String8& other) const |
| 281 | { |
Kenny Root | aa96663 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 282 | String8 tmp(*this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | tmp += other; |
| 284 | return tmp; |
| 285 | } |
| 286 | |
| 287 | inline String8& String8::operator+=(const char* other) |
| 288 | { |
| 289 | append(other); |
| 290 | return *this; |
| 291 | } |
| 292 | |
| 293 | inline String8 String8::operator+(const char* other) const |
| 294 | { |
Kenny Root | aa96663 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 295 | String8 tmp(*this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | tmp += other; |
| 297 | return tmp; |
| 298 | } |
| 299 | |
| 300 | inline int String8::compare(const String8& other) const |
| 301 | { |
| 302 | return strcmp(mString, other.mString); |
| 303 | } |
| 304 | |
| 305 | inline bool String8::operator<(const String8& other) const |
| 306 | { |
| 307 | return strcmp(mString, other.mString) < 0; |
| 308 | } |
| 309 | |
| 310 | inline bool String8::operator<=(const String8& other) const |
| 311 | { |
| 312 | return strcmp(mString, other.mString) <= 0; |
| 313 | } |
| 314 | |
| 315 | inline bool String8::operator==(const String8& other) const |
| 316 | { |
Brad Fitzpatrick | e22d214 | 2010-10-20 17:06:28 -0700 | [diff] [blame] | 317 | return strcmp(mString, other.mString) == 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | inline bool String8::operator!=(const String8& other) const |
| 321 | { |
| 322 | return strcmp(mString, other.mString) != 0; |
| 323 | } |
| 324 | |
| 325 | inline bool String8::operator>=(const String8& other) const |
| 326 | { |
| 327 | return strcmp(mString, other.mString) >= 0; |
| 328 | } |
| 329 | |
| 330 | inline bool String8::operator>(const String8& other) const |
| 331 | { |
| 332 | return strcmp(mString, other.mString) > 0; |
| 333 | } |
| 334 | |
| 335 | inline bool String8::operator<(const char* other) const |
| 336 | { |
| 337 | return strcmp(mString, other) < 0; |
| 338 | } |
| 339 | |
| 340 | inline bool String8::operator<=(const char* other) const |
| 341 | { |
| 342 | return strcmp(mString, other) <= 0; |
| 343 | } |
| 344 | |
| 345 | inline bool String8::operator==(const char* other) const |
| 346 | { |
| 347 | return strcmp(mString, other) == 0; |
| 348 | } |
| 349 | |
| 350 | inline bool String8::operator!=(const char* other) const |
| 351 | { |
| 352 | return strcmp(mString, other) != 0; |
| 353 | } |
| 354 | |
| 355 | inline bool String8::operator>=(const char* other) const |
| 356 | { |
| 357 | return strcmp(mString, other) >= 0; |
| 358 | } |
| 359 | |
| 360 | inline bool String8::operator>(const char* other) const |
| 361 | { |
| 362 | return strcmp(mString, other) > 0; |
| 363 | } |
| 364 | |
| 365 | inline String8::operator const char*() const |
| 366 | { |
| 367 | return mString; |
| 368 | } |
| 369 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 370 | } // namespace android |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | |
| 372 | // --------------------------------------------------------------------------- |
| 373 | |
| 374 | #endif // ANDROID_STRING8_H |