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/SharedBuffer.h>
22
23#include <stdint.h>
24#include <sys/types.h>
25
26// ---------------------------------------------------------------------------
27
28extern "C" {
29
30typedef uint16_t char16_t;
31
32// Standard string functions on char16 strings.
33int strcmp16(const char16_t *, const char16_t *);
34int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
35size_t strlen16(const char16_t *);
36size_t strnlen16(const char16_t *, size_t);
37char16_t *strcpy16(char16_t *, const char16_t *);
38char16_t *strncpy16(char16_t *, const char16_t *, size_t);
39
40// Version of comparison that supports embedded nulls.
41// This is different than strncmp() because we don't stop
42// at a nul character and consider the strings to be different
43// if the lengths are different (thus we need to supply the
44// lengths of both strings).  This can also be used when
45// your string is not nul-terminated as it will have the
46// equivalent result as strcmp16 (unlike strncmp16).
47int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
48
49// Version of strzcmp16 for comparing strings in different endianness.
50int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2);
51
52}
53
54// ---------------------------------------------------------------------------
55
56namespace android {
57
58class String8;
59class TextOutput;
60
61//! This is a string holding UTF-16 characters.
62class String16
63{
64public:
65                                String16();
66                                String16(const String16& o);
67                                String16(const String16& o,
68                                         size_t len,
69                                         size_t begin=0);
70    explicit                    String16(const char16_t* o);
71    explicit                    String16(const char16_t* o, size_t len);
72    explicit                    String16(const String8& o);
73    explicit                    String16(const char* o);
74    explicit                    String16(const char* o, size_t len);
75
76                                ~String16();
77
78    inline  const char16_t*     string() const;
79    inline  size_t              size() const;
80
81    inline  const SharedBuffer* sharedBuffer() const;
82
83            void                setTo(const String16& other);
84            status_t            setTo(const char16_t* other);
85            status_t            setTo(const char16_t* other, size_t len);
86            status_t            setTo(const String16& other,
87                                      size_t len,
88                                      size_t begin=0);
89
90            status_t            append(const String16& other);
91            status_t            append(const char16_t* other, size_t len);
92
93    inline  String16&           operator=(const String16& other);
94
95    inline  String16&           operator+=(const String16& other);
96    inline  String16            operator+(const String16& other) const;
97
98            status_t            insert(size_t pos, const char16_t* chrs);
99            status_t            insert(size_t pos,
100                                       const char16_t* chrs, size_t len);
101
102            ssize_t             findFirst(char16_t c) const;
103            ssize_t             findLast(char16_t c) const;
104
105            bool                startsWith(const String16& prefix) const;
106            bool                startsWith(const char16_t* prefix) const;
107
108            status_t            makeLower();
109
110            status_t            replaceAll(char16_t replaceThis,
111                                           char16_t withThis);
112
113            status_t            remove(size_t len, size_t begin=0);
114
115    inline  int                 compare(const String16& other) const;
116
117    inline  bool                operator<(const String16& other) const;
118    inline  bool                operator<=(const String16& other) const;
119    inline  bool                operator==(const String16& other) const;
120    inline  bool                operator!=(const String16& other) const;
121    inline  bool                operator>=(const String16& other) const;
122    inline  bool                operator>(const String16& other) const;
123
124    inline  bool                operator<(const char16_t* other) const;
125    inline  bool                operator<=(const char16_t* other) const;
126    inline  bool                operator==(const char16_t* other) const;
127    inline  bool                operator!=(const char16_t* other) const;
128    inline  bool                operator>=(const char16_t* other) const;
129    inline  bool                operator>(const char16_t* other) const;
130
131    inline                      operator const char16_t*() const;
132
133private:
134            const char16_t*     mString;
135};
136
137TextOutput& operator<<(TextOutput& to, const String16& val);
138
139// ---------------------------------------------------------------------------
140// No user servicable parts below.
141
142inline int compare_type(const String16& lhs, const String16& rhs)
143{
144    return lhs.compare(rhs);
145}
146
147inline int strictly_order_type(const String16& lhs, const String16& rhs)
148{
149    return compare_type(lhs, rhs) < 0;
150}
151
152inline const char16_t* String16::string() const
153{
154    return mString;
155}
156
157inline size_t String16::size() const
158{
159    return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
160}
161
162inline const SharedBuffer* String16::sharedBuffer() const
163{
164    return SharedBuffer::bufferFromData(mString);
165}
166
167inline String16& String16::operator=(const String16& other)
168{
169    setTo(other);
170    return *this;
171}
172
173inline String16& String16::operator+=(const String16& other)
174{
175    append(other);
176    return *this;
177}
178
179inline String16 String16::operator+(const String16& other) const
180{
181    String16 tmp;
182    tmp += other;
183    return tmp;
184}
185
186inline int String16::compare(const String16& other) const
187{
188    return strzcmp16(mString, size(), other.mString, other.size());
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 String16& other) const
202{
203    return strzcmp16(mString, size(), other.mString, other.size()) == 0;
204}
205
206inline bool String16::operator!=(const String16& other) const
207{
208    return strzcmp16(mString, size(), other.mString, other.size()) != 0;
209}
210
211inline bool String16::operator>=(const String16& other) const
212{
213    return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
214}
215
216inline bool String16::operator>(const String16& other) const
217{
218    return strzcmp16(mString, size(), other.mString, other.size()) > 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 bool String16::operator==(const char16_t* other) const
232{
233    return strcmp16(mString, other) == 0;
234}
235
236inline bool String16::operator!=(const char16_t* other) const
237{
238    return strcmp16(mString, other) != 0;
239}
240
241inline bool String16::operator>=(const char16_t* other) const
242{
243    return strcmp16(mString, other) >= 0;
244}
245
246inline bool String16::operator>(const char16_t* other) const
247{
248    return strcmp16(mString, other) > 0;
249}
250
251inline String16::operator const char16_t*() const
252{
253    return mString;
254}
255
256}; // namespace android
257
258// ---------------------------------------------------------------------------
259
260#endif // ANDROID_STRING16_H
261