1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/ics/utils/String8.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,383 @@ 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 +#include <utils/SharedBuffer.h> 1.25 +#include <utils/Unicode.h> 1.26 + 1.27 +#include <string.h> // for strcmp 1.28 +#include <stdarg.h> 1.29 + 1.30 +// --------------------------------------------------------------------------- 1.31 + 1.32 +namespace android { 1.33 + 1.34 +class String16; 1.35 +class TextOutput; 1.36 + 1.37 +//! This is a string holding UTF-8 characters. Does not allow the value more 1.38 +// than 0x10FFFF, which is not valid unicode codepoint. 1.39 +class String8 1.40 +{ 1.41 +public: 1.42 + String8(); 1.43 + String8(const String8& o); 1.44 + explicit String8(const char* o); 1.45 + explicit String8(const char* o, size_t numChars); 1.46 + 1.47 + explicit String8(const String16& o); 1.48 + explicit String8(const char16_t* o); 1.49 + explicit String8(const char16_t* o, size_t numChars); 1.50 + explicit String8(const char32_t* o); 1.51 + explicit String8(const char32_t* o, size_t numChars); 1.52 + ~String8(); 1.53 + 1.54 + static inline const String8 empty(); 1.55 + 1.56 + static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2))); 1.57 + static String8 formatV(const char* fmt, va_list args); 1.58 + 1.59 + inline const char* string() const; 1.60 + inline size_t size() const; 1.61 + inline size_t length() const; 1.62 + inline size_t bytes() const; 1.63 + inline bool isEmpty() const; 1.64 + 1.65 + inline const SharedBuffer* sharedBuffer() const; 1.66 + 1.67 + void clear(); 1.68 + 1.69 + void setTo(const String8& other); 1.70 + status_t setTo(const char* other); 1.71 + status_t setTo(const char* other, size_t numChars); 1.72 + status_t setTo(const char16_t* other, size_t numChars); 1.73 + status_t setTo(const char32_t* other, 1.74 + size_t length); 1.75 + 1.76 + status_t append(const String8& other); 1.77 + status_t append(const char* other); 1.78 + status_t append(const char* other, size_t numChars); 1.79 + 1.80 + status_t appendFormat(const char* fmt, ...) 1.81 + __attribute__((format (printf, 2, 3))); 1.82 + status_t appendFormatV(const char* fmt, va_list args); 1.83 + 1.84 + // Note that this function takes O(N) time to calculate the value. 1.85 + // No cache value is stored. 1.86 + size_t getUtf32Length() const; 1.87 + int32_t getUtf32At(size_t index, 1.88 + size_t *next_index) const; 1.89 + void getUtf32(char32_t* dst) const; 1.90 + 1.91 + inline String8& operator=(const String8& other); 1.92 + inline String8& operator=(const char* other); 1.93 + 1.94 + inline String8& operator+=(const String8& other); 1.95 + inline String8 operator+(const String8& other) const; 1.96 + 1.97 + inline String8& operator+=(const char* other); 1.98 + inline String8 operator+(const char* other) const; 1.99 + 1.100 + inline int compare(const String8& other) const; 1.101 + 1.102 + inline bool operator<(const String8& other) const; 1.103 + inline bool operator<=(const String8& other) const; 1.104 + inline bool operator==(const String8& other) const; 1.105 + inline bool operator!=(const String8& other) const; 1.106 + inline bool operator>=(const String8& other) const; 1.107 + inline bool operator>(const String8& other) const; 1.108 + 1.109 + inline bool operator<(const char* other) const; 1.110 + inline bool operator<=(const char* other) const; 1.111 + inline bool operator==(const char* other) const; 1.112 + inline bool operator!=(const char* other) const; 1.113 + inline bool operator>=(const char* other) const; 1.114 + inline bool operator>(const char* other) const; 1.115 + 1.116 + inline operator const char*() const; 1.117 + 1.118 + char* lockBuffer(size_t size); 1.119 + void unlockBuffer(); 1.120 + status_t unlockBuffer(size_t size); 1.121 + 1.122 + // return the index of the first byte of other in this at or after 1.123 + // start, or -1 if not found 1.124 + ssize_t find(const char* other, size_t start = 0) const; 1.125 + 1.126 + void toLower(); 1.127 + void toLower(size_t start, size_t numChars); 1.128 + void toUpper(); 1.129 + void toUpper(size_t start, size_t numChars); 1.130 + 1.131 + /* 1.132 + * These methods operate on the string as if it were a path name. 1.133 + */ 1.134 + 1.135 + /* 1.136 + * Set the filename field to a specific value. 1.137 + * 1.138 + * Normalizes the filename, removing a trailing '/' if present. 1.139 + */ 1.140 + void setPathName(const char* name); 1.141 + void setPathName(const char* name, size_t numChars); 1.142 + 1.143 + /* 1.144 + * Get just the filename component. 1.145 + * 1.146 + * "/tmp/foo/bar.c" --> "bar.c" 1.147 + */ 1.148 + String8 getPathLeaf(void) const; 1.149 + 1.150 + /* 1.151 + * Remove the last (file name) component, leaving just the directory 1.152 + * name. 1.153 + * 1.154 + * "/tmp/foo/bar.c" --> "/tmp/foo" 1.155 + * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX 1.156 + * "bar.c" --> "" 1.157 + */ 1.158 + String8 getPathDir(void) const; 1.159 + 1.160 + /* 1.161 + * Retrieve the front (root dir) component. Optionally also return the 1.162 + * remaining components. 1.163 + * 1.164 + * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c") 1.165 + * "/tmp" --> "tmp" (remain = "") 1.166 + * "bar.c" --> "bar.c" (remain = "") 1.167 + */ 1.168 + String8 walkPath(String8* outRemains = NULL) const; 1.169 + 1.170 + /* 1.171 + * Return the filename extension. This is the last '.' and any number 1.172 + * of characters that follow it. The '.' is included in case we 1.173 + * decide to expand our definition of what constitutes an extension. 1.174 + * 1.175 + * "/tmp/foo/bar.c" --> ".c" 1.176 + * "/tmp" --> "" 1.177 + * "/tmp/foo.bar/baz" --> "" 1.178 + * "foo.jpeg" --> ".jpeg" 1.179 + * "foo." --> "" 1.180 + */ 1.181 + String8 getPathExtension(void) const; 1.182 + 1.183 + /* 1.184 + * Return the path without the extension. Rules for what constitutes 1.185 + * an extension are described in the comment for getPathExtension(). 1.186 + * 1.187 + * "/tmp/foo/bar.c" --> "/tmp/foo/bar" 1.188 + */ 1.189 + String8 getBasePath(void) const; 1.190 + 1.191 + /* 1.192 + * Add a component to the pathname. We guarantee that there is 1.193 + * exactly one path separator between the old path and the new. 1.194 + * If there is no existing name, we just copy the new name in. 1.195 + * 1.196 + * If leaf is a fully qualified path (i.e. starts with '/', it 1.197 + * replaces whatever was there before. 1.198 + */ 1.199 + String8& appendPath(const char* leaf); 1.200 + String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } 1.201 + 1.202 + /* 1.203 + * Like appendPath(), but does not affect this string. Returns a new one instead. 1.204 + */ 1.205 + String8 appendPathCopy(const char* leaf) const 1.206 + { String8 p(*this); p.appendPath(leaf); return p; } 1.207 + String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } 1.208 + 1.209 + /* 1.210 + * Converts all separators in this string to /, the default path separator. 1.211 + * 1.212 + * If the default OS separator is backslash, this converts all 1.213 + * backslashes to slashes, in-place. Otherwise it does nothing. 1.214 + * Returns self. 1.215 + */ 1.216 + String8& convertToResPath(); 1.217 + 1.218 +private: 1.219 + status_t real_append(const char* other, size_t numChars); 1.220 + char* find_extension(void) const; 1.221 + 1.222 + const char* mString; 1.223 +}; 1.224 + 1.225 +TextOutput& operator<<(TextOutput& to, const String16& val); 1.226 + 1.227 +// --------------------------------------------------------------------------- 1.228 +// No user servicable parts below. 1.229 + 1.230 +inline int compare_type(const String8& lhs, const String8& rhs) 1.231 +{ 1.232 + return lhs.compare(rhs); 1.233 +} 1.234 + 1.235 +inline int strictly_order_type(const String8& lhs, const String8& rhs) 1.236 +{ 1.237 + return compare_type(lhs, rhs) < 0; 1.238 +} 1.239 + 1.240 +inline const String8 String8::empty() { 1.241 + return String8(); 1.242 +} 1.243 + 1.244 +inline const char* String8::string() const 1.245 +{ 1.246 + return mString; 1.247 +} 1.248 + 1.249 +inline size_t String8::length() const 1.250 +{ 1.251 + return SharedBuffer::sizeFromData(mString)-1; 1.252 +} 1.253 + 1.254 +inline size_t String8::size() const 1.255 +{ 1.256 + return length(); 1.257 +} 1.258 + 1.259 +inline bool String8::isEmpty() const 1.260 +{ 1.261 + return length() == 0; 1.262 +} 1.263 + 1.264 +inline size_t String8::bytes() const 1.265 +{ 1.266 + return SharedBuffer::sizeFromData(mString)-1; 1.267 +} 1.268 + 1.269 +inline const SharedBuffer* String8::sharedBuffer() const 1.270 +{ 1.271 + return SharedBuffer::bufferFromData(mString); 1.272 +} 1.273 + 1.274 +inline String8& String8::operator=(const String8& other) 1.275 +{ 1.276 + setTo(other); 1.277 + return *this; 1.278 +} 1.279 + 1.280 +inline String8& String8::operator=(const char* other) 1.281 +{ 1.282 + setTo(other); 1.283 + return *this; 1.284 +} 1.285 + 1.286 +inline String8& String8::operator+=(const String8& other) 1.287 +{ 1.288 + append(other); 1.289 + return *this; 1.290 +} 1.291 + 1.292 +inline String8 String8::operator+(const String8& other) const 1.293 +{ 1.294 + String8 tmp(*this); 1.295 + tmp += other; 1.296 + return tmp; 1.297 +} 1.298 + 1.299 +inline String8& String8::operator+=(const char* other) 1.300 +{ 1.301 + append(other); 1.302 + return *this; 1.303 +} 1.304 + 1.305 +inline String8 String8::operator+(const char* other) const 1.306 +{ 1.307 + String8 tmp(*this); 1.308 + tmp += other; 1.309 + return tmp; 1.310 +} 1.311 + 1.312 +inline int String8::compare(const String8& other) const 1.313 +{ 1.314 + return strcmp(mString, other.mString); 1.315 +} 1.316 + 1.317 +inline bool String8::operator<(const String8& other) const 1.318 +{ 1.319 + return strcmp(mString, other.mString) < 0; 1.320 +} 1.321 + 1.322 +inline bool String8::operator<=(const String8& other) const 1.323 +{ 1.324 + return strcmp(mString, other.mString) <= 0; 1.325 +} 1.326 + 1.327 +inline bool String8::operator==(const String8& other) const 1.328 +{ 1.329 + return strcmp(mString, other.mString) == 0; 1.330 +} 1.331 + 1.332 +inline bool String8::operator!=(const String8& other) const 1.333 +{ 1.334 + return strcmp(mString, other.mString) != 0; 1.335 +} 1.336 + 1.337 +inline bool String8::operator>=(const String8& other) const 1.338 +{ 1.339 + return strcmp(mString, other.mString) >= 0; 1.340 +} 1.341 + 1.342 +inline bool String8::operator>(const String8& other) const 1.343 +{ 1.344 + return strcmp(mString, other.mString) > 0; 1.345 +} 1.346 + 1.347 +inline bool String8::operator<(const char* other) const 1.348 +{ 1.349 + return strcmp(mString, other) < 0; 1.350 +} 1.351 + 1.352 +inline bool String8::operator<=(const char* other) const 1.353 +{ 1.354 + return strcmp(mString, other) <= 0; 1.355 +} 1.356 + 1.357 +inline bool String8::operator==(const char* other) const 1.358 +{ 1.359 + return strcmp(mString, other) == 0; 1.360 +} 1.361 + 1.362 +inline bool String8::operator!=(const char* other) const 1.363 +{ 1.364 + return strcmp(mString, other) != 0; 1.365 +} 1.366 + 1.367 +inline bool String8::operator>=(const char* other) const 1.368 +{ 1.369 + return strcmp(mString, other) >= 0; 1.370 +} 1.371 + 1.372 +inline bool String8::operator>(const char* other) const 1.373 +{ 1.374 + return strcmp(mString, other) > 0; 1.375 +} 1.376 + 1.377 +inline String8::operator const char*() const 1.378 +{ 1.379 + return mString; 1.380 +} 1.381 + 1.382 +} // namespace android 1.383 + 1.384 +// --------------------------------------------------------------------------- 1.385 + 1.386 +#endif // ANDROID_STRING8_H