media/omx-plugin/include/froyo/utils/String8.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial