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 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | class String8; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Steven Moreland | b8f152d | 2017-12-18 16:14:13 -0800 | [diff] [blame] | 40 | // DO NOT USE: please use std::u16string |
| 41 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Steven Moreland | 2aac335 | 2017-03-10 22:31:08 -0800 | [diff] [blame] | 70 | private: |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame] | 71 | static inline std::string std_string(const String16& str); |
Steven Moreland | 2aac335 | 2017-03-10 22:31:08 -0800 | [diff] [blame] | 72 | public: |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 73 | size_t size() const; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | void setTo(const String16& other); |
| 75 | status_t setTo(const char16_t* other); |
| 76 | status_t setTo(const char16_t* other, size_t len); |
| 77 | status_t setTo(const String16& other, |
| 78 | size_t len, |
| 79 | size_t begin=0); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | status_t append(const String16& other); |
| 82 | status_t append(const char16_t* other, size_t len); |
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); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 85 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | inline String16& operator+=(const String16& other); |
| 87 | inline String16 operator+(const String16& other) const; |
| 88 | |
| 89 | status_t insert(size_t pos, const char16_t* chrs); |
| 90 | status_t insert(size_t pos, |
| 91 | const char16_t* chrs, size_t len); |
| 92 | |
| 93 | ssize_t findFirst(char16_t c) const; |
| 94 | ssize_t findLast(char16_t c) const; |
| 95 | |
| 96 | bool startsWith(const String16& prefix) const; |
| 97 | bool startsWith(const char16_t* prefix) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 98 | |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 99 | bool contains(const char16_t* chrs) const; |
| 100 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | status_t makeLower(); |
| 102 | |
| 103 | status_t replaceAll(char16_t replaceThis, |
| 104 | char16_t withThis); |
| 105 | |
| 106 | status_t remove(size_t len, size_t begin=0); |
| 107 | |
| 108 | inline int compare(const String16& other) const; |
| 109 | |
| 110 | inline bool operator<(const String16& other) const; |
| 111 | inline bool operator<=(const String16& other) const; |
| 112 | inline bool operator==(const String16& other) const; |
| 113 | inline bool operator!=(const String16& other) const; |
| 114 | inline bool operator>=(const String16& other) const; |
| 115 | inline bool operator>(const String16& other) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 116 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | inline bool operator<(const char16_t* other) const; |
| 118 | inline bool operator<=(const char16_t* other) const; |
| 119 | inline bool operator==(const char16_t* other) const; |
| 120 | inline bool operator!=(const char16_t* other) const; |
| 121 | inline bool operator>=(const char16_t* other) const; |
| 122 | inline bool operator>(const char16_t* other) const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | inline operator const char16_t*() const; |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 125 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | private: |
| 127 | const char16_t* mString; |
| 128 | }; |
| 129 | |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 130 | // String16 can be trivially moved using memcpy() because moving does not |
| 131 | // require any change to the underlying SharedBuffer contents or reference count. |
| 132 | ANDROID_TRIVIAL_MOVE_TRAIT(String16) |
| 133 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | // --------------------------------------------------------------------------- |
| 135 | // No user servicable parts below. |
| 136 | |
| 137 | inline int compare_type(const String16& lhs, const String16& rhs) |
| 138 | { |
| 139 | return lhs.compare(rhs); |
| 140 | } |
| 141 | |
| 142 | inline int strictly_order_type(const String16& lhs, const String16& rhs) |
| 143 | { |
| 144 | return compare_type(lhs, rhs) < 0; |
| 145 | } |
| 146 | |
| 147 | inline const char16_t* String16::string() const |
| 148 | { |
| 149 | return mString; |
| 150 | } |
| 151 | |
Samuel Tan | 9ac4e00 | 2016-02-16 14:20:05 -0800 | [diff] [blame] | 152 | inline std::string String16::std_string(const String16& str) |
| 153 | { |
| 154 | return std::string(String8(str).string()); |
| 155 | } |
| 156 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | inline String16& String16::operator=(const String16& other) |
| 158 | { |
| 159 | setTo(other); |
| 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | inline String16& String16::operator+=(const String16& other) |
| 164 | { |
| 165 | append(other); |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | inline String16 String16::operator+(const String16& other) const |
| 170 | { |
Josiah Gaskin | 9ee3fc4 | 2011-08-16 15:16:04 -0700 | [diff] [blame] | 171 | String16 tmp(*this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | tmp += other; |
| 173 | return tmp; |
| 174 | } |
| 175 | |
| 176 | inline int String16::compare(const String16& other) const |
| 177 | { |
| 178 | return strzcmp16(mString, size(), other.mString, other.size()); |
| 179 | } |
| 180 | |
| 181 | inline bool String16::operator<(const String16& other) const |
| 182 | { |
| 183 | return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
| 184 | } |
| 185 | |
| 186 | inline bool String16::operator<=(const String16& other) const |
| 187 | { |
| 188 | return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
| 189 | } |
| 190 | |
| 191 | inline bool String16::operator==(const String16& other) const |
| 192 | { |
Brad Fitzpatrick | 9d589aa | 2010-10-20 17:06:28 -0700 | [diff] [blame] | 193 | return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 char16_t* other) const |
| 212 | { |
| 213 | return strcmp16(mString, other) < 0; |
| 214 | } |
| 215 | |
| 216 | inline bool String16::operator<=(const char16_t* other) const |
| 217 | { |
| 218 | return strcmp16(mString, other) <= 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 String16::operator const char16_t*() const |
| 242 | { |
| 243 | return mString; |
| 244 | } |
| 245 | |
Pirama Arumuga Nainar | eab48ce | 2018-04-10 14:31:29 -0700 | [diff] [blame] | 246 | } // namespace android |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
| 248 | // --------------------------------------------------------------------------- |
| 249 | |
| 250 | #endif // ANDROID_STRING16_H |