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
20#include <utils/Errors.h>
21#include <utils/Unicode.h>
22#include <utils/TypeHelpers.h>
23
24// ---------------------------------------------------------------------------
25
26extern "C" {
27
28}
29
30// ---------------------------------------------------------------------------
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36class SharedBuffer;
37class String8;
38class TextOutput;
39
40//! This is a string holding UTF-16 characters.
41class String16
42{
43public:
44    /* 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
52                                String16();
53    explicit                    String16(StaticLinkage);
54                                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;
67
68            size_t              size() const;
69            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            bool                contains(const char16_t* chrs) const;
95
96            status_t            makeLower();
97
98            status_t            replaceAll(char16_t replaceThis,
99                                           char16_t withThis);
100
101            status_t            remove(size_t len, size_t begin=0);
102
103    inline  int                 compare(const String16& other) const;
104
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;
111
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;
118
119    inline                      operator const char16_t*() const;
120
121private:
122            const char16_t*     mString;
123};
124
125// String16 can be trivially moved using memcpy() because moving does not
126// require any change to the underlying SharedBuffer contents or reference count.
127ANDROID_TRIVIAL_MOVE_TRAIT(String16)
128
129// ---------------------------------------------------------------------------
130// No user servicable parts below.
131
132inline int compare_type(const String16& lhs, const String16& rhs)
133{
134    return lhs.compare(rhs);
135}
136
137inline int strictly_order_type(const String16& lhs, const String16& rhs)
138{
139    return compare_type(lhs, rhs) < 0;
140}
141
142inline const char16_t* String16::string() const
143{
144    return mString;
145}
146
147inline String16& String16::operator=(const String16& other)
148{
149    setTo(other);
150    return *this;
151}
152
153inline String16& String16::operator+=(const String16& other)
154{
155    append(other);
156    return *this;
157}
158
159inline String16 String16::operator+(const String16& other) const
160{
161    String16 tmp(*this);
162    tmp += other;
163    return tmp;
164}
165
166inline int String16::compare(const String16& other) const
167{
168    return strzcmp16(mString, size(), other.mString, other.size());
169}
170
171inline bool String16::operator<(const String16& other) const
172{
173    return strzcmp16(mString, size(), other.mString, other.size()) < 0;
174}
175
176inline bool String16::operator<=(const String16& other) const
177{
178    return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
179}
180
181inline bool String16::operator==(const String16& other) const
182{
183    return strzcmp16(mString, size(), other.mString, other.size()) == 0;
184}
185
186inline bool String16::operator!=(const String16& other) const
187{
188    return strzcmp16(mString, size(), other.mString, other.size()) != 0;
189}
190
191inline bool String16::operator>=(const String16& other) const
192{
193    return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
194}
195
196inline bool String16::operator>(const String16& other) const
197{
198    return strzcmp16(mString, size(), other.mString, other.size()) > 0;
199}
200
201inline bool String16::operator<(const char16_t* other) const
202{
203    return strcmp16(mString, other) < 0;
204}
205
206inline bool String16::operator<=(const char16_t* other) const
207{
208    return strcmp16(mString, other) <= 0;
209}
210
211inline bool String16::operator==(const char16_t* other) const
212{
213    return strcmp16(mString, other) == 0;
214}
215
216inline bool String16::operator!=(const char16_t* other) const
217{
218    return strcmp16(mString, other) != 0;
219}
220
221inline bool String16::operator>=(const char16_t* other) const
222{
223    return strcmp16(mString, other) >= 0;
224}
225
226inline bool String16::operator>(const char16_t* other) const
227{
228    return strcmp16(mString, other) > 0;
229}
230
231inline String16::operator const char16_t*() const
232{
233    return mString;
234}
235
236}; // namespace android
237
238// ---------------------------------------------------------------------------
239
240#endif // ANDROID_STRING16_H
241