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 <iostream>
21 #include <string>
22 
23 #include <utils/Errors.h>
24 #include <utils/String8.h>
25 #include <utils/TypeHelpers.h>
26 
27 // ---------------------------------------------------------------------------
28 
29 namespace android {
30 
31 // ---------------------------------------------------------------------------
32 
33 template <size_t N>
34 class StaticString16;
35 
36 // DO NOT USE: please use std::u16string
37 
38 //! This is a string holding UTF-16 characters.
39 class String16
40 {
41 public:
42                                 String16();
43                                 String16(const String16& o);
44                                 String16(const String16& o,
45                                          size_t len,
46                                          size_t begin=0);
47     explicit                    String16(const char16_t* o);
48     explicit                    String16(const char16_t* o, size_t len);
49     explicit                    String16(const String8& o);
50     explicit                    String16(const char* o);
51     explicit                    String16(const char* o, size_t len);
52 
53                                 ~String16();
54 
55     inline  const char16_t*     string() const;
56 
57 private:
58     static inline std::string   std_string(const String16& str);
59 public:
60             size_t              size() const;
61             void                setTo(const String16& other);
62             status_t            setTo(const char16_t* other);
63             status_t            setTo(const char16_t* other, size_t len);
64             status_t            setTo(const String16& other,
65                                       size_t len,
66                                       size_t begin=0);
67 
68             status_t            append(const String16& other);
69             status_t            append(const char16_t* other, size_t len);
70 
71     inline  String16&           operator=(const String16& other);
72 
73     inline  String16&           operator+=(const String16& other);
74     inline  String16            operator+(const String16& other) const;
75 
76             status_t            insert(size_t pos, const char16_t* chrs);
77             status_t            insert(size_t pos,
78                                        const char16_t* chrs, size_t len);
79 
80             ssize_t             findFirst(char16_t c) const;
81             ssize_t             findLast(char16_t c) const;
82 
83             bool                startsWith(const String16& prefix) const;
84             bool                startsWith(const char16_t* prefix) const;
85 
86             bool                contains(const char16_t* chrs) const;
87 
88             status_t            makeLower();
89 
90             status_t            replaceAll(char16_t replaceThis,
91                                            char16_t withThis);
92 
93             status_t            remove(size_t len, size_t begin=0);
94 
95     inline  int                 compare(const String16& other) const;
96 
97     inline  bool                operator<(const String16& other) const;
98     inline  bool                operator<=(const String16& other) const;
99     inline  bool                operator==(const String16& other) const;
100     inline  bool                operator!=(const String16& other) const;
101     inline  bool                operator>=(const String16& other) const;
102     inline  bool                operator>(const String16& other) const;
103 
104     inline  bool                operator<(const char16_t* other) const;
105     inline  bool                operator<=(const char16_t* other) const;
106     inline  bool                operator==(const char16_t* other) const;
107     inline  bool                operator!=(const char16_t* other) const;
108     inline  bool                operator>=(const char16_t* other) const;
109     inline  bool                operator>(const char16_t* other) const;
110 
111     inline                      operator const char16_t*() const;
112 
113     // Static and non-static String16 behave the same for the users, so
114     // this method isn't of much use for the users. It is public for testing.
115             bool                isStaticString() const;
116 
117   private:
118     /*
119      * A flag indicating the type of underlying buffer.
120      */
121     static constexpr uint32_t kIsSharedBufferAllocated = 0x80000000;
122 
123     /*
124      * alloc() returns void* so that SharedBuffer class is not exposed.
125      */
126     static void* alloc(size_t size);
127     static char16_t* allocFromUTF8(const char* u8str, size_t u8len);
128     static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len);
129 
130     /*
131      * edit() and editResize() return void* so that SharedBuffer class
132      * is not exposed.
133      */
134     void* edit();
135     void* editResize(size_t new_size);
136 
137     void acquire();
138     void release();
139 
140     size_t staticStringSize() const;
141 
142     const char16_t* mString;
143 
144 protected:
145     /*
146      * Data structure used to allocate static storage for static String16.
147      *
148      * Note that this data structure and SharedBuffer are used interchangably
149      * as the underlying data structure for a String16.  Therefore, the layout
150      * of this data structure must match the part in SharedBuffer that is
151      * visible to String16.
152      */
153     template <size_t N>
154     struct StaticData {
155         // The high bit of 'size' is used as a flag.
156         static_assert(N - 1 < kIsSharedBufferAllocated, "StaticString16 too long!");
StaticDataStaticData157         constexpr StaticData() : size(N - 1), data{0} {}
158         const uint32_t size;
159         char16_t data[N];
160 
161         constexpr StaticData(const StaticData<N>&) = default;
162     };
163 
164     /*
165      * Helper function for constructing a StaticData object.
166      */
167     template <size_t N>
makeStaticData(const char16_t (& s)[N])168     static constexpr const StaticData<N> makeStaticData(const char16_t (&s)[N]) {
169         StaticData<N> r;
170         // The 'size' field is at the same location where mClientMetadata would
171         // be for a SharedBuffer.  We do NOT set kIsSharedBufferAllocated flag
172         // here.
173         for (size_t i = 0; i < N - 1; ++i) r.data[i] = s[i];
174         return r;
175     }
176 
177     template <size_t N>
String16(const StaticData<N> & s)178     explicit constexpr String16(const StaticData<N>& s) : mString(s.data) {}
179 
180 public:
181     template <size_t N>
String16(const StaticString16<N> & s)182     explicit constexpr String16(const StaticString16<N>& s) : mString(s.mString) {}
183 };
184 
185 // String16 can be trivially moved using memcpy() because moving does not
186 // require any change to the underlying SharedBuffer contents or reference count.
187 ANDROID_TRIVIAL_MOVE_TRAIT(String16)
188 
189 static inline std::ostream& operator<<(std::ostream& os, const String16& str) {
190     os << String8(str);
191     return os;
192 }
193 
194 // ---------------------------------------------------------------------------
195 
196 /*
197  * A StaticString16 object is a specialized String16 object.  Instead of holding
198  * the string data in a ref counted SharedBuffer object, it holds data in a
199  * buffer within StaticString16 itself.  Note that this buffer is NOT ref
200  * counted and is assumed to be available for as long as there is at least a
201  * String16 object using it.  Therefore, one must be extra careful to NEVER
202  * assign a StaticString16 to a String16 that outlives the StaticString16
203  * object.
204  *
205  * THE SAFEST APPROACH IS TO USE StaticString16 ONLY AS GLOBAL VARIABLES.
206  *
207  * A StaticString16 SHOULD NEVER APPEAR IN APIs.  USE String16 INSTEAD.
208  */
209 template <size_t N>
210 class StaticString16 : public String16 {
211 public:
StaticString16(const char16_t (& s)[N])212     constexpr StaticString16(const char16_t (&s)[N]) : String16(mData), mData(makeStaticData(s)) {}
213 
StaticString16(const StaticString16<N> & other)214     constexpr StaticString16(const StaticString16<N>& other)
215         : String16(mData), mData(other.mData) {}
216 
217     constexpr StaticString16(const StaticString16<N>&&) = delete;
218 
219     // There is no reason why one would want to 'new' a StaticString16.  Delete
220     // it to discourage misuse.
221     static void* operator new(std::size_t) = delete;
222 
223 private:
224     const StaticData<N> mData;
225 };
226 
227 template <typename F>
228 StaticString16(const F&)->StaticString16<sizeof(F) / sizeof(char16_t)>;
229 
230 // ---------------------------------------------------------------------------
231 // No user servicable parts below.
232 
compare_type(const String16 & lhs,const String16 & rhs)233 inline int compare_type(const String16& lhs, const String16& rhs)
234 {
235     return lhs.compare(rhs);
236 }
237 
strictly_order_type(const String16 & lhs,const String16 & rhs)238 inline int strictly_order_type(const String16& lhs, const String16& rhs)
239 {
240     return compare_type(lhs, rhs) < 0;
241 }
242 
string()243 inline const char16_t* String16::string() const
244 {
245     return mString;
246 }
247 
std_string(const String16 & str)248 inline std::string String16::std_string(const String16& str)
249 {
250     return std::string(String8(str).string());
251 }
252 
253 inline String16& String16::operator=(const String16& other)
254 {
255     setTo(other);
256     return *this;
257 }
258 
259 inline String16& String16::operator+=(const String16& other)
260 {
261     append(other);
262     return *this;
263 }
264 
265 inline String16 String16::operator+(const String16& other) const
266 {
267     String16 tmp(*this);
268     tmp += other;
269     return tmp;
270 }
271 
compare(const String16 & other)272 inline int String16::compare(const String16& other) const
273 {
274     return strzcmp16(mString, size(), other.mString, other.size());
275 }
276 
277 inline bool String16::operator<(const String16& other) const
278 {
279     return strzcmp16(mString, size(), other.mString, other.size()) < 0;
280 }
281 
282 inline bool String16::operator<=(const String16& other) const
283 {
284     return strzcmp16(mString, size(), other.mString, other.size()) <= 0;
285 }
286 
287 inline bool String16::operator==(const String16& other) const
288 {
289     return strzcmp16(mString, size(), other.mString, other.size()) == 0;
290 }
291 
292 inline bool String16::operator!=(const String16& other) const
293 {
294     return strzcmp16(mString, size(), other.mString, other.size()) != 0;
295 }
296 
297 inline bool String16::operator>=(const String16& other) const
298 {
299     return strzcmp16(mString, size(), other.mString, other.size()) >= 0;
300 }
301 
302 inline bool String16::operator>(const String16& other) const
303 {
304     return strzcmp16(mString, size(), other.mString, other.size()) > 0;
305 }
306 
307 inline bool String16::operator<(const char16_t* other) const
308 {
309     return strcmp16(mString, other) < 0;
310 }
311 
312 inline bool String16::operator<=(const char16_t* other) const
313 {
314     return strcmp16(mString, other) <= 0;
315 }
316 
317 inline bool String16::operator==(const char16_t* other) const
318 {
319     return strcmp16(mString, other) == 0;
320 }
321 
322 inline bool String16::operator!=(const char16_t* other) const
323 {
324     return strcmp16(mString, other) != 0;
325 }
326 
327 inline bool String16::operator>=(const char16_t* other) const
328 {
329     return strcmp16(mString, other) >= 0;
330 }
331 
332 inline bool String16::operator>(const char16_t* other) const
333 {
334     return strcmp16(mString, other) > 0;
335 }
336 
337 inline String16::operator const char16_t*() const
338 {
339     return mString;
340 }
341 
342 }  // namespace android
343 
344 // ---------------------------------------------------------------------------
345 
346 #endif // ANDROID_STRING16_H
347