blob: 9a67c7a5f91f3247436de917e800aca5def814e8 [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>
Kenny Rootba0165b2010-11-09 14:37:23 -080021#include <utils/Unicode.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070022#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023
24// ---------------------------------------------------------------------------
25
26extern "C" {
27
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028}
29
30// ---------------------------------------------------------------------------
31
32namespace android {
33
Kenny Root9a2d83e2009-12-04 09:38:48 -080034// ---------------------------------------------------------------------------
35
Sergio Girod2529f22015-09-23 16:22:59 +010036class SharedBuffer;
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:
Mathias Agopian4485d0d2013-05-08 16:04:13 -070044 /* use String16(StaticLinkage) if you're statically linking against
45 * libutils and declaring an empty static String16, e.g.:
46 *
47 * static String16 sAStaticEmptyString(String16::kEmptyString);
48 * static String16 sAnotherStaticEmptyString(sAStaticEmptyString);
49 */
50 enum StaticLinkage { kEmptyString };
51
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052 String16();
Mathias Agopian4485d0d2013-05-08 16:04:13 -070053 explicit String16(StaticLinkage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054 String16(const String16& o);
55 String16(const String16& o,
56 size_t len,
57 size_t begin=0);
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);
63
64 ~String16();
65
66 inline const char16_t* string() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067
Sergio Girod2529f22015-09-23 16:22:59 +010068 size_t size() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069 void setTo(const String16& other);
70 status_t setTo(const char16_t* other);
71 status_t setTo(const char16_t* other, size_t len);
72 status_t setTo(const String16& other,
73 size_t len,
74 size_t begin=0);
75
76 status_t append(const String16& other);
77 status_t append(const char16_t* other, size_t len);
78
79 inline String16& operator=(const String16& other);
80
81 inline String16& operator+=(const String16& other);
82 inline String16 operator+(const String16& other) const;
83
84 status_t insert(size_t pos, const char16_t* chrs);
85 status_t insert(size_t pos,
86 const char16_t* chrs, size_t len);
87
88 ssize_t findFirst(char16_t c) const;
89 ssize_t findLast(char16_t c) const;
90
91 bool startsWith(const String16& prefix) const;
92 bool startsWith(const char16_t* prefix) const;
93
94 status_t makeLower();
95
96 status_t replaceAll(char16_t replaceThis,
97 char16_t withThis);
98
99 status_t remove(size_t len, size_t begin=0);
100
101 inline int compare(const String16& other) const;
102
103 inline bool operator<(const String16& other) const;
104 inline bool operator<=(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
110 inline bool operator<(const char16_t* other) const;
111 inline bool operator<=(const char16_t* 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
117 inline operator const char16_t*() const;
118
119private:
120 const char16_t* mString;
121};
122
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700123// String16 can be trivially moved using memcpy() because moving does not
124// require any change to the underlying SharedBuffer contents or reference count.
125ANDROID_TRIVIAL_MOVE_TRAIT(String16)
126
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800127// ---------------------------------------------------------------------------
128// No user servicable parts below.
129
130inline int compare_type(const String16& lhs, const String16& rhs)
131{
132 return lhs.compare(rhs);
133}
134
135inline int strictly_order_type(const String16& lhs, const String16& rhs)
136{
137 return compare_type(lhs, rhs) < 0;
138}
139
140inline const char16_t* String16::string() const
141{
142 return mString;
143}
144
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800145inline String16& String16::operator=(const String16& other)
146{
147 setTo(other);
148 return *this;
149}
150
151inline String16& String16::operator+=(const String16& other)
152{
153 append(other);
154 return *this;
155}
156
157inline String16 String16::operator+(const String16& other) const
158{
Josiah Gaskin9ee3fc42011-08-16 15:16:04 -0700159 String16 tmp(*this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800160 tmp += other;
161 return tmp;
162}
163
164inline int String16::compare(const String16& other) const
165{
166 return strzcmp16(mString, size(), other.mString, other.size());
167}
168
169inline bool String16::operator<(const String16& other) const
170{
171 return strzcmp16(mString, size(), other.mString, other.size()) < 0;
172}
173
174inline bool String16::operator<=(const String16& other) const
175{
176 return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
177}
178
179inline bool String16::operator==(const String16& other) const
180{
Brad Fitzpatrick9d589aa2010-10-20 17:06:28 -0700181 return strzcmp16(mString, size(), other.mString, other.size()) == 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800182}
183
184inline bool String16::operator!=(const String16& other) const
185{
186 return strzcmp16(mString, size(), other.mString, other.size()) != 0;
187}
188
189inline bool String16::operator>=(const String16& other) const
190{
191 return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
192}
193
194inline bool String16::operator>(const String16& other) const
195{
196 return strzcmp16(mString, size(), other.mString, other.size()) > 0;
197}
198
199inline bool String16::operator<(const char16_t* other) const
200{
201 return strcmp16(mString, other) < 0;
202}
203
204inline bool String16::operator<=(const char16_t* other) const
205{
206 return strcmp16(mString, other) <= 0;
207}
208
209inline bool String16::operator==(const char16_t* other) const
210{
211 return strcmp16(mString, other) == 0;
212}
213
214inline bool String16::operator!=(const char16_t* other) const
215{
216 return strcmp16(mString, other) != 0;
217}
218
219inline bool String16::operator>=(const char16_t* other) const
220{
221 return strcmp16(mString, other) >= 0;
222}
223
224inline bool String16::operator>(const char16_t* other) const
225{
226 return strcmp16(mString, other) > 0;
227}
228
229inline String16::operator const char16_t*() const
230{
231 return mString;
232}
233
234}; // namespace android
235
236// ---------------------------------------------------------------------------
237
238#endif // ANDROID_STRING16_H