2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_STRING16_H
18 #define ANDROID_STRING16_H
20 #include <utils/Errors.h>
21 #include <utils/SharedBuffer.h>
22 #include <utils/Unicode.h>
23 #include <utils/TypeHelpers.h>
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
40 //! This is a string holding UTF-16 characters.
44 /* use String16(StaticLinkage) if you're statically linking against
45 * libutils and declaring an empty static String16, e.g.:
47 * static String16 sAStaticEmptyString(String16::kEmptyString);
48 * static String16 sAnotherStaticEmptyString(sAStaticEmptyString);
50 enum StaticLinkage { kEmptyString };
53 explicit String16(StaticLinkage);
54 String16(const String16& o);
55 String16(const String16& o,
58 explicit String16(const char16_t* o);
59 explicit String16(const char16_t* o, size_t len);
60 explicit String16(const String8& o);
61 explicit String16(const char* o);
62 explicit String16(const char* o, size_t len);
66 inline const char16_t* string() const;
67 inline size_t size() const;
69 inline const SharedBuffer* sharedBuffer() const;
71 void setTo(const String16& other);
72 status_t setTo(const char16_t* other);
73 status_t setTo(const char16_t* other, size_t len);
74 status_t setTo(const String16& other,
78 status_t append(const String16& other);
79 status_t append(const char16_t* other, size_t len);
81 inline String16& operator=(const String16& other);
83 inline String16& operator+=(const String16& other);
84 inline String16 operator+(const String16& other) const;
86 status_t insert(size_t pos, const char16_t* chrs);
87 status_t insert(size_t pos,
88 const char16_t* chrs, size_t len);
90 ssize_t findFirst(char16_t c) const;
91 ssize_t findLast(char16_t c) const;
93 bool startsWith(const String16& prefix) const;
94 bool startsWith(const char16_t* prefix) const;
98 status_t replaceAll(char16_t replaceThis,
101 status_t remove(size_t len, size_t begin=0);
103 inline int compare(const String16& other) const;
105 inline bool operator<(const String16& other) const;
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;
112 inline bool operator<(const char16_t* other) const;
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;
119 inline operator const char16_t*() const;
122 const char16_t* mString;
125 // String16 can be trivially moved using memcpy() because moving does not
126 // require any change to the underlying SharedBuffer contents or reference count.
127 ANDROID_TRIVIAL_MOVE_TRAIT(String16)
129 // ---------------------------------------------------------------------------
130 // No user servicable parts below.
132 inline int compare_type(const String16& lhs, const String16& rhs)
134 return lhs.compare(rhs);
137 inline int strictly_order_type(const String16& lhs, const String16& rhs)
139 return compare_type(lhs, rhs) < 0;
142 inline const char16_t* String16::string() const
147 inline size_t String16::size() const
149 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
152 inline const SharedBuffer* String16::sharedBuffer() const
154 return SharedBuffer::bufferFromData(mString);
157 inline String16& String16::operator=(const String16& other)
163 inline String16& String16::operator+=(const String16& other)
169 inline String16 String16::operator+(const String16& other) const
176 inline int String16::compare(const String16& other) const
178 return strzcmp16(mString, size(), other.mString, other.size());
181 inline bool String16::operator<(const String16& other) const
183 return strzcmp16(mString, size(), other.mString, other.size()) < 0;
186 inline bool String16::operator<=(const String16& other) const
188 return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
191 inline bool String16::operator==(const String16& other) const
193 return strzcmp16(mString, size(), other.mString, other.size()) == 0;
196 inline bool String16::operator!=(const String16& other) const
198 return strzcmp16(mString, size(), other.mString, other.size()) != 0;
201 inline bool String16::operator>=(const String16& other) const
203 return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
206 inline bool String16::operator>(const String16& other) const
208 return strzcmp16(mString, size(), other.mString, other.size()) > 0;
211 inline bool String16::operator<(const char16_t* other) const
213 return strcmp16(mString, other) < 0;
216 inline bool String16::operator<=(const char16_t* other) const
218 return strcmp16(mString, other) <= 0;
221 inline bool String16::operator==(const char16_t* other) const
223 return strcmp16(mString, other) == 0;
226 inline bool String16::operator!=(const char16_t* other) const
228 return strcmp16(mString, other) != 0;
231 inline bool String16::operator>=(const char16_t* other) const
233 return strcmp16(mString, other) >= 0;
236 inline bool String16::operator>(const char16_t* other) const
238 return strcmp16(mString, other) > 0;
241 inline String16::operator const char16_t*() const
246 }; // namespace android
248 // ---------------------------------------------------------------------------
250 #endif // ANDROID_STRING16_H