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