1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/gb/utils/String8.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,472 @@ 1.4 +/* 1.5 + * Copyright (C) 2005 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef ANDROID_STRING8_H 1.21 +#define ANDROID_STRING8_H 1.22 + 1.23 +#include <utils/Errors.h> 1.24 + 1.25 +// Need this for the char16_t type; String8.h should not 1.26 +// be depedent on the String16 class. 1.27 +#include <utils/String16.h> 1.28 + 1.29 +#include <stdint.h> 1.30 +#include <string.h> 1.31 +#include <sys/types.h> 1.32 + 1.33 +// --------------------------------------------------------------------------- 1.34 + 1.35 +extern "C" { 1.36 + 1.37 +#if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98 1.38 +typedef uint32_t char32_t; 1.39 +#endif 1.40 + 1.41 +size_t strlen32(const char32_t *); 1.42 +size_t strnlen32(const char32_t *, size_t); 1.43 + 1.44 +/* 1.45 + * Returns the length of "src" when "src" is valid UTF-8 string. 1.46 + * Returns 0 if src is NULL, 0-length string or non UTF-8 string. 1.47 + * This function should be used to determine whether "src" is valid UTF-8 1.48 + * characters with valid unicode codepoints. "src" must be null-terminated. 1.49 + * 1.50 + * If you are going to use other GetUtf... functions defined in this header 1.51 + * with string which may not be valid UTF-8 with valid codepoint (form 0 to 1.52 + * 0x10FFFF), you should use this function before calling others, since the 1.53 + * other functions do not check whether the string is valid UTF-8 or not. 1.54 + * 1.55 + * If you do not care whether "src" is valid UTF-8 or not, you should use 1.56 + * strlen() as usual, which should be much faster. 1.57 + */ 1.58 +size_t utf8_length(const char *src); 1.59 + 1.60 +/* 1.61 + * Returns the UTF-32 length of "src". 1.62 + */ 1.63 +size_t utf32_length(const char *src, size_t src_len); 1.64 + 1.65 +/* 1.66 + * Returns the UTF-8 length of "src". 1.67 + */ 1.68 +size_t utf8_length_from_utf16(const char16_t *src, size_t src_len); 1.69 + 1.70 +/* 1.71 + * Returns the UTF-8 length of "src". 1.72 + */ 1.73 +size_t utf8_length_from_utf32(const char32_t *src, size_t src_len); 1.74 + 1.75 +/* 1.76 + * Returns the unicode value at "index". 1.77 + * Returns -1 when the index is invalid (equals to or more than "src_len"). 1.78 + * If returned value is positive, it is able to be converted to char32_t, which 1.79 + * is unsigned. Then, if "next_index" is not NULL, the next index to be used is 1.80 + * stored in "next_index". "next_index" can be NULL. 1.81 + */ 1.82 +int32_t utf32_at(const char *src, size_t src_len, 1.83 + size_t index, size_t *next_index); 1.84 + 1.85 +/* 1.86 + * Stores a UTF-32 string converted from "src" in "dst", if "dst_length" is not 1.87 + * large enough to store the string, the part of the "src" string is stored 1.88 + * into "dst". 1.89 + * Returns the size actually used for storing the string. 1.90 + * "dst" is not null-terminated when dst_len is fully used (like strncpy). 1.91 + */ 1.92 +size_t utf8_to_utf32(const char* src, size_t src_len, 1.93 + char32_t* dst, size_t dst_len); 1.94 + 1.95 +/* 1.96 + * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not 1.97 + * large enough to store the string, the part of the "src" string is stored 1.98 + * into "dst" as much as possible. See the examples for more detail. 1.99 + * Returns the size actually used for storing the string. 1.100 + * dst" is not null-terminated when dst_len is fully used (like strncpy). 1.101 + * 1.102 + * Example 1 1.103 + * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 1.104 + * "src_len" == 2 1.105 + * "dst_len" >= 7 1.106 + * -> 1.107 + * Returned value == 6 1.108 + * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0 1.109 + * (note that "dst" is null-terminated) 1.110 + * 1.111 + * Example 2 1.112 + * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 1.113 + * "src_len" == 2 1.114 + * "dst_len" == 5 1.115 + * -> 1.116 + * Returned value == 3 1.117 + * "dst" becomes \xE3\x81\x82\0 1.118 + * (note that "dst" is null-terminated, but \u3044 is not stored in "dst" 1.119 + * since "dst" does not have enough size to store the character) 1.120 + * 1.121 + * Example 3 1.122 + * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 1.123 + * "src_len" == 2 1.124 + * "dst_len" == 6 1.125 + * -> 1.126 + * Returned value == 6 1.127 + * "dst" becomes \xE3\x81\x82\xE3\x81\x84 1.128 + * (note that "dst" is NOT null-terminated, like strncpy) 1.129 + */ 1.130 +size_t utf32_to_utf8(const char32_t* src, size_t src_len, 1.131 + char* dst, size_t dst_len); 1.132 + 1.133 +size_t utf16_to_utf8(const char16_t* src, size_t src_len, 1.134 + char* dst, size_t dst_len); 1.135 + 1.136 +} 1.137 + 1.138 +// --------------------------------------------------------------------------- 1.139 + 1.140 +namespace android { 1.141 + 1.142 +class TextOutput; 1.143 + 1.144 +//! This is a string holding UTF-8 characters. Does not allow the value more 1.145 +// than 0x10FFFF, which is not valid unicode codepoint. 1.146 +class String8 1.147 +{ 1.148 +public: 1.149 + String8(); 1.150 + String8(const String8& o); 1.151 + explicit String8(const char* o); 1.152 + explicit String8(const char* o, size_t numChars); 1.153 + 1.154 + explicit String8(const String16& o); 1.155 + explicit String8(const char16_t* o); 1.156 + explicit String8(const char16_t* o, size_t numChars); 1.157 + explicit String8(const char32_t* o); 1.158 + explicit String8(const char32_t* o, size_t numChars); 1.159 + ~String8(); 1.160 + 1.161 + inline const char* string() const; 1.162 + inline size_t size() const; 1.163 + inline size_t length() const; 1.164 + inline size_t bytes() const; 1.165 + 1.166 + inline const SharedBuffer* sharedBuffer() const; 1.167 + 1.168 + void setTo(const String8& other); 1.169 + status_t setTo(const char* other); 1.170 + status_t setTo(const char* other, size_t numChars); 1.171 + status_t setTo(const char16_t* other, size_t numChars); 1.172 + status_t setTo(const char32_t* other, 1.173 + size_t length); 1.174 + 1.175 + status_t append(const String8& other); 1.176 + status_t append(const char* other); 1.177 + status_t append(const char* other, size_t numChars); 1.178 + 1.179 + status_t appendFormat(const char* fmt, ...) 1.180 + __attribute__((format (printf, 2, 3))); 1.181 + 1.182 + // Note that this function takes O(N) time to calculate the value. 1.183 + // No cache value is stored. 1.184 + size_t getUtf32Length() const; 1.185 + int32_t getUtf32At(size_t index, 1.186 + size_t *next_index) const; 1.187 + size_t getUtf32(char32_t* dst, size_t dst_len) const; 1.188 + 1.189 + inline String8& operator=(const String8& other); 1.190 + inline String8& operator=(const char* other); 1.191 + 1.192 + inline String8& operator+=(const String8& other); 1.193 + inline String8 operator+(const String8& other) const; 1.194 + 1.195 + inline String8& operator+=(const char* other); 1.196 + inline String8 operator+(const char* other) const; 1.197 + 1.198 + inline int compare(const String8& other) const; 1.199 + 1.200 + inline bool operator<(const String8& other) const; 1.201 + inline bool operator<=(const String8& other) const; 1.202 + inline bool operator==(const String8& other) const; 1.203 + inline bool operator!=(const String8& other) const; 1.204 + inline bool operator>=(const String8& other) const; 1.205 + inline bool operator>(const String8& other) const; 1.206 + 1.207 + inline bool operator<(const char* other) const; 1.208 + inline bool operator<=(const char* other) const; 1.209 + inline bool operator==(const char* other) const; 1.210 + inline bool operator!=(const char* other) const; 1.211 + inline bool operator>=(const char* other) const; 1.212 + inline bool operator>(const char* other) const; 1.213 + 1.214 + inline operator const char*() const; 1.215 + 1.216 + char* lockBuffer(size_t size); 1.217 + void unlockBuffer(); 1.218 + status_t unlockBuffer(size_t size); 1.219 + 1.220 + // return the index of the first byte of other in this at or after 1.221 + // start, or -1 if not found 1.222 + ssize_t find(const char* other, size_t start = 0) const; 1.223 + 1.224 + void toLower(); 1.225 + void toLower(size_t start, size_t numChars); 1.226 + void toUpper(); 1.227 + void toUpper(size_t start, size_t numChars); 1.228 + 1.229 + /* 1.230 + * These methods operate on the string as if it were a path name. 1.231 + */ 1.232 + 1.233 + /* 1.234 + * Set the filename field to a specific value. 1.235 + * 1.236 + * Normalizes the filename, removing a trailing '/' if present. 1.237 + */ 1.238 + void setPathName(const char* name); 1.239 + void setPathName(const char* name, size_t numChars); 1.240 + 1.241 + /* 1.242 + * Get just the filename component. 1.243 + * 1.244 + * "/tmp/foo/bar.c" --> "bar.c" 1.245 + */ 1.246 + String8 getPathLeaf(void) const; 1.247 + 1.248 + /* 1.249 + * Remove the last (file name) component, leaving just the directory 1.250 + * name. 1.251 + * 1.252 + * "/tmp/foo/bar.c" --> "/tmp/foo" 1.253 + * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX 1.254 + * "bar.c" --> "" 1.255 + */ 1.256 + String8 getPathDir(void) const; 1.257 + 1.258 + /* 1.259 + * Retrieve the front (root dir) component. Optionally also return the 1.260 + * remaining components. 1.261 + * 1.262 + * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") 1.263 + * "/tmp" --> "tmp" (remain = "") 1.264 + * "bar.c" --> "bar.c" (remain = "") 1.265 + */ 1.266 + String8 walkPath(String8* outRemains = NULL) const; 1.267 + 1.268 + /* 1.269 + * Return the filename extension. This is the last '.' and up to 1.270 + * four characters that follow it. The '.' is included in case we 1.271 + * decide to expand our definition of what constitutes an extension. 1.272 + * 1.273 + * "/tmp/foo/bar.c" --> ".c" 1.274 + * "/tmp" --> "" 1.275 + * "/tmp/foo.bar/baz" --> "" 1.276 + * "foo.jpeg" --> ".jpeg" 1.277 + * "foo." --> "" 1.278 + */ 1.279 + String8 getPathExtension(void) const; 1.280 + 1.281 + /* 1.282 + * Return the path without the extension. Rules for what constitutes 1.283 + * an extension are described in the comment for getPathExtension(). 1.284 + * 1.285 + * "/tmp/foo/bar.c" --> "/tmp/foo/bar" 1.286 + */ 1.287 + String8 getBasePath(void) const; 1.288 + 1.289 + /* 1.290 + * Add a component to the pathname. We guarantee that there is 1.291 + * exactly one path separator between the old path and the new. 1.292 + * If there is no existing name, we just copy the new name in. 1.293 + * 1.294 + * If leaf is a fully qualified path (i.e. starts with '/', it 1.295 + * replaces whatever was there before. 1.296 + */ 1.297 + String8& appendPath(const char* leaf); 1.298 + String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } 1.299 + 1.300 + /* 1.301 + * Like appendPath(), but does not affect this string. Returns a new one instead. 1.302 + */ 1.303 + String8 appendPathCopy(const char* leaf) const 1.304 + { String8 p(*this); p.appendPath(leaf); return p; } 1.305 + String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } 1.306 + 1.307 + /* 1.308 + * Converts all separators in this string to /, the default path separator. 1.309 + * 1.310 + * If the default OS separator is backslash, this converts all 1.311 + * backslashes to slashes, in-place. Otherwise it does nothing. 1.312 + * Returns self. 1.313 + */ 1.314 + String8& convertToResPath(); 1.315 + 1.316 +private: 1.317 + status_t real_append(const char* other, size_t numChars); 1.318 + char* find_extension(void) const; 1.319 + 1.320 + const char* mString; 1.321 +}; 1.322 + 1.323 +TextOutput& operator<<(TextOutput& to, const String16& val); 1.324 + 1.325 +// --------------------------------------------------------------------------- 1.326 +// No user servicable parts below. 1.327 + 1.328 +inline int compare_type(const String8& lhs, const String8& rhs) 1.329 +{ 1.330 + return lhs.compare(rhs); 1.331 +} 1.332 + 1.333 +inline int strictly_order_type(const String8& lhs, const String8& rhs) 1.334 +{ 1.335 + return compare_type(lhs, rhs) < 0; 1.336 +} 1.337 + 1.338 +inline const char* String8::string() const 1.339 +{ 1.340 + return mString; 1.341 +} 1.342 + 1.343 +inline size_t String8::length() const 1.344 +{ 1.345 + return SharedBuffer::sizeFromData(mString)-1; 1.346 +} 1.347 + 1.348 +inline size_t String8::size() const 1.349 +{ 1.350 + return length(); 1.351 +} 1.352 + 1.353 +inline size_t String8::bytes() const 1.354 +{ 1.355 + return SharedBuffer::sizeFromData(mString)-1; 1.356 +} 1.357 + 1.358 +inline const SharedBuffer* String8::sharedBuffer() const 1.359 +{ 1.360 + return SharedBuffer::bufferFromData(mString); 1.361 +} 1.362 + 1.363 +inline String8& String8::operator=(const String8& other) 1.364 +{ 1.365 + setTo(other); 1.366 + return *this; 1.367 +} 1.368 + 1.369 +inline String8& String8::operator=(const char* other) 1.370 +{ 1.371 + setTo(other); 1.372 + return *this; 1.373 +} 1.374 + 1.375 +inline String8& String8::operator+=(const String8& other) 1.376 +{ 1.377 + append(other); 1.378 + return *this; 1.379 +} 1.380 + 1.381 +inline String8 String8::operator+(const String8& other) const 1.382 +{ 1.383 + String8 tmp(*this); 1.384 + tmp += other; 1.385 + return tmp; 1.386 +} 1.387 + 1.388 +inline String8& String8::operator+=(const char* other) 1.389 +{ 1.390 + append(other); 1.391 + return *this; 1.392 +} 1.393 + 1.394 +inline String8 String8::operator+(const char* other) const 1.395 +{ 1.396 + String8 tmp(*this); 1.397 + tmp += other; 1.398 + return tmp; 1.399 +} 1.400 + 1.401 +inline int String8::compare(const String8& other) const 1.402 +{ 1.403 + return strcmp(mString, other.mString); 1.404 +} 1.405 + 1.406 +inline bool String8::operator<(const String8& other) const 1.407 +{ 1.408 + return strcmp(mString, other.mString) < 0; 1.409 +} 1.410 + 1.411 +inline bool String8::operator<=(const String8& other) const 1.412 +{ 1.413 + return strcmp(mString, other.mString) <= 0; 1.414 +} 1.415 + 1.416 +inline bool String8::operator==(const String8& other) const 1.417 +{ 1.418 + return strcmp(mString, other.mString) == 0; 1.419 +} 1.420 + 1.421 +inline bool String8::operator!=(const String8& other) const 1.422 +{ 1.423 + return strcmp(mString, other.mString) != 0; 1.424 +} 1.425 + 1.426 +inline bool String8::operator>=(const String8& other) const 1.427 +{ 1.428 + return strcmp(mString, other.mString) >= 0; 1.429 +} 1.430 + 1.431 +inline bool String8::operator>(const String8& other) const 1.432 +{ 1.433 + return strcmp(mString, other.mString) > 0; 1.434 +} 1.435 + 1.436 +inline bool String8::operator<(const char* other) const 1.437 +{ 1.438 + return strcmp(mString, other) < 0; 1.439 +} 1.440 + 1.441 +inline bool String8::operator<=(const char* other) const 1.442 +{ 1.443 + return strcmp(mString, other) <= 0; 1.444 +} 1.445 + 1.446 +inline bool String8::operator==(const char* other) const 1.447 +{ 1.448 + return strcmp(mString, other) == 0; 1.449 +} 1.450 + 1.451 +inline bool String8::operator!=(const char* other) const 1.452 +{ 1.453 + return strcmp(mString, other) != 0; 1.454 +} 1.455 + 1.456 +inline bool String8::operator>=(const char* other) const 1.457 +{ 1.458 + return strcmp(mString, other) >= 0; 1.459 +} 1.460 + 1.461 +inline bool String8::operator>(const char* other) const 1.462 +{ 1.463 + return strcmp(mString, other) > 0; 1.464 +} 1.465 + 1.466 +inline String8::operator const char*() const 1.467 +{ 1.468 + return mString; 1.469 +} 1.470 + 1.471 +} // namespace android 1.472 + 1.473 +// --------------------------------------------------------------------------- 1.474 + 1.475 +#endif // ANDROID_STRING8_H