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 #include <utils/String16.h>
17 #include <utils/Log.h>
18 #include <ctype.h>
19 #include "SharedBuffer.h"
20 namespace android {
getEmptyString()21 static inline char16_t* getEmptyString() {
22 static SharedBuffer* gEmptyStringBuf = [] {
23 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
24 char16_t* str = static_cast<char16_t*>(buf->data());
25 *str = 0;
26 return buf;
27 }();
28 gEmptyStringBuf->acquire();
29 return static_cast<char16_t*>(gEmptyStringBuf->data());
30 }
31 // ---------------------------------------------------------------------------
allocFromUTF8(const char * u8str,size_t u8len)32 static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
33 {
34 if (u8len == 0) return getEmptyString();
35 const uint8_t* u8cur = (const uint8_t*) u8str;
36 const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
37 if (u16len < 0) {
38 return getEmptyString();
39 }
40 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
41 if (buf) {
42 u8cur = (const uint8_t*) u8str;
43 char16_t* u16str = (char16_t*)buf->data();
44 utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1);
45 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
46 //printHexData(1, str, buf->size(), 16, 1);
47 //printf("\n");
48 return u16str;
49 }
50 return getEmptyString();
51 }
allocFromUTF16(const char16_t * u16str,size_t u16len)52 static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len) {
53 if (u16len >= SIZE_MAX / sizeof(char16_t)) {
54 android_errorWriteLog(0x534e4554, "73826242");
55 abort();
56 }
57 SharedBuffer* buf = SharedBuffer::alloc((u16len + 1) * sizeof(char16_t));
58 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
59 if (buf) {
60 char16_t* str = (char16_t*)buf->data();
61 memcpy(str, u16str, u16len * sizeof(char16_t));
62 str[u16len] = 0;
63 return str;
64 }
65 return getEmptyString();
66 }
67 // ---------------------------------------------------------------------------
String16()68 String16::String16()
69 : mString(getEmptyString())
70 {
71 }
String16(StaticLinkage)72 String16::String16(StaticLinkage)
73 : mString(nullptr)
74 {
75 // this constructor is used when we can't rely on the static-initializers
76 // having run. In this case we always allocate an empty string. It's less
77 // efficient than using getEmptyString(), but we assume it's uncommon.
78 char16_t* data = static_cast<char16_t*>(
79 SharedBuffer::alloc(sizeof(char16_t))->data());
80 data[0] = 0;
81 mString = data;
82 }
String16(const String16 & o)83 String16::String16(const String16& o)
84 : mString(o.mString)
85 {
86 SharedBuffer::bufferFromData(mString)->acquire();
87 }
String16(const String16 & o,size_t len,size_t begin)88 String16::String16(const String16& o, size_t len, size_t begin)
89 : mString(getEmptyString())
90 {
91 setTo(o, len, begin);
92 }
String16(const char16_t * o)93 String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
String16(const char16_t * o,size_t len)94 String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
String16(const String8 & o)95 String16::String16(const String8& o)
96 : mString(allocFromUTF8(o.string(), o.size()))
97 {
98 }
String16(const char * o)99 String16::String16(const char* o)
100 : mString(allocFromUTF8(o, strlen(o)))
101 {
102 }
String16(const char * o,size_t len)103 String16::String16(const char* o, size_t len)
104 : mString(allocFromUTF8(o, len))
105 {
106 }
~String16()107 String16::~String16()
108 {
109 SharedBuffer::bufferFromData(mString)->release();
110 }
size() const111 size_t String16::size() const
112 {
113 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
114 }
setTo(const String16 & other)115 void String16::setTo(const String16& other)
116 {
117 SharedBuffer::bufferFromData(other.mString)->acquire();
118 SharedBuffer::bufferFromData(mString)->release();
119 mString = other.mString;
120 }
setTo(const String16 & other,size_t len,size_t begin)121 status_t String16::setTo(const String16& other, size_t len, size_t begin)
122 {
123 const size_t N = other.size();
124 if (begin >= N) {
125 SharedBuffer::bufferFromData(mString)->release();
126 mString = getEmptyString();
127 return OK;
128 }
129 if ((begin+len) > N) len = N-begin;
130 if (begin == 0 && len == N) {
131 setTo(other);
132 return OK;
133 }
134 if (&other == this) {
135 LOG_ALWAYS_FATAL("Not implemented");
136 }
137 return setTo(other.string()+begin, len);
138 }
setTo(const char16_t * other)139 status_t String16::setTo(const char16_t* other)
140 {
141 return setTo(other, strlen16(other));
142 }
setTo(const char16_t * other,size_t len)143 status_t String16::setTo(const char16_t* other, size_t len)
144 {
145 if (len >= SIZE_MAX / sizeof(char16_t)) {
146 android_errorWriteLog(0x534e4554, "73826242");
147 abort();
148 }
149 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
150 ->editResize((len+1)*sizeof(char16_t));
151 if (buf) {
152 char16_t* str = (char16_t*)buf->data();
153 memmove(str, other, len*sizeof(char16_t));
154 str[len] = 0;
155 mString = str;
156 return OK;
157 }
158 return NO_MEMORY;
159 }
append(const String16 & other)160 status_t String16::append(const String16& other)
161 {
162 const size_t myLen = size();
163 const size_t otherLen = other.size();
164 if (myLen == 0) {
165 setTo(other);
166 return OK;
167 } else if (otherLen == 0) {
168 return OK;
169 }
170 if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
171 android_errorWriteLog(0x534e4554, "73826242");
172 abort();
173 }
174 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
175 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
176 if (buf) {
177 char16_t* str = (char16_t*)buf->data();
178 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
179 mString = str;
180 return OK;
181 }
182 return NO_MEMORY;
183 }
append(const char16_t * chrs,size_t otherLen)184 status_t String16::append(const char16_t* chrs, size_t otherLen)
185 {
186 const size_t myLen = size();
187 if (myLen == 0) {
188 setTo(chrs, otherLen);
189 return OK;
190 } else if (otherLen == 0) {
191 return OK;
192 }
193 if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
194 android_errorWriteLog(0x534e4554, "73826242");
195 abort();
196 }
197 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
198 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
199 if (buf) {
200 char16_t* str = (char16_t*)buf->data();
201 memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
202 str[myLen+otherLen] = 0;
203 mString = str;
204 return OK;
205 }
206 return NO_MEMORY;
207 }
insert(size_t pos,const char16_t * chrs)208 status_t String16::insert(size_t pos, const char16_t* chrs)
209 {
210 return insert(pos, chrs, strlen16(chrs));
211 }
insert(size_t pos,const char16_t * chrs,size_t len)212 status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
213 {
214 const size_t myLen = size();
215 if (myLen == 0) {
216 return setTo(chrs, len);
217 return OK;
218 } else if (len == 0) {
219 return OK;
220 }
221 if (pos > myLen) pos = myLen;
222 #if 0
223 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
224 String8(*this).string(), pos,
225 len, myLen, String8(chrs, len).string());
226 #endif
227 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
228 ->editResize((myLen+len+1)*sizeof(char16_t));
229 if (buf) {
230 char16_t* str = (char16_t*)buf->data();
231 if (pos < myLen) {
232 memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t));
233 }
234 memcpy(str+pos, chrs, len*sizeof(char16_t));
235 str[myLen+len] = 0;
236 mString = str;
237 #if 0
238 printf("Result (%d chrs): %s\n", size(), String8(*this).string());
239 #endif
240 return OK;
241 }
242 return NO_MEMORY;
243 }
findFirst(char16_t c) const244 ssize_t String16::findFirst(char16_t c) const
245 {
246 const char16_t* str = string();
247 const char16_t* p = str;
248 const char16_t* e = p + size();
249 while (p < e) {
250 if (*p == c) {
251 return p-str;
252 }
253 p++;
254 }
255 return -1;
256 }
findLast(char16_t c) const257 ssize_t String16::findLast(char16_t c) const
258 {
259 const char16_t* str = string();
260 const char16_t* p = str;
261 const char16_t* e = p + size();
262 while (p < e) {
263 e--;
264 if (*e == c) {
265 return e-str;
266 }
267 }
268 return -1;
269 }
startsWith(const String16 & prefix) const270 bool String16::startsWith(const String16& prefix) const
271 {
272 const size_t ps = prefix.size();
273 if (ps > size()) return false;
274 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
275 }
startsWith(const char16_t * prefix) const276 bool String16::startsWith(const char16_t* prefix) const
277 {
278 const size_t ps = strlen16(prefix);
279 if (ps > size()) return false;
280 return strncmp16(mString, prefix, ps) == 0;
281 }
contains(const char16_t * chrs) const282 bool String16::contains(const char16_t* chrs) const
283 {
284 return strstr16(mString, chrs) != nullptr;
285 }
makeLower()286 status_t String16::makeLower()
287 {
288 const size_t N = size();
289 const char16_t* str = string();
290 char16_t* edit = nullptr;
291 for (size_t i=0; i<N; i++) {
292 const char16_t v = str[i];
293 if (v >= 'A' && v <= 'Z') {
294 if (!edit) {
295 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
296 if (!buf) {
297 return NO_MEMORY;
298 }
299 edit = (char16_t*)buf->data();
300 mString = str = edit;
301 }
302 edit[i] = tolower((char)v);
303 }
304 }
305 return OK;
306 }
replaceAll(char16_t replaceThis,char16_t withThis)307 status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
308 {
309 const size_t N = size();
310 const char16_t* str = string();
311 char16_t* edit = nullptr;
312 for (size_t i=0; i<N; i++) {
313 if (str[i] == replaceThis) {
314 if (!edit) {
315 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
316 if (!buf) {
317 return NO_MEMORY;
318 }
319 edit = (char16_t*)buf->data();
320 mString = str = edit;
321 }
322 edit[i] = withThis;
323 }
324 }
325 return OK;
326 }
remove(size_t len,size_t begin)327 status_t String16::remove(size_t len, size_t begin)
328 {
329 const size_t N = size();
330 if (begin >= N) {
331 SharedBuffer::bufferFromData(mString)->release();
332 mString = getEmptyString();
333 return OK;
334 }
335 if ((begin+len) > N) len = N-begin;
336 if (begin == 0 && len == N) {
337 return OK;
338 }
339 if (begin > 0) {
340 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
341 ->editResize((N+1)*sizeof(char16_t));
342 if (!buf) {
343 return NO_MEMORY;
344 }
345 char16_t* str = (char16_t*)buf->data();
346 memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
347 mString = str;
348 }
349 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
350 ->editResize((len+1)*sizeof(char16_t));
351 if (buf) {
352 char16_t* str = (char16_t*)buf->data();
353 str[len] = 0;
354 mString = str;
355 return OK;
356 }
357 return NO_MEMORY;
358 }
359 }; // namespace android
360