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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial