Thu, 22 Jan 2015 13:21:57 +0100
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_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 | #include <utils/Unicode.h> |
michael@0 | 23 | |
michael@0 | 24 | // --------------------------------------------------------------------------- |
michael@0 | 25 | |
michael@0 | 26 | extern "C" { |
michael@0 | 27 | |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | // --------------------------------------------------------------------------- |
michael@0 | 31 | |
michael@0 | 32 | namespace android { |
michael@0 | 33 | |
michael@0 | 34 | // --------------------------------------------------------------------------- |
michael@0 | 35 | |
michael@0 | 36 | class String8; |
michael@0 | 37 | class TextOutput; |
michael@0 | 38 | |
michael@0 | 39 | //! This is a string holding UTF-16 characters. |
michael@0 | 40 | class String16 |
michael@0 | 41 | { |
michael@0 | 42 | public: |
michael@0 | 43 | String16(); |
michael@0 | 44 | String16(const String16& o); |
michael@0 | 45 | String16(const String16& o, |
michael@0 | 46 | size_t len, |
michael@0 | 47 | size_t begin=0); |
michael@0 | 48 | explicit String16(const char16_t* o); |
michael@0 | 49 | explicit String16(const char16_t* o, size_t len); |
michael@0 | 50 | explicit String16(const String8& o); |
michael@0 | 51 | explicit String16(const char* o); |
michael@0 | 52 | explicit String16(const char* o, size_t len); |
michael@0 | 53 | |
michael@0 | 54 | ~String16(); |
michael@0 | 55 | |
michael@0 | 56 | inline const char16_t* string() const; |
michael@0 | 57 | inline size_t size() const; |
michael@0 | 58 | |
michael@0 | 59 | inline const SharedBuffer* sharedBuffer() const; |
michael@0 | 60 | |
michael@0 | 61 | void setTo(const String16& other); |
michael@0 | 62 | status_t setTo(const char16_t* other); |
michael@0 | 63 | status_t setTo(const char16_t* other, size_t len); |
michael@0 | 64 | status_t setTo(const String16& other, |
michael@0 | 65 | size_t len, |
michael@0 | 66 | size_t begin=0); |
michael@0 | 67 | |
michael@0 | 68 | status_t append(const String16& other); |
michael@0 | 69 | status_t append(const char16_t* other, size_t len); |
michael@0 | 70 | |
michael@0 | 71 | inline String16& operator=(const String16& other); |
michael@0 | 72 | |
michael@0 | 73 | inline String16& operator+=(const String16& other); |
michael@0 | 74 | inline String16 operator+(const String16& other) const; |
michael@0 | 75 | |
michael@0 | 76 | status_t insert(size_t pos, const char16_t* chrs); |
michael@0 | 77 | status_t insert(size_t pos, |
michael@0 | 78 | const char16_t* chrs, size_t len); |
michael@0 | 79 | |
michael@0 | 80 | ssize_t findFirst(char16_t c) const; |
michael@0 | 81 | ssize_t findLast(char16_t c) const; |
michael@0 | 82 | |
michael@0 | 83 | bool startsWith(const String16& prefix) const; |
michael@0 | 84 | bool startsWith(const char16_t* prefix) const; |
michael@0 | 85 | |
michael@0 | 86 | status_t makeLower(); |
michael@0 | 87 | |
michael@0 | 88 | status_t replaceAll(char16_t replaceThis, |
michael@0 | 89 | char16_t withThis); |
michael@0 | 90 | |
michael@0 | 91 | status_t remove(size_t len, size_t begin=0); |
michael@0 | 92 | |
michael@0 | 93 | inline int compare(const String16& other) const; |
michael@0 | 94 | |
michael@0 | 95 | inline bool operator<(const String16& other) const; |
michael@0 | 96 | inline bool operator<=(const String16& other) const; |
michael@0 | 97 | inline bool operator==(const String16& other) const; |
michael@0 | 98 | inline bool operator!=(const String16& other) const; |
michael@0 | 99 | inline bool operator>=(const String16& other) const; |
michael@0 | 100 | inline bool operator>(const String16& other) const; |
michael@0 | 101 | |
michael@0 | 102 | inline bool operator<(const char16_t* other) const; |
michael@0 | 103 | inline bool operator<=(const char16_t* other) const; |
michael@0 | 104 | inline bool operator==(const char16_t* other) const; |
michael@0 | 105 | inline bool operator!=(const char16_t* other) const; |
michael@0 | 106 | inline bool operator>=(const char16_t* other) const; |
michael@0 | 107 | inline bool operator>(const char16_t* other) const; |
michael@0 | 108 | |
michael@0 | 109 | inline operator const char16_t*() const; |
michael@0 | 110 | |
michael@0 | 111 | private: |
michael@0 | 112 | const char16_t* mString; |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | TextOutput& operator<<(TextOutput& to, const String16& val); |
michael@0 | 116 | |
michael@0 | 117 | // --------------------------------------------------------------------------- |
michael@0 | 118 | // No user servicable parts below. |
michael@0 | 119 | |
michael@0 | 120 | inline int compare_type(const String16& lhs, const String16& rhs) |
michael@0 | 121 | { |
michael@0 | 122 | return lhs.compare(rhs); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
michael@0 | 126 | { |
michael@0 | 127 | return compare_type(lhs, rhs) < 0; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | inline const char16_t* String16::string() const |
michael@0 | 131 | { |
michael@0 | 132 | return mString; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | inline size_t String16::size() const |
michael@0 | 136 | { |
michael@0 | 137 | return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | inline const SharedBuffer* String16::sharedBuffer() const |
michael@0 | 141 | { |
michael@0 | 142 | return SharedBuffer::bufferFromData(mString); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | inline String16& String16::operator=(const String16& other) |
michael@0 | 146 | { |
michael@0 | 147 | setTo(other); |
michael@0 | 148 | return *this; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | inline String16& String16::operator+=(const String16& other) |
michael@0 | 152 | { |
michael@0 | 153 | append(other); |
michael@0 | 154 | return *this; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | inline String16 String16::operator+(const String16& other) const |
michael@0 | 158 | { |
michael@0 | 159 | String16 tmp(*this); |
michael@0 | 160 | tmp += other; |
michael@0 | 161 | return tmp; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | inline int String16::compare(const String16& other) const |
michael@0 | 165 | { |
michael@0 | 166 | return strzcmp16(mString, size(), other.mString, other.size()); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | inline bool String16::operator<(const String16& other) const |
michael@0 | 170 | { |
michael@0 | 171 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | inline bool String16::operator<=(const String16& other) const |
michael@0 | 175 | { |
michael@0 | 176 | return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | inline bool String16::operator==(const String16& other) const |
michael@0 | 180 | { |
michael@0 | 181 | return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | inline bool String16::operator!=(const String16& other) const |
michael@0 | 185 | { |
michael@0 | 186 | return strzcmp16(mString, size(), other.mString, other.size()) != 0; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | inline bool String16::operator>=(const String16& other) const |
michael@0 | 190 | { |
michael@0 | 191 | return strzcmp16(mString, size(), other.mString, other.size()) >= 0; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | inline bool String16::operator>(const String16& other) const |
michael@0 | 195 | { |
michael@0 | 196 | return strzcmp16(mString, size(), other.mString, other.size()) > 0; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | inline bool String16::operator<(const char16_t* other) const |
michael@0 | 200 | { |
michael@0 | 201 | return strcmp16(mString, other) < 0; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | inline bool String16::operator<=(const char16_t* other) const |
michael@0 | 205 | { |
michael@0 | 206 | return strcmp16(mString, other) <= 0; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | inline bool String16::operator==(const char16_t* other) const |
michael@0 | 210 | { |
michael@0 | 211 | return strcmp16(mString, other) == 0; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | inline bool String16::operator!=(const char16_t* other) const |
michael@0 | 215 | { |
michael@0 | 216 | return strcmp16(mString, other) != 0; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | inline bool String16::operator>=(const char16_t* other) const |
michael@0 | 220 | { |
michael@0 | 221 | return strcmp16(mString, other) >= 0; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | inline bool String16::operator>(const char16_t* other) const |
michael@0 | 225 | { |
michael@0 | 226 | return strcmp16(mString, other) > 0; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | inline String16::operator const char16_t*() const |
michael@0 | 230 | { |
michael@0 | 231 | return mString; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | }; // namespace android |
michael@0 | 235 | |
michael@0 | 236 | // --------------------------------------------------------------------------- |
michael@0 | 237 | |
michael@0 | 238 | #endif // ANDROID_STRING16_H |