The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [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 | |
| 52 | } |
| 53 | |
| 54 | // --------------------------------------------------------------------------- |
| 55 | |
| 56 | namespace android { |
| 57 | |
| 58 | class String8; |
| 59 | class TextOutput; |
| 60 | |
| 61 | //! This is a string holding UTF-16 characters. |
| 62 | class String16 |
| 63 | { |
| 64 | public: |
| 65 | String16(); |
| 66 | String16(const String16& o); |
| 67 | String16(const String16& o, |
| 68 | size_t len, |
| 69 | size_t begin=0); |
| 70 | explicit String16(const char16_t* o); |
| 71 | explicit String16(const char16_t* o, size_t len); |
| 72 | explicit String16(const String8& o); |
| 73 | explicit String16(const char* o); |
| 74 | explicit String16(const char* o, size_t len); |
| 75 | |
| 76 | ~String16(); |
| 77 | |
| 78 | inline const char16_t* string() const; |
| 79 | inline size_t size() const; |
| 80 | |
| 81 | inline const SharedBuffer* sharedBuffer() const; |
| 82 | |
| 83 | void setTo(const String16& other); |
| 84 | status_t setTo(const char16_t* other); |
| 85 | status_t setTo(const char16_t* other, size_t len); |
| 86 | status_t setTo(const String16& other, |
| 87 | size_t len, |
| 88 | size_t begin=0); |
| 89 | |
| 90 | status_t append(const String16& other); |
| 91 | status_t append(const char16_t* other, size_t len); |
| 92 | |
| 93 | inline String16& operator=(const String16& other); |
| 94 | |
| 95 | inline String16& operator+=(const String16& other); |
| 96 | inline String16 operator+(const String16& other) const; |
| 97 | |
| 98 | status_t insert(size_t pos, const char16_t* chrs); |
| 99 | status_t insert(size_t pos, |
| 100 | const char16_t* chrs, size_t len); |
| 101 | |
| 102 | ssize_t findFirst(char16_t c) const; |
| 103 | ssize_t findLast(char16_t c) const; |
| 104 | |
| 105 | bool startsWith(const String16& prefix) const; |
| 106 | bool startsWith(const char16_t* prefix) const; |
| 107 | |
| 108 | status_t makeLower(); |
| 109 | |
| 110 | status_t replaceAll(char16_t replaceThis, |
| 111 | char16_t withThis); |
| 112 | |
| 113 | status_t remove(size_t len, size_t begin=0); |
| 114 | |
| 115 | inline int compare(const String16& other) const; |
| 116 | |
| 117 | inline bool operator<(const String16& other) const; |
| 118 | inline bool operator<=(const String16& other) const; |
| 119 | inline bool operator==(const String16& other) const; |
| 120 | inline bool operator!=(const String16& other) const; |
| 121 | inline bool operator>=(const String16& other) const; |
| 122 | inline bool operator>(const String16& other) const; |
| 123 | |
| 124 | inline bool operator<(const char16_t* other) const; |
| 125 | inline bool operator<=(const char16_t* other) const; |
| 126 | inline bool operator==(const char16_t* other) const; |
| 127 | inline bool operator!=(const char16_t* other) const; |
| 128 | inline bool operator>=(const char16_t* other) const; |
| 129 | inline bool operator>(const char16_t* other) const; |
| 130 | |
| 131 | inline operator const char16_t*() const; |
| 132 | |
| 133 | private: |
| 134 | const char16_t* mString; |
| 135 | }; |
| 136 | |
| 137 | TextOutput& operator<<(TextOutput& to, const String16& val); |
| 138 | |
| 139 | // --------------------------------------------------------------------------- |
| 140 | // No user servicable parts below. |
| 141 | |
| 142 | inline int compare_type(const String16& lhs, const String16& rhs) |
| 143 | { |
| 144 | return lhs.compare(rhs); |
| 145 | } |
| 146 | |
| 147 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
| 148 | { |
| 149 | return compare_type(lhs, rhs) < 0; |
| 150 | } |
| 151 | |
| 152 | inline const char16_t* String16::string() const |
| 153 | { |
| 154 | return mString; |
| 155 | } |
| 156 | |
| 157 | inline size_t String16::size() const |
| 158 | { |
| 159 | return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
| 160 | } |
| 161 | |
| 162 | inline const SharedBuffer* String16::sharedBuffer() const |
| 163 | { |
| 164 | return SharedBuffer::bufferFromData(mString); |
| 165 | } |
| 166 | |
| 167 | inline String16& String16::operator=(const String16& other) |
| 168 | { |
| 169 | setTo(other); |
| 170 | return *this; |
| 171 | } |
| 172 | |
| 173 | inline String16& String16::operator+=(const String16& other) |
| 174 | { |
| 175 | append(other); |
| 176 | return *this; |
| 177 | } |
| 178 | |
| 179 | inline String16 String16::operator+(const String16& other) const |
| 180 | { |
| 181 | String16 tmp; |
| 182 | tmp += other; |
| 183 | return tmp; |
| 184 | } |
| 185 | |
| 186 | inline int String16::compare(const String16& other) const |
| 187 | { |
| 188 | return strzcmp16(mString, size(), other.mString, other.size()); |
| 189 | } |
| 190 | |
| 191 | inline bool String16::operator<(const String16& other) const |
| 192 | { |
| 193 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
| 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 char16_t* other) const |
| 222 | { |
| 223 | return strcmp16(mString, other) < 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 String16::operator const char16_t*() const |
| 252 | { |
| 253 | return mString; |
| 254 | } |
| 255 | |
| 256 | }; // namespace android |
| 257 | |
| 258 | // --------------------------------------------------------------------------- |
| 259 | |
| 260 | #endif // ANDROID_STRING16_H |