Wed, 31 Dec 2014 06:09:35 +0100
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_STRING16_H |
michael@0 | 18 | #define ANDROID_STRING16_H |
michael@0 | 19 | |
michael@0 | 20 | #include <utils/Errors.h> |
michael@0 | 21 | #include <utils/SharedBuffer.h> |
michael@0 | 22 | |
michael@0 | 23 | #include <stdint.h> |
michael@0 | 24 | #include <sys/types.h> |
michael@0 | 25 | |
michael@0 | 26 | // --------------------------------------------------------------------------- |
michael@0 | 27 | |
michael@0 | 28 | extern "C" { |
michael@0 | 29 | |
michael@0 | 30 | #if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98 |
michael@0 | 31 | typedef uint16_t char16_t; |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | // Standard string functions on char16 strings. |
michael@0 | 35 | int strcmp16(const char16_t *, const char16_t *); |
michael@0 | 36 | int strncmp16(const char16_t *s1, const char16_t *s2, size_t n); |
michael@0 | 37 | size_t strlen16(const char16_t *); |
michael@0 | 38 | size_t strnlen16(const char16_t *, size_t); |
michael@0 | 39 | char16_t *strcpy16(char16_t *, const char16_t *); |
michael@0 | 40 | char16_t *strncpy16(char16_t *, const char16_t *, size_t); |
michael@0 | 41 | |
michael@0 | 42 | // Version of comparison that supports embedded nulls. |
michael@0 | 43 | // This is different than strncmp() because we don't stop |
michael@0 | 44 | // at a nul character and consider the strings to be different |
michael@0 | 45 | // if the lengths are different (thus we need to supply the |
michael@0 | 46 | // lengths of both strings). This can also be used when |
michael@0 | 47 | // your string is not nul-terminated as it will have the |
michael@0 | 48 | // equivalent result as strcmp16 (unlike strncmp16). |
michael@0 | 49 | int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2); |
michael@0 | 50 | |
michael@0 | 51 | // Version of strzcmp16 for comparing strings in different endianness. |
michael@0 | 52 | int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2); |
michael@0 | 53 | |
michael@0 | 54 | // Convert UTF-8 to UTF-16 including surrogate pairs |
michael@0 | 55 | void utf8_to_utf16(const uint8_t *src, size_t srcLen, char16_t* dst, const size_t dstLen); |
michael@0 | 56 | |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | // --------------------------------------------------------------------------- |
michael@0 | 60 | |
michael@0 | 61 | namespace android { |
michael@0 | 62 | |
michael@0 | 63 | // --------------------------------------------------------------------------- |
michael@0 | 64 | |
michael@0 | 65 | class String8; |
michael@0 | 66 | class TextOutput; |
michael@0 | 67 | |
michael@0 | 68 | //! This is a string holding UTF-16 characters. |
michael@0 | 69 | class String16 |
michael@0 | 70 | { |
michael@0 | 71 | public: |
michael@0 | 72 | String16(); |
michael@0 | 73 | String16(const String16& o); |
michael@0 | 74 | String16(const String16& o, |
michael@0 | 75 | size_t len, |
michael@0 | 76 | size_t begin=0); |
michael@0 | 77 | explicit String16(const char16_t* o); |
michael@0 | 78 | explicit String16(const char16_t* o, size_t len); |
michael@0 | 79 | explicit String16(const String8& o); |
michael@0 | 80 | explicit String16(const char* o); |
michael@0 | 81 | explicit String16(const char* o, size_t len); |
michael@0 | 82 | |
michael@0 | 83 | ~String16(); |
michael@0 | 84 | |
michael@0 | 85 | inline const char16_t* string() const; |
michael@0 | 86 | inline size_t size() const; |
michael@0 | 87 | |
michael@0 | 88 | inline const SharedBuffer* sharedBuffer() const; |
michael@0 | 89 | |
michael@0 | 90 | void setTo(const String16& other); |
michael@0 | 91 | status_t setTo(const char16_t* other); |
michael@0 | 92 | status_t setTo(const char16_t* other, size_t len); |
michael@0 | 93 | status_t setTo(const String16& other, |
michael@0 | 94 | size_t len, |
michael@0 | 95 | size_t begin=0); |
michael@0 | 96 | |
michael@0 | 97 | status_t append(const String16& other); |
michael@0 | 98 | status_t append(const char16_t* other, size_t len); |
michael@0 | 99 | |
michael@0 | 100 | inline String16& operator=(const String16& other); |
michael@0 | 101 | |
michael@0 | 102 | inline String16& operator+=(const String16& other); |
michael@0 | 103 | inline String16 operator+(const String16& other) const; |
michael@0 | 104 | |
michael@0 | 105 | status_t insert(size_t pos, const char16_t* chrs); |
michael@0 | 106 | status_t insert(size_t pos, |
michael@0 | 107 | const char16_t* chrs, size_t len); |
michael@0 | 108 | |
michael@0 | 109 | ssize_t findFirst(char16_t c) const; |
michael@0 | 110 | ssize_t findLast(char16_t c) const; |
michael@0 | 111 | |
michael@0 | 112 | bool startsWith(const String16& prefix) const; |
michael@0 | 113 | bool startsWith(const char16_t* prefix) const; |
michael@0 | 114 | |
michael@0 | 115 | status_t makeLower(); |
michael@0 | 116 | |
michael@0 | 117 | status_t replaceAll(char16_t replaceThis, |
michael@0 | 118 | char16_t withThis); |
michael@0 | 119 | |
michael@0 | 120 | status_t remove(size_t len, size_t begin=0); |
michael@0 | 121 | |
michael@0 | 122 | inline int compare(const String16& other) const; |
michael@0 | 123 | |
michael@0 | 124 | inline bool operator<(const String16& other) const; |
michael@0 | 125 | inline bool operator<=(const String16& other) const; |
michael@0 | 126 | inline bool operator==(const String16& other) const; |
michael@0 | 127 | inline bool operator!=(const String16& other) const; |
michael@0 | 128 | inline bool operator>=(const String16& other) const; |
michael@0 | 129 | inline bool operator>(const String16& other) const; |
michael@0 | 130 | |
michael@0 | 131 | inline bool operator<(const char16_t* other) const; |
michael@0 | 132 | inline bool operator<=(const char16_t* other) const; |
michael@0 | 133 | inline bool operator==(const char16_t* other) const; |
michael@0 | 134 | inline bool operator!=(const char16_t* other) const; |
michael@0 | 135 | inline bool operator>=(const char16_t* other) const; |
michael@0 | 136 | inline bool operator>(const char16_t* other) const; |
michael@0 | 137 | |
michael@0 | 138 | inline operator const char16_t*() const; |
michael@0 | 139 | |
michael@0 | 140 | private: |
michael@0 | 141 | const char16_t* mString; |
michael@0 | 142 | }; |
michael@0 | 143 | |
michael@0 | 144 | TextOutput& operator<<(TextOutput& to, const String16& val); |
michael@0 | 145 | |
michael@0 | 146 | // --------------------------------------------------------------------------- |
michael@0 | 147 | // No user servicable parts below. |
michael@0 | 148 | |
michael@0 | 149 | inline int compare_type(const String16& lhs, const String16& rhs) |
michael@0 | 150 | { |
michael@0 | 151 | return lhs.compare(rhs); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
michael@0 | 155 | { |
michael@0 | 156 | return compare_type(lhs, rhs) < 0; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | inline const char16_t* String16::string() const |
michael@0 | 160 | { |
michael@0 | 161 | return mString; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | inline size_t String16::size() const |
michael@0 | 165 | { |
michael@0 | 166 | return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | inline const SharedBuffer* String16::sharedBuffer() const |
michael@0 | 170 | { |
michael@0 | 171 | return SharedBuffer::bufferFromData(mString); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | inline String16& String16::operator=(const String16& other) |
michael@0 | 175 | { |
michael@0 | 176 | setTo(other); |
michael@0 | 177 | return *this; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | inline String16& String16::operator+=(const String16& other) |
michael@0 | 181 | { |
michael@0 | 182 | append(other); |
michael@0 | 183 | return *this; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | inline String16 String16::operator+(const String16& other) const |
michael@0 | 187 | { |
michael@0 | 188 | String16 tmp; |
michael@0 | 189 | tmp += other; |
michael@0 | 190 | return tmp; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | inline int String16::compare(const String16& other) const |
michael@0 | 194 | { |
michael@0 | 195 | return strzcmp16(mString, size(), other.mString, other.size()); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | inline bool String16::operator<(const String16& other) const |
michael@0 | 199 | { |
michael@0 | 200 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | inline bool String16::operator<=(const String16& other) const |
michael@0 | 204 | { |
michael@0 | 205 | return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | inline bool String16::operator==(const String16& other) const |
michael@0 | 209 | { |
michael@0 | 210 | return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | inline bool String16::operator!=(const String16& other) const |
michael@0 | 214 | { |
michael@0 | 215 | return strzcmp16(mString, size(), other.mString, other.size()) != 0; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | inline bool String16::operator>=(const String16& other) const |
michael@0 | 219 | { |
michael@0 | 220 | return strzcmp16(mString, size(), other.mString, other.size()) >= 0; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | inline bool String16::operator>(const String16& other) const |
michael@0 | 224 | { |
michael@0 | 225 | return strzcmp16(mString, size(), other.mString, other.size()) > 0; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | inline bool String16::operator<(const char16_t* other) const |
michael@0 | 229 | { |
michael@0 | 230 | return strcmp16(mString, other) < 0; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | inline bool String16::operator<=(const char16_t* other) const |
michael@0 | 234 | { |
michael@0 | 235 | return strcmp16(mString, other) <= 0; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | inline bool String16::operator==(const char16_t* other) const |
michael@0 | 239 | { |
michael@0 | 240 | return strcmp16(mString, other) == 0; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | inline bool String16::operator!=(const char16_t* other) const |
michael@0 | 244 | { |
michael@0 | 245 | return strcmp16(mString, other) != 0; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | inline bool String16::operator>=(const char16_t* other) const |
michael@0 | 249 | { |
michael@0 | 250 | return strcmp16(mString, other) >= 0; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | inline bool String16::operator>(const char16_t* other) const |
michael@0 | 254 | { |
michael@0 | 255 | return strcmp16(mString, other) > 0; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | inline String16::operator const char16_t*() const |
michael@0 | 259 | { |
michael@0 | 260 | return mString; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | }; // namespace android |
michael@0 | 264 | |
michael@0 | 265 | // --------------------------------------------------------------------------- |
michael@0 | 266 | |
michael@0 | 267 | #endif // ANDROID_STRING16_H |