The Android Open Source Project | cbb1011 | 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 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 20 | #include <string> // for std::string |
| 21 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 23 | #include <utils/String8.h> |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 24 | #include <utils/TypeHelpers.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | // --------------------------------------------------------------------------- |
| 27 | |
| 28 | extern "C" { |
| 29 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 36 | // --------------------------------------------------------------------------- |
| 37 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 38 | class SharedBuffer; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | class String8; |
| 40 | class TextOutput; |
| 41 | |
| 42 | //! This is a string holding UTF-16 characters. |
| 43 | class String16 |
| 44 | { |
| 45 | public: |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 46 | /* use String16(StaticLinkage) if you're statically linking against |
| 47 | * libutils and declaring an empty static String16, e.g.: |
| 48 | * |
| 49 | * static String16 sAStaticEmptyString(String16::kEmptyString); |
| 50 | * static String16 sAnotherStaticEmptyString(sAStaticEmptyString); |
| 51 | */ |
| 52 | enum StaticLinkage { kEmptyString }; |
| 53 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | String16(); |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 55 | explicit String16(StaticLinkage); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | String16(const String16& o); |
| 57 | String16(const String16& o, |
| 58 | size_t len, |
| 59 | size_t begin=0); |
| 60 | explicit String16(const char16_t* o); |
| 61 | explicit String16(const char16_t* o, size_t len); |
| 62 | explicit String16(const String8& o); |
| 63 | explicit String16(const char* o); |
| 64 | explicit String16(const char* o, size_t len); |
| 65 | |
| 66 | ~String16(); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 67 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | inline const char16_t* string() const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 69 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 70 | static inline std::string std_string(const String16& str); |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 71 | size_t size() const; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | void setTo(const String16& other); |
| 73 | status_t setTo(const char16_t* other); |
| 74 | status_t setTo(const char16_t* other, size_t len); |
| 75 | status_t setTo(const String16& other, |
| 76 | size_t len, |
| 77 | size_t begin=0); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | status_t append(const String16& other); |
| 80 | status_t append(const char16_t* other, size_t len); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 81 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | inline String16& operator=(const String16& other); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 83 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | inline String16& operator+=(const String16& other); |
| 85 | inline String16 operator+(const String16& other) const; |
| 86 | |
| 87 | status_t insert(size_t pos, const char16_t* chrs); |
| 88 | status_t insert(size_t pos, |
| 89 | const char16_t* chrs, size_t len); |
| 90 | |
| 91 | ssize_t findFirst(char16_t c) const; |
| 92 | ssize_t findLast(char16_t c) const; |
| 93 | |
| 94 | bool startsWith(const String16& prefix) const; |
| 95 | bool startsWith(const char16_t* prefix) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | status_t makeLower(); |
| 98 | |
| 99 | status_t replaceAll(char16_t replaceThis, |
| 100 | char16_t withThis); |
| 101 | |
| 102 | status_t remove(size_t len, size_t begin=0); |
| 103 | |
| 104 | inline int compare(const String16& other) const; |
| 105 | |
| 106 | inline bool operator<(const String16& other) const; |
| 107 | inline bool operator<=(const String16& other) const; |
| 108 | inline bool operator==(const String16& other) const; |
| 109 | inline bool operator!=(const String16& other) const; |
| 110 | inline bool operator>=(const String16& other) const; |
| 111 | inline bool operator>(const String16& other) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 112 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | inline bool operator<(const char16_t* other) const; |
| 114 | inline bool operator<=(const char16_t* other) const; |
| 115 | inline bool operator==(const char16_t* other) const; |
| 116 | inline bool operator!=(const char16_t* other) const; |
| 117 | inline bool operator>=(const char16_t* other) const; |
| 118 | inline bool operator>(const char16_t* other) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 119 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | inline operator const char16_t*() const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 121 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | private: |
| 123 | const char16_t* mString; |
| 124 | }; |
| 125 | |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 126 | // String16 can be trivially moved using memcpy() because moving does not |
| 127 | // require any change to the underlying SharedBuffer contents or reference count. |
| 128 | ANDROID_TRIVIAL_MOVE_TRAIT(String16) |
| 129 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | // --------------------------------------------------------------------------- |
| 131 | // No user servicable parts below. |
| 132 | |
| 133 | inline int compare_type(const String16& lhs, const String16& rhs) |
| 134 | { |
| 135 | return lhs.compare(rhs); |
| 136 | } |
| 137 | |
| 138 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
| 139 | { |
| 140 | return compare_type(lhs, rhs) < 0; |
| 141 | } |
| 142 | |
| 143 | inline const char16_t* String16::string() const |
| 144 | { |
| 145 | return mString; |
| 146 | } |
| 147 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame^] | 148 | inline std::string String16::std_string(const String16& str) |
| 149 | { |
| 150 | return std::string(String8(str).string()); |
| 151 | } |
| 152 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | inline String16& String16::operator=(const String16& other) |
| 154 | { |
| 155 | setTo(other); |
| 156 | return *this; |
| 157 | } |
| 158 | |
| 159 | inline String16& String16::operator+=(const String16& other) |
| 160 | { |
| 161 | append(other); |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | inline String16 String16::operator+(const String16& other) const |
| 166 | { |
Josiah Gaskin | 9ee3fc4 | 2011-08-16 15:16:04 -0700 | [diff] [blame] | 167 | String16 tmp(*this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | tmp += other; |
| 169 | return tmp; |
| 170 | } |
| 171 | |
| 172 | inline int String16::compare(const String16& other) const |
| 173 | { |
| 174 | return strzcmp16(mString, size(), other.mString, other.size()); |
| 175 | } |
| 176 | |
| 177 | inline bool String16::operator<(const String16& other) const |
| 178 | { |
| 179 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
| 180 | } |
| 181 | |
| 182 | inline bool String16::operator<=(const String16& other) const |
| 183 | { |
| 184 | return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
| 185 | } |
| 186 | |
| 187 | inline bool String16::operator==(const String16& other) const |
| 188 | { |
Brad Fitzpatrick | 9d589aa | 2010-10-20 17:06:28 -0700 | [diff] [blame] | 189 | return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | inline bool String16::operator!=(const String16& other) const |
| 193 | { |
| 194 | return strzcmp16(mString, size(), other.mString, other.size()) != 0; |
| 195 | } |
| 196 | |
| 197 | inline bool String16::operator>=(const String16& other) const |
| 198 | { |
| 199 | return strzcmp16(mString, size(), other.mString, other.size()) >= 0; |
| 200 | } |
| 201 | |
| 202 | inline bool String16::operator>(const String16& other) const |
| 203 | { |
| 204 | return strzcmp16(mString, size(), other.mString, other.size()) > 0; |
| 205 | } |
| 206 | |
| 207 | inline bool String16::operator<(const char16_t* other) const |
| 208 | { |
| 209 | return strcmp16(mString, other) < 0; |
| 210 | } |
| 211 | |
| 212 | inline bool String16::operator<=(const char16_t* other) const |
| 213 | { |
| 214 | return strcmp16(mString, other) <= 0; |
| 215 | } |
| 216 | |
| 217 | inline bool String16::operator==(const char16_t* other) const |
| 218 | { |
| 219 | return strcmp16(mString, other) == 0; |
| 220 | } |
| 221 | |
| 222 | inline bool String16::operator!=(const char16_t* other) const |
| 223 | { |
| 224 | return strcmp16(mString, other) != 0; |
| 225 | } |
| 226 | |
| 227 | inline bool String16::operator>=(const char16_t* other) const |
| 228 | { |
| 229 | return strcmp16(mString, other) >= 0; |
| 230 | } |
| 231 | |
| 232 | inline bool String16::operator>(const char16_t* other) const |
| 233 | { |
| 234 | return strcmp16(mString, other) > 0; |
| 235 | } |
| 236 | |
| 237 | inline String16::operator const char16_t*() const |
| 238 | { |
| 239 | return mString; |
| 240 | } |
| 241 | |
| 242 | }; // namespace android |
| 243 | |
| 244 | // --------------------------------------------------------------------------- |
| 245 | |
| 246 | #endif // ANDROID_STRING16_H |