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