blob: f6433a829d4ce2ab48d9dbced94b1f0ca4c74ea3 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Tan9ac4e002016-02-16 14:20:05 -080020#include <string> // for std::string
21
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <utils/Errors.h>
Samuel Tan9ac4e002016-02-16 14:20:05 -080023#include <utils/String8.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070024#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
26// ---------------------------------------------------------------------------
27
28extern "C" {
29
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080030}
31
32// ---------------------------------------------------------------------------
33
34namespace android {
35
Kenny Root9a2d83e2009-12-04 09:38:48 -080036// ---------------------------------------------------------------------------
37
Sergio Girod2529f22015-09-23 16:22:59 +010038class SharedBuffer;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039class String8;
40class TextOutput;
41
42//! This is a string holding UTF-16 characters.
43class String16
44{
45public:
Mathias Agopian4485d0d2013-05-08 16:04:13 -070046 /* 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 Projectcbb10112009-03-03 19:31:44 -080054 String16();
Mathias Agopian4485d0d2013-05-08 16:04:13 -070055 explicit String16(StaticLinkage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080056 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 Tanf9d16ef2016-02-16 15:17:10 -080067
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 inline const char16_t* string() const;
Samuel Tanf9d16ef2016-02-16 15:17:10 -080069
Steven Moreland2aac3352017-03-10 22:31:08 -080070//TODO(b/35363681): remove
71private:
Samuel Tan9ac4e002016-02-16 14:20:05 -080072 static inline std::string std_string(const String16& str);
Steven Moreland2aac3352017-03-10 22:31:08 -080073public:
Sergio Girod2529f22015-09-23 16:22:59 +010074 size_t size() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 void setTo(const String16& other);
76 status_t setTo(const char16_t* other);
77 status_t setTo(const char16_t* other, size_t len);
78 status_t setTo(const String16& other,
79 size_t len,
80 size_t begin=0);
Samuel Tanf9d16ef2016-02-16 15:17:10 -080081
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080082 status_t append(const String16& other);
83 status_t append(const char16_t* other, size_t len);
Samuel Tanf9d16ef2016-02-16 15:17:10 -080084
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085 inline String16& operator=(const String16& other);
Samuel Tanf9d16ef2016-02-16 15:17:10 -080086
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080087 inline String16& operator+=(const String16& other);
88 inline String16 operator+(const String16& other) const;
89
90 status_t insert(size_t pos, const char16_t* chrs);
91 status_t insert(size_t pos,
92 const char16_t* chrs, size_t len);
93
94 ssize_t findFirst(char16_t c) const;
95 ssize_t findLast(char16_t c) const;
96
97 bool startsWith(const String16& prefix) const;
98 bool startsWith(const char16_t* prefix) const;
Samuel Tanf9d16ef2016-02-16 15:17:10 -080099
Michael Wright5bacef32016-05-09 14:43:31 +0100100 bool contains(const char16_t* chrs) const;
101
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800102 status_t makeLower();
103
104 status_t replaceAll(char16_t replaceThis,
105 char16_t withThis);
106
107 status_t remove(size_t len, size_t begin=0);
108
109 inline int compare(const String16& other) const;
110
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;
116 inline bool operator>(const String16& other) const;
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800117
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800118 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;
123 inline bool operator>(const char16_t* other) const;
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800124
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800125 inline operator const char16_t*() const;
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800126
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800127private:
128 const char16_t* mString;
129};
130
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700131// String16 can be trivially moved using memcpy() because moving does not
132// require any change to the underlying SharedBuffer contents or reference count.
133ANDROID_TRIVIAL_MOVE_TRAIT(String16)
134
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800135// ---------------------------------------------------------------------------
136// No user servicable parts below.
137
138inline int compare_type(const String16& lhs, const String16& rhs)
139{
140 return lhs.compare(rhs);
141}
142
143inline int strictly_order_type(const String16& lhs, const String16& rhs)
144{
145 return compare_type(lhs, rhs) < 0;
146}
147
148inline const char16_t* String16::string() const
149{
150 return mString;
151}
152
Samuel Tan9ac4e002016-02-16 14:20:05 -0800153inline std::string String16::std_string(const String16& str)
154{
155 return std::string(String8(str).string());
156}
157
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800158inline String16& String16::operator=(const String16& other)
159{
160 setTo(other);
161 return *this;
162}
163
164inline String16& String16::operator+=(const String16& other)
165{
166 append(other);
167 return *this;
168}
169
170inline String16 String16::operator+(const String16& other) const
171{
Josiah Gaskin9ee3fc42011-08-16 15:16:04 -0700172 String16 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800173 tmp += other;
174 return tmp;
175}
176
177inline int String16::compare(const String16& other) const
178{
179 return strzcmp16(mString, size(), other.mString, other.size());
180}
181
182inline bool String16::operator<(const String16& other) const
183{
184 return strzcmp16(mString, size(), other.mString, other.size()) < 0;
185}
186
187inline bool String16::operator<=(const String16& other) const
188{
189 return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
190}
191
192inline bool String16::operator==(const String16& other) const
193{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700194 return strzcmp16(mString, size(), other.mString, other.size()) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800195}
196
197inline bool String16::operator!=(const String16& other) const
198{
199 return strzcmp16(mString, size(), other.mString, other.size()) != 0;
200}
201
202inline bool String16::operator>=(const String16& other) const
203{
204 return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
205}
206
207inline bool String16::operator>(const String16& other) const
208{
209 return strzcmp16(mString, size(), other.mString, other.size()) > 0;
210}
211
212inline bool String16::operator<(const char16_t* other) const
213{
214 return strcmp16(mString, other) < 0;
215}
216
217inline bool String16::operator<=(const char16_t* other) const
218{
219 return strcmp16(mString, other) <= 0;
220}
221
222inline bool String16::operator==(const char16_t* other) const
223{
224 return strcmp16(mString, other) == 0;
225}
226
227inline bool String16::operator!=(const char16_t* other) const
228{
229 return strcmp16(mString, other) != 0;
230}
231
232inline bool String16::operator>=(const char16_t* other) const
233{
234 return strcmp16(mString, other) >= 0;
235}
236
237inline bool String16::operator>(const char16_t* other) const
238{
239 return strcmp16(mString, other) > 0;
240}
241
242inline String16::operator const char16_t*() const
243{
244 return mString;
245}
246
247}; // namespace android
248
249// ---------------------------------------------------------------------------
250
251#endif // ANDROID_STRING16_H