blob: 6d4253d24dd3ee622887d60d6157bbf86b9876bb [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
20#include <utils/Errors.h>
21#include <utils/SharedBuffer.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080022#include <utils/Unicode.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070023#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
25// ---------------------------------------------------------------------------
26
27extern "C" {
28
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029}
30
31// ---------------------------------------------------------------------------
32
33namespace android {
34
Kenny Root9a2d83e2009-12-04 09:38:48 -080035// ---------------------------------------------------------------------------
36
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080037class String8;
38class TextOutput;
39
40//! This is a string holding UTF-16 characters.
41class String16
42{
43public:
44 String16();
45 String16(const String16& o);
46 String16(const String16& o,
47 size_t len,
48 size_t begin=0);
49 explicit String16(const char16_t* o);
50 explicit String16(const char16_t* o, size_t len);
51 explicit String16(const String8& o);
52 explicit String16(const char* o);
53 explicit String16(const char* o, size_t len);
54
55 ~String16();
56
57 inline const char16_t* string() const;
58 inline size_t size() const;
59
60 inline const SharedBuffer* sharedBuffer() const;
61
62 void setTo(const String16& other);
63 status_t setTo(const char16_t* other);
64 status_t setTo(const char16_t* other, size_t len);
65 status_t setTo(const String16& other,
66 size_t len,
67 size_t begin=0);
68
69 status_t append(const String16& other);
70 status_t append(const char16_t* other, size_t len);
71
72 inline String16& operator=(const String16& other);
73
74 inline String16& operator+=(const String16& other);
75 inline String16 operator+(const String16& other) const;
76
77 status_t insert(size_t pos, const char16_t* chrs);
78 status_t insert(size_t pos,
79 const char16_t* chrs, size_t len);
80
81 ssize_t findFirst(char16_t c) const;
82 ssize_t findLast(char16_t c) const;
83
84 bool startsWith(const String16& prefix) const;
85 bool startsWith(const char16_t* prefix) const;
86
87 status_t makeLower();
88
89 status_t replaceAll(char16_t replaceThis,
90 char16_t withThis);
91
92 status_t remove(size_t len, size_t begin=0);
93
94 inline int compare(const String16& other) const;
95
96 inline bool operator<(const String16& other) const;
97 inline bool operator<=(const String16& other) const;
98 inline bool operator==(const String16& other) const;
99 inline bool operator!=(const String16& other) const;
100 inline bool operator>=(const String16& other) const;
101 inline bool operator>(const String16& other) const;
102
103 inline bool operator<(const char16_t* other) const;
104 inline bool operator<=(const char16_t* other) const;
105 inline bool operator==(const char16_t* other) const;
106 inline bool operator!=(const char16_t* other) const;
107 inline bool operator>=(const char16_t* other) const;
108 inline bool operator>(const char16_t* other) const;
109
110 inline operator const char16_t*() const;
111
112private:
113 const char16_t* mString;
114};
115
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700116// String16 can be trivially moved using memcpy() because moving does not
117// require any change to the underlying SharedBuffer contents or reference count.
118ANDROID_TRIVIAL_MOVE_TRAIT(String16)
119
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800120// ---------------------------------------------------------------------------
121// No user servicable parts below.
122
123inline int compare_type(const String16& lhs, const String16& rhs)
124{
125 return lhs.compare(rhs);
126}
127
128inline int strictly_order_type(const String16& lhs, const String16& rhs)
129{
130 return compare_type(lhs, rhs) < 0;
131}
132
133inline const char16_t* String16::string() const
134{
135 return mString;
136}
137
138inline size_t String16::size() const
139{
140 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
141}
142
143inline const SharedBuffer* String16::sharedBuffer() const
144{
145 return SharedBuffer::bufferFromData(mString);
146}
147
148inline String16& String16::operator=(const String16& other)
149{
150 setTo(other);
151 return *this;
152}
153
154inline String16& String16::operator+=(const String16& other)
155{
156 append(other);
157 return *this;
158}
159
160inline String16 String16::operator+(const String16& other) const
161{
Josiah Gaskin9ee3fc42011-08-16 15:16:04 -0700162 String16 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800163 tmp += other;
164 return tmp;
165}
166
167inline int String16::compare(const String16& other) const
168{
169 return strzcmp16(mString, size(), other.mString, other.size());
170}
171
172inline bool String16::operator<(const String16& other) const
173{
174 return strzcmp16(mString, size(), other.mString, other.size()) < 0;
175}
176
177inline bool String16::operator<=(const String16& other) const
178{
179 return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
180}
181
182inline bool String16::operator==(const String16& other) const
183{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700184 return strzcmp16(mString, size(), other.mString, other.size()) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800185}
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{
194 return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
195}
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 char16_t* other) const
203{
204 return strcmp16(mString, other) < 0;
205}
206
207inline bool String16::operator<=(const char16_t* other) const
208{
209 return strcmp16(mString, other) <= 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 String16::operator const char16_t*() const
233{
234 return mString;
235}
236
237}; // namespace android
238
239// ---------------------------------------------------------------------------
240
241#endif // ANDROID_STRING16_H