The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_STRING16_H |
| 18 | #define ANDROID_STRING16_H |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | #include <utils/SharedBuffer.h> |
Kenny Root | 300ba68 | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 22 | #include <utils/Unicode.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | // --------------------------------------------------------------------------- |
| 25 | |
| 26 | extern "C" { |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | namespace android { |
| 33 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 34 | // --------------------------------------------------------------------------- |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | class String8; |
| 37 | class TextOutput; |
| 38 | |
| 39 | //! This is a string holding UTF-16 characters. |
| 40 | class String16 |
| 41 | { |
| 42 | public: |
| 43 | String16(); |
| 44 | String16(const String16& o); |
| 45 | String16(const String16& o, |
| 46 | size_t len, |
| 47 | size_t begin=0); |
| 48 | explicit String16(const char16_t* o); |
| 49 | explicit String16(const char16_t* o, size_t len); |
| 50 | explicit String16(const String8& o); |
| 51 | explicit String16(const char* o); |
| 52 | explicit String16(const char* o, size_t len); |
| 53 | |
| 54 | ~String16(); |
| 55 | |
| 56 | inline const char16_t* string() const; |
| 57 | inline size_t size() const; |
| 58 | |
| 59 | inline const SharedBuffer* sharedBuffer() const; |
| 60 | |
| 61 | void setTo(const String16& other); |
| 62 | status_t setTo(const char16_t* other); |
| 63 | status_t setTo(const char16_t* other, size_t len); |
| 64 | status_t setTo(const String16& other, |
| 65 | size_t len, |
| 66 | size_t begin=0); |
| 67 | |
| 68 | status_t append(const String16& other); |
| 69 | status_t append(const char16_t* other, size_t len); |
| 70 | |
| 71 | inline String16& operator=(const String16& other); |
| 72 | |
| 73 | inline String16& operator+=(const String16& other); |
| 74 | inline String16 operator+(const String16& other) const; |
| 75 | |
| 76 | status_t insert(size_t pos, const char16_t* chrs); |
| 77 | status_t insert(size_t pos, |
| 78 | const char16_t* chrs, size_t len); |
| 79 | |
| 80 | ssize_t findFirst(char16_t c) const; |
| 81 | ssize_t findLast(char16_t c) const; |
| 82 | |
| 83 | bool startsWith(const String16& prefix) const; |
| 84 | bool startsWith(const char16_t* prefix) const; |
| 85 | |
| 86 | status_t makeLower(); |
| 87 | |
| 88 | status_t replaceAll(char16_t replaceThis, |
| 89 | char16_t withThis); |
| 90 | |
| 91 | status_t remove(size_t len, size_t begin=0); |
| 92 | |
| 93 | inline int compare(const String16& other) const; |
| 94 | |
| 95 | inline bool operator<(const String16& other) const; |
| 96 | inline bool operator<=(const String16& other) const; |
| 97 | inline bool operator==(const String16& other) const; |
| 98 | inline bool operator!=(const String16& other) const; |
| 99 | inline bool operator>=(const String16& other) const; |
| 100 | inline bool operator>(const String16& other) const; |
| 101 | |
| 102 | inline bool operator<(const char16_t* other) const; |
| 103 | inline bool operator<=(const char16_t* other) const; |
| 104 | inline bool operator==(const char16_t* other) const; |
| 105 | inline bool operator!=(const char16_t* other) const; |
| 106 | inline bool operator>=(const char16_t* other) const; |
| 107 | inline bool operator>(const char16_t* other) const; |
| 108 | |
| 109 | inline operator const char16_t*() const; |
| 110 | |
| 111 | private: |
| 112 | const char16_t* mString; |
| 113 | }; |
| 114 | |
| 115 | TextOutput& operator<<(TextOutput& to, const String16& val); |
| 116 | |
| 117 | // --------------------------------------------------------------------------- |
| 118 | // No user servicable parts below. |
| 119 | |
| 120 | inline int compare_type(const String16& lhs, const String16& rhs) |
| 121 | { |
| 122 | return lhs.compare(rhs); |
| 123 | } |
| 124 | |
| 125 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
| 126 | { |
| 127 | return compare_type(lhs, rhs) < 0; |
| 128 | } |
| 129 | |
| 130 | inline const char16_t* String16::string() const |
| 131 | { |
| 132 | return mString; |
| 133 | } |
| 134 | |
| 135 | inline size_t String16::size() const |
| 136 | { |
| 137 | return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
| 138 | } |
| 139 | |
| 140 | inline const SharedBuffer* String16::sharedBuffer() const |
| 141 | { |
| 142 | return SharedBuffer::bufferFromData(mString); |
| 143 | } |
| 144 | |
| 145 | inline String16& String16::operator=(const String16& other) |
| 146 | { |
| 147 | setTo(other); |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | inline String16& String16::operator+=(const String16& other) |
| 152 | { |
| 153 | append(other); |
| 154 | return *this; |
| 155 | } |
| 156 | |
| 157 | inline String16 String16::operator+(const String16& other) const |
| 158 | { |
| 159 | String16 tmp; |
| 160 | tmp += other; |
| 161 | return tmp; |
| 162 | } |
| 163 | |
| 164 | inline int String16::compare(const String16& other) const |
| 165 | { |
| 166 | return strzcmp16(mString, size(), other.mString, other.size()); |
| 167 | } |
| 168 | |
| 169 | inline bool String16::operator<(const String16& other) const |
| 170 | { |
| 171 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
| 172 | } |
| 173 | |
| 174 | inline bool String16::operator<=(const String16& other) const |
| 175 | { |
| 176 | return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
| 177 | } |
| 178 | |
| 179 | inline bool String16::operator==(const String16& other) const |
| 180 | { |
Brad Fitzpatrick | cf89188 | 2010-10-20 17:06:28 -0700 | [diff] [blame] | 181 | return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | inline bool String16::operator!=(const String16& other) const |
| 185 | { |
| 186 | return strzcmp16(mString, size(), other.mString, other.size()) != 0; |
| 187 | } |
| 188 | |
| 189 | inline bool String16::operator>=(const String16& other) const |
| 190 | { |
| 191 | return strzcmp16(mString, size(), other.mString, other.size()) >= 0; |
| 192 | } |
| 193 | |
| 194 | inline bool String16::operator>(const String16& other) const |
| 195 | { |
| 196 | return strzcmp16(mString, size(), other.mString, other.size()) > 0; |
| 197 | } |
| 198 | |
| 199 | inline bool String16::operator<(const char16_t* other) const |
| 200 | { |
| 201 | return strcmp16(mString, other) < 0; |
| 202 | } |
| 203 | |
| 204 | inline bool String16::operator<=(const char16_t* other) const |
| 205 | { |
| 206 | return strcmp16(mString, other) <= 0; |
| 207 | } |
| 208 | |
| 209 | inline bool String16::operator==(const char16_t* other) const |
| 210 | { |
| 211 | return strcmp16(mString, other) == 0; |
| 212 | } |
| 213 | |
| 214 | inline bool String16::operator!=(const char16_t* other) const |
| 215 | { |
| 216 | return strcmp16(mString, other) != 0; |
| 217 | } |
| 218 | |
| 219 | inline bool String16::operator>=(const char16_t* other) const |
| 220 | { |
| 221 | return strcmp16(mString, other) >= 0; |
| 222 | } |
| 223 | |
| 224 | inline bool String16::operator>(const char16_t* other) const |
| 225 | { |
| 226 | return strcmp16(mString, other) > 0; |
| 227 | } |
| 228 | |
| 229 | inline String16::operator const char16_t*() const |
| 230 | { |
| 231 | return mString; |
| 232 | } |
| 233 | |
| 234 | }; // namespace android |
| 235 | |
| 236 | // --------------------------------------------------------------------------- |
| 237 | |
| 238 | #endif // ANDROID_STRING16_H |