1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/froyo/utils/String8.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,469 @@ 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 + // Note that this function takes O(N) time to calculate the value. 1.180 + // No cache value is stored. 1.181 + size_t getUtf32Length() const; 1.182 + int32_t getUtf32At(size_t index, 1.183 + size_t *next_index) const; 1.184 + size_t getUtf32(char32_t* dst, size_t dst_len) const; 1.185 + 1.186 + inline String8& operator=(const String8& other); 1.187 + inline String8& operator=(const char* other); 1.188 + 1.189 + inline String8& operator+=(const String8& other); 1.190 + inline String8 operator+(const String8& other) const; 1.191 + 1.192 + inline String8& operator+=(const char* other); 1.193 + inline String8 operator+(const char* other) const; 1.194 + 1.195 + inline int compare(const String8& other) const; 1.196 + 1.197 + inline bool operator<(const String8& other) const; 1.198 + inline bool operator<=(const String8& other) const; 1.199 + inline bool operator==(const String8& other) const; 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 + 1.204 + inline bool operator<(const char* other) const; 1.205 + inline bool operator<=(const char* other) const; 1.206 + inline bool operator==(const char* other) const; 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 + 1.211 + inline operator const char*() const; 1.212 + 1.213 + char* lockBuffer(size_t size); 1.214 + void unlockBuffer(); 1.215 + status_t unlockBuffer(size_t size); 1.216 + 1.217 + // return the index of the first byte of other in this at or after 1.218 + // start, or -1 if not found 1.219 + ssize_t find(const char* other, size_t start = 0) const; 1.220 + 1.221 + void toLower(); 1.222 + void toLower(size_t start, size_t numChars); 1.223 + void toUpper(); 1.224 + void toUpper(size_t start, size_t numChars); 1.225 + 1.226 + /* 1.227 + * These methods operate on the string as if it were a path name. 1.228 + */ 1.229 + 1.230 + /* 1.231 + * Set the filename field to a specific value. 1.232 + * 1.233 + * Normalizes the filename, removing a trailing '/' if present. 1.234 + */ 1.235 + void setPathName(const char* name); 1.236 + void setPathName(const char* name, size_t numChars); 1.237 + 1.238 + /* 1.239 + * Get just the filename component. 1.240 + * 1.241 + * "/tmp/foo/bar.c" --> "bar.c" 1.242 + */ 1.243 + String8 getPathLeaf(void) const; 1.244 + 1.245 + /* 1.246 + * Remove the last (file name) component, leaving just the directory 1.247 + * name. 1.248 + * 1.249 + * "/tmp/foo/bar.c" --> "/tmp/foo" 1.250 + * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX 1.251 + * "bar.c" --> "" 1.252 + */ 1.253 + String8 getPathDir(void) const; 1.254 + 1.255 + /* 1.256 + * Retrieve the front (root dir) component. Optionally also return the 1.257 + * remaining components. 1.258 + * 1.259 + * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") 1.260 + * "/tmp" --> "tmp" (remain = "") 1.261 + * "bar.c" --> "bar.c" (remain = "") 1.262 + */ 1.263 + String8 walkPath(String8* outRemains = NULL) const; 1.264 + 1.265 + /* 1.266 + * Return the filename extension. This is the last '.' and up to 1.267 + * four characters that follow it. The '.' is included in case we 1.268 + * decide to expand our definition of what constitutes an extension. 1.269 + * 1.270 + * "/tmp/foo/bar.c" --> ".c" 1.271 + * "/tmp" --> "" 1.272 + * "/tmp/foo.bar/baz" --> "" 1.273 + * "foo.jpeg" --> ".jpeg" 1.274 + * "foo." --> "" 1.275 + */ 1.276 + String8 getPathExtension(void) const; 1.277 + 1.278 + /* 1.279 + * Return the path without the extension. Rules for what constitutes 1.280 + * an extension are described in the comment for getPathExtension(). 1.281 + * 1.282 + * "/tmp/foo/bar.c" --> "/tmp/foo/bar" 1.283 + */ 1.284 + String8 getBasePath(void) const; 1.285 + 1.286 + /* 1.287 + * Add a component to the pathname. We guarantee that there is 1.288 + * exactly one path separator between the old path and the new. 1.289 + * If there is no existing name, we just copy the new name in. 1.290 + * 1.291 + * If leaf is a fully qualified path (i.e. starts with '/', it 1.292 + * replaces whatever was there before. 1.293 + */ 1.294 + String8& appendPath(const char* leaf); 1.295 + String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } 1.296 + 1.297 + /* 1.298 + * Like appendPath(), but does not affect this string. Returns a new one instead. 1.299 + */ 1.300 + String8 appendPathCopy(const char* leaf) const 1.301 + { String8 p(*this); p.appendPath(leaf); return p; } 1.302 + String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } 1.303 + 1.304 + /* 1.305 + * Converts all separators in this string to /, the default path separator. 1.306 + * 1.307 + * If the default OS separator is backslash, this converts all 1.308 + * backslashes to slashes, in-place. Otherwise it does nothing. 1.309 + * Returns self. 1.310 + */ 1.311 + String8& convertToResPath(); 1.312 + 1.313 +private: 1.314 + status_t real_append(const char* other, size_t numChars); 1.315 + char* find_extension(void) const; 1.316 + 1.317 + const char* mString; 1.318 +}; 1.319 + 1.320 +TextOutput& operator<<(TextOutput& to, const String16& val); 1.321 + 1.322 +// --------------------------------------------------------------------------- 1.323 +// No user servicable parts below. 1.324 + 1.325 +inline int compare_type(const String8& lhs, const String8& rhs) 1.326 +{ 1.327 + return lhs.compare(rhs); 1.328 +} 1.329 + 1.330 +inline int strictly_order_type(const String8& lhs, const String8& rhs) 1.331 +{ 1.332 + return compare_type(lhs, rhs) < 0; 1.333 +} 1.334 + 1.335 +inline const char* String8::string() const 1.336 +{ 1.337 + return mString; 1.338 +} 1.339 + 1.340 +inline size_t String8::length() const 1.341 +{ 1.342 + return SharedBuffer::sizeFromData(mString)-1; 1.343 +} 1.344 + 1.345 +inline size_t String8::size() const 1.346 +{ 1.347 + return length(); 1.348 +} 1.349 + 1.350 +inline size_t String8::bytes() const 1.351 +{ 1.352 + return SharedBuffer::sizeFromData(mString)-1; 1.353 +} 1.354 + 1.355 +inline const SharedBuffer* String8::sharedBuffer() const 1.356 +{ 1.357 + return SharedBuffer::bufferFromData(mString); 1.358 +} 1.359 + 1.360 +inline String8& String8::operator=(const String8& other) 1.361 +{ 1.362 + setTo(other); 1.363 + return *this; 1.364 +} 1.365 + 1.366 +inline String8& String8::operator=(const char* other) 1.367 +{ 1.368 + setTo(other); 1.369 + return *this; 1.370 +} 1.371 + 1.372 +inline String8& String8::operator+=(const String8& other) 1.373 +{ 1.374 + append(other); 1.375 + return *this; 1.376 +} 1.377 + 1.378 +inline String8 String8::operator+(const String8& other) const 1.379 +{ 1.380 + String8 tmp; 1.381 + tmp += other; 1.382 + return tmp; 1.383 +} 1.384 + 1.385 +inline String8& String8::operator+=(const char* other) 1.386 +{ 1.387 + append(other); 1.388 + return *this; 1.389 +} 1.390 + 1.391 +inline String8 String8::operator+(const char* other) const 1.392 +{ 1.393 + String8 tmp; 1.394 + tmp += other; 1.395 + return tmp; 1.396 +} 1.397 + 1.398 +inline int String8::compare(const String8& other) const 1.399 +{ 1.400 + return strcmp(mString, other.mString); 1.401 +} 1.402 + 1.403 +inline bool String8::operator<(const String8& other) const 1.404 +{ 1.405 + return strcmp(mString, other.mString) < 0; 1.406 +} 1.407 + 1.408 +inline bool String8::operator<=(const String8& other) const 1.409 +{ 1.410 + return strcmp(mString, other.mString) <= 0; 1.411 +} 1.412 + 1.413 +inline bool String8::operator==(const String8& other) const 1.414 +{ 1.415 + return strcmp(mString, other.mString) == 0; 1.416 +} 1.417 + 1.418 +inline bool String8::operator!=(const String8& other) const 1.419 +{ 1.420 + return strcmp(mString, other.mString) != 0; 1.421 +} 1.422 + 1.423 +inline bool String8::operator>=(const String8& other) const 1.424 +{ 1.425 + return strcmp(mString, other.mString) >= 0; 1.426 +} 1.427 + 1.428 +inline bool String8::operator>(const String8& other) const 1.429 +{ 1.430 + return strcmp(mString, other.mString) > 0; 1.431 +} 1.432 + 1.433 +inline bool String8::operator<(const char* other) const 1.434 +{ 1.435 + return strcmp(mString, other) < 0; 1.436 +} 1.437 + 1.438 +inline bool String8::operator<=(const char* other) const 1.439 +{ 1.440 + return strcmp(mString, other) <= 0; 1.441 +} 1.442 + 1.443 +inline bool String8::operator==(const char* other) const 1.444 +{ 1.445 + return strcmp(mString, other) == 0; 1.446 +} 1.447 + 1.448 +inline bool String8::operator!=(const char* other) const 1.449 +{ 1.450 + return strcmp(mString, other) != 0; 1.451 +} 1.452 + 1.453 +inline bool String8::operator>=(const char* other) const 1.454 +{ 1.455 + return strcmp(mString, other) >= 0; 1.456 +} 1.457 + 1.458 +inline bool String8::operator>(const char* other) const 1.459 +{ 1.460 + return strcmp(mString, other) > 0; 1.461 +} 1.462 + 1.463 +inline String8::operator const char*() const 1.464 +{ 1.465 + return mString; 1.466 +} 1.467 + 1.468 +} // namespace android 1.469 + 1.470 +// --------------------------------------------------------------------------- 1.471 + 1.472 +#endif // ANDROID_STRING8_H