Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 */
17 #ifndef ANDROID_STRING16_H
18 #define ANDROID_STRING16_H
20 #include <utils/Errors.h>
21 #include <utils/SharedBuffer.h>
23 #include <stdint.h>
24 #include <sys/types.h>
26 // ---------------------------------------------------------------------------
28 extern "C" {
30 #if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98
31 typedef uint16_t char16_t;
32 #endif
34 // Standard string functions on char16 strings.
35 int strcmp16(const char16_t *, const char16_t *);
36 int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
37 size_t strlen16(const char16_t *);
38 size_t strnlen16(const char16_t *, size_t);
39 char16_t *strcpy16(char16_t *, const char16_t *);
40 char16_t *strncpy16(char16_t *, const char16_t *, size_t);
42 // Version of comparison that supports embedded nulls.
43 // This is different than strncmp() because we don't stop
44 // at a nul character and consider the strings to be different
45 // if the lengths are different (thus we need to supply the
46 // lengths of both strings). This can also be used when
47 // your string is not nul-terminated as it will have the
48 // equivalent result as strcmp16 (unlike strncmp16).
49 int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
51 // Version of strzcmp16 for comparing strings in different endianness.
52 int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2);
54 // Convert UTF-8 to UTF-16 including surrogate pairs
55 void utf8_to_utf16(const uint8_t *src, size_t srcLen, char16_t* dst, const size_t dstLen);
57 }
59 // ---------------------------------------------------------------------------
61 namespace android {
63 // ---------------------------------------------------------------------------
65 class String8;
66 class TextOutput;
68 //! This is a string holding UTF-16 characters.
69 class String16
70 {
71 public:
72 String16();
73 String16(const String16& o);
74 String16(const String16& o,
75 size_t len,
76 size_t begin=0);
77 explicit String16(const char16_t* o);
78 explicit String16(const char16_t* o, size_t len);
79 explicit String16(const String8& o);
80 explicit String16(const char* o);
81 explicit String16(const char* o, size_t len);
83 ~String16();
85 inline const char16_t* string() const;
86 inline size_t size() const;
88 inline const SharedBuffer* sharedBuffer() const;
90 void setTo(const String16& other);
91 status_t setTo(const char16_t* other);
92 status_t setTo(const char16_t* other, size_t len);
93 status_t setTo(const String16& other,
94 size_t len,
95 size_t begin=0);
97 status_t append(const String16& other);
98 status_t append(const char16_t* other, size_t len);
100 inline String16& operator=(const String16& other);
102 inline String16& operator+=(const String16& other);
103 inline String16 operator+(const String16& other) const;
105 status_t insert(size_t pos, const char16_t* chrs);
106 status_t insert(size_t pos,
107 const char16_t* chrs, size_t len);
109 ssize_t findFirst(char16_t c) const;
110 ssize_t findLast(char16_t c) const;
112 bool startsWith(const String16& prefix) const;
113 bool startsWith(const char16_t* prefix) const;
115 status_t makeLower();
117 status_t replaceAll(char16_t replaceThis,
118 char16_t withThis);
120 status_t remove(size_t len, size_t begin=0);
122 inline int compare(const String16& other) const;
124 inline bool operator<(const String16& other) const;
125 inline bool operator<=(const String16& other) const;
126 inline bool operator==(const String16& other) const;
127 inline bool operator!=(const String16& other) const;
128 inline bool operator>=(const String16& other) const;
129 inline bool operator>(const String16& other) const;
131 inline bool operator<(const char16_t* other) const;
132 inline bool operator<=(const char16_t* other) const;
133 inline bool operator==(const char16_t* other) const;
134 inline bool operator!=(const char16_t* other) const;
135 inline bool operator>=(const char16_t* other) const;
136 inline bool operator>(const char16_t* other) const;
138 inline operator const char16_t*() const;
140 private:
141 const char16_t* mString;
142 };
144 TextOutput& operator<<(TextOutput& to, const String16& val);
146 // ---------------------------------------------------------------------------
147 // No user servicable parts below.
149 inline int compare_type(const String16& lhs, const String16& rhs)
150 {
151 return lhs.compare(rhs);
152 }
154 inline int strictly_order_type(const String16& lhs, const String16& rhs)
155 {
156 return compare_type(lhs, rhs) < 0;
157 }
159 inline const char16_t* String16::string() const
160 {
161 return mString;
162 }
164 inline size_t String16::size() const
165 {
166 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
167 }
169 inline const SharedBuffer* String16::sharedBuffer() const
170 {
171 return SharedBuffer::bufferFromData(mString);
172 }
174 inline String16& String16::operator=(const String16& other)
175 {
176 setTo(other);
177 return *this;
178 }
180 inline String16& String16::operator+=(const String16& other)
181 {
182 append(other);
183 return *this;
184 }
186 inline String16 String16::operator+(const String16& other) const
187 {
188 String16 tmp;
189 tmp += other;
190 return tmp;
191 }
193 inline int String16::compare(const String16& other) const
194 {
195 return strzcmp16(mString, size(), other.mString, other.size());
196 }
198 inline bool String16::operator<(const String16& other) const
199 {
200 return strzcmp16(mString, size(), other.mString, other.size()) < 0;
201 }
203 inline bool String16::operator<=(const String16& other) const
204 {
205 return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
206 }
208 inline bool String16::operator==(const String16& other) const
209 {
210 return strzcmp16(mString, size(), other.mString, other.size()) == 0;
211 }
213 inline bool String16::operator!=(const String16& other) const
214 {
215 return strzcmp16(mString, size(), other.mString, other.size()) != 0;
216 }
218 inline bool String16::operator>=(const String16& other) const
219 {
220 return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
221 }
223 inline bool String16::operator>(const String16& other) const
224 {
225 return strzcmp16(mString, size(), other.mString, other.size()) > 0;
226 }
228 inline bool String16::operator<(const char16_t* other) const
229 {
230 return strcmp16(mString, other) < 0;
231 }
233 inline bool String16::operator<=(const char16_t* other) const
234 {
235 return strcmp16(mString, other) <= 0;
236 }
238 inline bool String16::operator==(const char16_t* other) const
239 {
240 return strcmp16(mString, other) == 0;
241 }
243 inline bool String16::operator!=(const char16_t* other) const
244 {
245 return strcmp16(mString, other) != 0;
246 }
248 inline bool String16::operator>=(const char16_t* other) const
249 {
250 return strcmp16(mString, other) >= 0;
251 }
253 inline bool String16::operator>(const char16_t* other) const
254 {
255 return strcmp16(mString, other) > 0;
256 }
258 inline String16::operator const char16_t*() const
259 {
260 return mString;
261 }
263 }; // namespace android
265 // ---------------------------------------------------------------------------
267 #endif // ANDROID_STRING16_H