|
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 |
|
28 extern "C" { |
|
29 |
|
30 #if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98 |
|
31 typedef uint16_t char16_t; |
|
32 #endif |
|
33 |
|
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); |
|
41 |
|
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); |
|
50 |
|
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); |
|
53 |
|
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); |
|
56 |
|
57 } |
|
58 |
|
59 // --------------------------------------------------------------------------- |
|
60 |
|
61 namespace android { |
|
62 |
|
63 // --------------------------------------------------------------------------- |
|
64 |
|
65 class String8; |
|
66 class TextOutput; |
|
67 |
|
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); |
|
82 |
|
83 ~String16(); |
|
84 |
|
85 inline const char16_t* string() const; |
|
86 inline size_t size() const; |
|
87 |
|
88 inline const SharedBuffer* sharedBuffer() const; |
|
89 |
|
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); |
|
96 |
|
97 status_t append(const String16& other); |
|
98 status_t append(const char16_t* other, size_t len); |
|
99 |
|
100 inline String16& operator=(const String16& other); |
|
101 |
|
102 inline String16& operator+=(const String16& other); |
|
103 inline String16 operator+(const String16& other) const; |
|
104 |
|
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); |
|
108 |
|
109 ssize_t findFirst(char16_t c) const; |
|
110 ssize_t findLast(char16_t c) const; |
|
111 |
|
112 bool startsWith(const String16& prefix) const; |
|
113 bool startsWith(const char16_t* prefix) const; |
|
114 |
|
115 status_t makeLower(); |
|
116 |
|
117 status_t replaceAll(char16_t replaceThis, |
|
118 char16_t withThis); |
|
119 |
|
120 status_t remove(size_t len, size_t begin=0); |
|
121 |
|
122 inline int compare(const String16& other) const; |
|
123 |
|
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; |
|
130 |
|
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; |
|
137 |
|
138 inline operator const char16_t*() const; |
|
139 |
|
140 private: |
|
141 const char16_t* mString; |
|
142 }; |
|
143 |
|
144 TextOutput& operator<<(TextOutput& to, const String16& val); |
|
145 |
|
146 // --------------------------------------------------------------------------- |
|
147 // No user servicable parts below. |
|
148 |
|
149 inline int compare_type(const String16& lhs, const String16& rhs) |
|
150 { |
|
151 return lhs.compare(rhs); |
|
152 } |
|
153 |
|
154 inline int strictly_order_type(const String16& lhs, const String16& rhs) |
|
155 { |
|
156 return compare_type(lhs, rhs) < 0; |
|
157 } |
|
158 |
|
159 inline const char16_t* String16::string() const |
|
160 { |
|
161 return mString; |
|
162 } |
|
163 |
|
164 inline size_t String16::size() const |
|
165 { |
|
166 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
|
167 } |
|
168 |
|
169 inline const SharedBuffer* String16::sharedBuffer() const |
|
170 { |
|
171 return SharedBuffer::bufferFromData(mString); |
|
172 } |
|
173 |
|
174 inline String16& String16::operator=(const String16& other) |
|
175 { |
|
176 setTo(other); |
|
177 return *this; |
|
178 } |
|
179 |
|
180 inline String16& String16::operator+=(const String16& other) |
|
181 { |
|
182 append(other); |
|
183 return *this; |
|
184 } |
|
185 |
|
186 inline String16 String16::operator+(const String16& other) const |
|
187 { |
|
188 String16 tmp; |
|
189 tmp += other; |
|
190 return tmp; |
|
191 } |
|
192 |
|
193 inline int String16::compare(const String16& other) const |
|
194 { |
|
195 return strzcmp16(mString, size(), other.mString, other.size()); |
|
196 } |
|
197 |
|
198 inline bool String16::operator<(const String16& other) const |
|
199 { |
|
200 return strzcmp16(mString, size(), other.mString, other.size()) < 0; |
|
201 } |
|
202 |
|
203 inline bool String16::operator<=(const String16& other) const |
|
204 { |
|
205 return strzcmp16(mString, size(), other.mString, other.size()) <= 0; |
|
206 } |
|
207 |
|
208 inline bool String16::operator==(const String16& other) const |
|
209 { |
|
210 return strzcmp16(mString, size(), other.mString, other.size()) == 0; |
|
211 } |
|
212 |
|
213 inline bool String16::operator!=(const String16& other) const |
|
214 { |
|
215 return strzcmp16(mString, size(), other.mString, other.size()) != 0; |
|
216 } |
|
217 |
|
218 inline bool String16::operator>=(const String16& other) const |
|
219 { |
|
220 return strzcmp16(mString, size(), other.mString, other.size()) >= 0; |
|
221 } |
|
222 |
|
223 inline bool String16::operator>(const String16& other) const |
|
224 { |
|
225 return strzcmp16(mString, size(), other.mString, other.size()) > 0; |
|
226 } |
|
227 |
|
228 inline bool String16::operator<(const char16_t* other) const |
|
229 { |
|
230 return strcmp16(mString, other) < 0; |
|
231 } |
|
232 |
|
233 inline bool String16::operator<=(const char16_t* other) const |
|
234 { |
|
235 return strcmp16(mString, other) <= 0; |
|
236 } |
|
237 |
|
238 inline bool String16::operator==(const char16_t* other) const |
|
239 { |
|
240 return strcmp16(mString, other) == 0; |
|
241 } |
|
242 |
|
243 inline bool String16::operator!=(const char16_t* other) const |
|
244 { |
|
245 return strcmp16(mString, other) != 0; |
|
246 } |
|
247 |
|
248 inline bool String16::operator>=(const char16_t* other) const |
|
249 { |
|
250 return strcmp16(mString, other) >= 0; |
|
251 } |
|
252 |
|
253 inline bool String16::operator>(const char16_t* other) const |
|
254 { |
|
255 return strcmp16(mString, other) > 0; |
|
256 } |
|
257 |
|
258 inline String16::operator const char16_t*() const |
|
259 { |
|
260 return mString; |
|
261 } |
|
262 |
|
263 }; // namespace android |
|
264 |
|
265 // --------------------------------------------------------------------------- |
|
266 |
|
267 #endif // ANDROID_STRING16_H |