The Android Open Source Project | 9066cfe | 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> |
| 21 | |
| 22 | // Need this for the char16_t type; String8.h should not |
| 23 | // be depedent on the String16 class. |
| 24 | #include <utils/String16.h> |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 32 | extern "C" { |
| 33 | |
| 34 | typedef uint32_t char32_t; |
| 35 | |
| 36 | size_t strlen32(const char32_t *); |
| 37 | size_t strnlen32(const char32_t *, size_t); |
| 38 | |
| 39 | /* |
| 40 | * Returns the length of "src" when "src" is valid UTF-8 string. |
| 41 | * Returns 0 if src is NULL, 0-length string or non UTF-8 string. |
| 42 | * This function should be used to determine whether "src" is valid UTF-8 |
| 43 | * characters with valid unicode codepoints. "src" must be null-terminated. |
| 44 | * |
| 45 | * If you are going to use other GetUtf... functions defined in this header |
| 46 | * with string which may not be valid UTF-8 with valid codepoint (form 0 to |
| 47 | * 0x10FFFF), you should use this function before calling others, since the |
| 48 | * other functions do not check whether the string is valid UTF-8 or not. |
| 49 | * |
| 50 | * If you do not care whether "src" is valid UTF-8 or not, you should use |
| 51 | * strlen() as usual, which should be much faster. |
| 52 | */ |
| 53 | size_t utf8_length(const char *src); |
| 54 | |
| 55 | /* |
| 56 | * Returns the UTF-32 length of "src". |
| 57 | */ |
| 58 | size_t utf32_length(const char *src, size_t src_len); |
| 59 | |
| 60 | /* |
| 61 | * Returns the UTF-8 length of "src". |
| 62 | */ |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 63 | size_t utf8_length_from_utf16(const char16_t *src, size_t src_len); |
| 64 | |
| 65 | /* |
| 66 | * Returns the UTF-8 length of "src". |
| 67 | */ |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 68 | size_t utf8_length_from_utf32(const char32_t *src, size_t src_len); |
| 69 | |
| 70 | /* |
| 71 | * Returns the unicode value at "index". |
| 72 | * Returns -1 when the index is invalid (equals to or more than "src_len"). |
| 73 | * If returned value is positive, it is able to be converted to char32_t, which |
| 74 | * is unsigned. Then, if "next_index" is not NULL, the next index to be used is |
| 75 | * stored in "next_index". "next_index" can be NULL. |
| 76 | */ |
| 77 | int32_t utf32_at(const char *src, size_t src_len, |
| 78 | size_t index, size_t *next_index); |
| 79 | |
| 80 | /* |
| 81 | * Stores a UTF-32 string converted from "src" in "dst", if "dst_length" is not |
| 82 | * large enough to store the string, the part of the "src" string is stored |
| 83 | * into "dst". |
| 84 | * Returns the size actually used for storing the string. |
| 85 | * "dst" is not null-terminated when dst_len is fully used (like strncpy). |
| 86 | */ |
| 87 | size_t utf8_to_utf32(const char* src, size_t src_len, |
| 88 | char32_t* dst, size_t dst_len); |
| 89 | |
| 90 | /* |
| 91 | * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not |
| 92 | * large enough to store the string, the part of the "src" string is stored |
| 93 | * into "dst" as much as possible. See the examples for more detail. |
| 94 | * Returns the size actually used for storing the string. |
| 95 | * dst" is not null-terminated when dst_len is fully used (like strncpy). |
| 96 | * |
| 97 | * Example 1 |
| 98 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
| 99 | * "src_len" == 2 |
| 100 | * "dst_len" >= 7 |
| 101 | * -> |
| 102 | * Returned value == 6 |
| 103 | * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0 |
| 104 | * (note that "dst" is null-terminated) |
| 105 | * |
| 106 | * Example 2 |
| 107 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
| 108 | * "src_len" == 2 |
| 109 | * "dst_len" == 5 |
| 110 | * -> |
| 111 | * Returned value == 3 |
| 112 | * "dst" becomes \xE3\x81\x82\0 |
| 113 | * (note that "dst" is null-terminated, but \u3044 is not stored in "dst" |
| 114 | * since "dst" does not have enough size to store the character) |
| 115 | * |
| 116 | * Example 3 |
| 117 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
| 118 | * "src_len" == 2 |
| 119 | * "dst_len" == 6 |
| 120 | * -> |
| 121 | * Returned value == 6 |
| 122 | * "dst" becomes \xE3\x81\x82\xE3\x81\x84 |
| 123 | * (note that "dst" is NOT null-terminated, like strncpy) |
| 124 | */ |
| 125 | size_t utf32_to_utf8(const char32_t* src, size_t src_len, |
| 126 | char* dst, size_t dst_len); |
| 127 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 128 | size_t utf16_to_utf8(const char16_t* src, size_t src_len, |
| 129 | char* dst, size_t dst_len); |
| 130 | |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // --------------------------------------------------------------------------- |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | namespace android { |
| 136 | |
| 137 | class TextOutput; |
| 138 | |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 139 | //! This is a string holding UTF-8 characters. Does not allow the value more |
| 140 | // than 0x10FFFF, which is not valid unicode codepoint. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | class String8 |
| 142 | { |
| 143 | public: |
| 144 | String8(); |
| 145 | String8(const String8& o); |
| 146 | explicit String8(const char* o); |
| 147 | explicit String8(const char* o, size_t numChars); |
| 148 | |
| 149 | explicit String8(const String16& o); |
| 150 | explicit String8(const char16_t* o); |
| 151 | explicit String8(const char16_t* o, size_t numChars); |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 152 | explicit String8(const char32_t* o); |
| 153 | explicit String8(const char32_t* o, size_t numChars); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | ~String8(); |
| 155 | |
| 156 | inline const char* string() const; |
| 157 | inline size_t size() const; |
| 158 | inline size_t length() const; |
| 159 | inline size_t bytes() const; |
| 160 | |
| 161 | inline const SharedBuffer* sharedBuffer() const; |
| 162 | |
| 163 | void setTo(const String8& other); |
| 164 | status_t setTo(const char* other); |
| 165 | status_t setTo(const char* other, size_t numChars); |
| 166 | status_t setTo(const char16_t* other, size_t numChars); |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 167 | status_t setTo(const char32_t* other, |
| 168 | size_t length); |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | status_t append(const String8& other); |
| 171 | status_t append(const char* other); |
| 172 | status_t append(const char* other, size_t numChars); |
| 173 | |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 174 | // Note that this function takes O(N) time to calculate the value. |
| 175 | // No cache value is stored. |
| 176 | size_t getUtf32Length() const; |
| 177 | int32_t getUtf32At(size_t index, |
| 178 | size_t *next_index) const; |
| 179 | size_t getUtf32(char32_t* dst, size_t dst_len) const; |
| 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | inline String8& operator=(const String8& other); |
| 182 | inline String8& operator=(const char* other); |
| 183 | |
| 184 | inline String8& operator+=(const String8& other); |
| 185 | inline String8 operator+(const String8& other) const; |
| 186 | |
| 187 | inline String8& operator+=(const char* other); |
| 188 | inline String8 operator+(const char* other) const; |
| 189 | |
| 190 | inline int compare(const String8& other) const; |
| 191 | |
| 192 | inline bool operator<(const String8& other) const; |
| 193 | inline bool operator<=(const String8& other) const; |
| 194 | inline bool operator==(const String8& other) const; |
| 195 | inline bool operator!=(const String8& other) const; |
| 196 | inline bool operator>=(const String8& other) const; |
| 197 | inline bool operator>(const String8& other) const; |
| 198 | |
| 199 | inline bool operator<(const char* other) const; |
| 200 | inline bool operator<=(const char* other) const; |
| 201 | inline bool operator==(const char* other) const; |
| 202 | inline bool operator!=(const char* other) const; |
| 203 | inline bool operator>=(const char* other) const; |
| 204 | inline bool operator>(const char* other) const; |
| 205 | |
| 206 | inline operator const char*() const; |
| 207 | |
| 208 | char* lockBuffer(size_t size); |
| 209 | void unlockBuffer(); |
| 210 | status_t unlockBuffer(size_t size); |
| 211 | |
| 212 | // return the index of the first byte of other in this at or after |
| 213 | // start, or -1 if not found |
| 214 | ssize_t find(const char* other, size_t start = 0) const; |
| 215 | |
| 216 | void toLower(); |
| 217 | void toLower(size_t start, size_t numChars); |
| 218 | void toUpper(); |
| 219 | void toUpper(size_t start, size_t numChars); |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | /* |
| 222 | * These methods operate on the string as if it were a path name. |
| 223 | */ |
| 224 | |
| 225 | /* |
| 226 | * Set the filename field to a specific value. |
| 227 | * |
| 228 | * Normalizes the filename, removing a trailing '/' if present. |
| 229 | */ |
| 230 | void setPathName(const char* name); |
| 231 | void setPathName(const char* name, size_t numChars); |
| 232 | |
| 233 | /* |
| 234 | * Get just the filename component. |
| 235 | * |
| 236 | * "/tmp/foo/bar.c" --> "bar.c" |
| 237 | */ |
| 238 | String8 getPathLeaf(void) const; |
| 239 | |
| 240 | /* |
| 241 | * Remove the last (file name) component, leaving just the directory |
| 242 | * name. |
| 243 | * |
| 244 | * "/tmp/foo/bar.c" --> "/tmp/foo" |
| 245 | * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX |
| 246 | * "bar.c" --> "" |
| 247 | */ |
| 248 | String8 getPathDir(void) const; |
| 249 | |
| 250 | /* |
| 251 | * Retrieve the front (root dir) component. Optionally also return the |
| 252 | * remaining components. |
| 253 | * |
| 254 | * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") |
| 255 | * "/tmp" --> "tmp" (remain = "") |
| 256 | * "bar.c" --> "bar.c" (remain = "") |
| 257 | */ |
| 258 | String8 walkPath(String8* outRemains = NULL) const; |
| 259 | |
| 260 | /* |
| 261 | * Return the filename extension. This is the last '.' and up to |
| 262 | * four characters that follow it. The '.' is included in case we |
| 263 | * decide to expand our definition of what constitutes an extension. |
| 264 | * |
| 265 | * "/tmp/foo/bar.c" --> ".c" |
| 266 | * "/tmp" --> "" |
| 267 | * "/tmp/foo.bar/baz" --> "" |
| 268 | * "foo.jpeg" --> ".jpeg" |
| 269 | * "foo." --> "" |
| 270 | */ |
| 271 | String8 getPathExtension(void) const; |
| 272 | |
| 273 | /* |
| 274 | * Return the path without the extension. Rules for what constitutes |
| 275 | * an extension are described in the comment for getPathExtension(). |
| 276 | * |
| 277 | * "/tmp/foo/bar.c" --> "/tmp/foo/bar" |
| 278 | */ |
| 279 | String8 getBasePath(void) const; |
| 280 | |
| 281 | /* |
| 282 | * Add a component to the pathname. We guarantee that there is |
| 283 | * exactly one path separator between the old path and the new. |
| 284 | * If there is no existing name, we just copy the new name in. |
| 285 | * |
| 286 | * If leaf is a fully qualified path (i.e. starts with '/', it |
| 287 | * replaces whatever was there before. |
| 288 | */ |
| 289 | String8& appendPath(const char* leaf); |
| 290 | String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } |
| 291 | |
| 292 | /* |
| 293 | * Like appendPath(), but does not affect this string. Returns a new one instead. |
| 294 | */ |
| 295 | String8 appendPathCopy(const char* leaf) const |
| 296 | { String8 p(*this); p.appendPath(leaf); return p; } |
| 297 | String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } |
| 298 | |
| 299 | /* |
| 300 | * Converts all separators in this string to /, the default path separator. |
| 301 | * |
| 302 | * If the default OS separator is backslash, this converts all |
| 303 | * backslashes to slashes, in-place. Otherwise it does nothing. |
| 304 | * Returns self. |
| 305 | */ |
| 306 | String8& convertToResPath(); |
| 307 | |
| 308 | private: |
| 309 | status_t real_append(const char* other, size_t numChars); |
| 310 | char* find_extension(void) const; |
| 311 | |
| 312 | const char* mString; |
| 313 | }; |
| 314 | |
| 315 | TextOutput& operator<<(TextOutput& to, const String16& val); |
| 316 | |
| 317 | // --------------------------------------------------------------------------- |
| 318 | // No user servicable parts below. |
| 319 | |
| 320 | inline int compare_type(const String8& lhs, const String8& rhs) |
| 321 | { |
| 322 | return lhs.compare(rhs); |
| 323 | } |
| 324 | |
| 325 | inline int strictly_order_type(const String8& lhs, const String8& rhs) |
| 326 | { |
| 327 | return compare_type(lhs, rhs) < 0; |
| 328 | } |
| 329 | |
| 330 | inline const char* String8::string() const |
| 331 | { |
| 332 | return mString; |
| 333 | } |
| 334 | |
| 335 | inline size_t String8::length() const |
| 336 | { |
| 337 | return SharedBuffer::sizeFromData(mString)-1; |
| 338 | } |
| 339 | |
| 340 | inline size_t String8::size() const |
| 341 | { |
| 342 | return length(); |
| 343 | } |
| 344 | |
| 345 | inline size_t String8::bytes() const |
| 346 | { |
| 347 | return SharedBuffer::sizeFromData(mString)-1; |
| 348 | } |
| 349 | |
| 350 | inline const SharedBuffer* String8::sharedBuffer() const |
| 351 | { |
| 352 | return SharedBuffer::bufferFromData(mString); |
| 353 | } |
| 354 | |
| 355 | inline String8& String8::operator=(const String8& other) |
| 356 | { |
| 357 | setTo(other); |
| 358 | return *this; |
| 359 | } |
| 360 | |
| 361 | inline String8& String8::operator=(const char* other) |
| 362 | { |
| 363 | setTo(other); |
| 364 | return *this; |
| 365 | } |
| 366 | |
| 367 | inline String8& String8::operator+=(const String8& other) |
| 368 | { |
| 369 | append(other); |
| 370 | return *this; |
| 371 | } |
| 372 | |
| 373 | inline String8 String8::operator+(const String8& other) const |
| 374 | { |
| 375 | String8 tmp; |
| 376 | tmp += other; |
| 377 | return tmp; |
| 378 | } |
| 379 | |
| 380 | inline String8& String8::operator+=(const char* other) |
| 381 | { |
| 382 | append(other); |
| 383 | return *this; |
| 384 | } |
| 385 | |
| 386 | inline String8 String8::operator+(const char* other) const |
| 387 | { |
| 388 | String8 tmp; |
| 389 | tmp += other; |
| 390 | return tmp; |
| 391 | } |
| 392 | |
| 393 | inline int String8::compare(const String8& other) const |
| 394 | { |
| 395 | return strcmp(mString, other.mString); |
| 396 | } |
| 397 | |
| 398 | inline bool String8::operator<(const String8& other) const |
| 399 | { |
| 400 | return strcmp(mString, other.mString) < 0; |
| 401 | } |
| 402 | |
| 403 | inline bool String8::operator<=(const String8& other) const |
| 404 | { |
| 405 | return strcmp(mString, other.mString) <= 0; |
| 406 | } |
| 407 | |
| 408 | inline bool String8::operator==(const String8& other) const |
| 409 | { |
| 410 | return strcmp(mString, other.mString) == 0; |
| 411 | } |
| 412 | |
| 413 | inline bool String8::operator!=(const String8& other) const |
| 414 | { |
| 415 | return strcmp(mString, other.mString) != 0; |
| 416 | } |
| 417 | |
| 418 | inline bool String8::operator>=(const String8& other) const |
| 419 | { |
| 420 | return strcmp(mString, other.mString) >= 0; |
| 421 | } |
| 422 | |
| 423 | inline bool String8::operator>(const String8& other) const |
| 424 | { |
| 425 | return strcmp(mString, other.mString) > 0; |
| 426 | } |
| 427 | |
| 428 | inline bool String8::operator<(const char* other) const |
| 429 | { |
| 430 | return strcmp(mString, other) < 0; |
| 431 | } |
| 432 | |
| 433 | inline bool String8::operator<=(const char* other) const |
| 434 | { |
| 435 | return strcmp(mString, other) <= 0; |
| 436 | } |
| 437 | |
| 438 | inline bool String8::operator==(const char* other) const |
| 439 | { |
| 440 | return strcmp(mString, other) == 0; |
| 441 | } |
| 442 | |
| 443 | inline bool String8::operator!=(const char* other) const |
| 444 | { |
| 445 | return strcmp(mString, other) != 0; |
| 446 | } |
| 447 | |
| 448 | inline bool String8::operator>=(const char* other) const |
| 449 | { |
| 450 | return strcmp(mString, other) >= 0; |
| 451 | } |
| 452 | |
| 453 | inline bool String8::operator>(const char* other) const |
| 454 | { |
| 455 | return strcmp(mString, other) > 0; |
| 456 | } |
| 457 | |
| 458 | inline String8::operator const char*() const |
| 459 | { |
| 460 | return mString; |
| 461 | } |
| 462 | |
Daisuke Miyakawa | f05b33b | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 463 | } // namespace android |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | |
| 465 | // --------------------------------------------------------------------------- |
| 466 | |
| 467 | #endif // ANDROID_STRING8_H |