The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #include <utils/String16.h> |
| 18 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <utils/Log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Steven Moreland | d21cfab | 2017-03-10 08:58:36 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 23 | #include "SharedBuffer.h" |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 27 | static inline char16_t* getEmptyString() { |
| 28 | static SharedBuffer* gEmptyStringBuf = [] { |
| 29 | SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)); |
| 30 | char16_t* str = static_cast<char16_t*>(buf->data()); |
| 31 | *str = 0; |
| 32 | return buf; |
| 33 | }(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | gEmptyStringBuf->acquire(); |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 36 | return static_cast<char16_t*>(gEmptyStringBuf->data()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 41 | static char16_t* allocFromUTF8(const char* u8str, size_t u8len) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 43 | if (u8len == 0) return getEmptyString(); |
| 44 | |
| 45 | const uint8_t* u8cur = (const uint8_t*) u8str; |
| 46 | |
| 47 | const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len); |
| 48 | if (u16len < 0) { |
| 49 | return getEmptyString(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | } |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 51 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 52 | SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | if (buf) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 54 | u8cur = (const uint8_t*) u8str; |
| 55 | char16_t* u16str = (char16_t*)buf->data(); |
| 56 | |
Sergio Giro | 1dcc0c8 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 57 | utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1); |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 58 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | //printf("Created UTF-16 string from UTF-8 \"%s\":", in); |
| 60 | //printHexData(1, str, buf->size(), 16, 1); |
| 61 | //printf("\n"); |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 62 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 63 | return u16str; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | return getEmptyString(); |
| 67 | } |
| 68 | |
| 69 | // --------------------------------------------------------------------------- |
| 70 | |
| 71 | String16::String16() |
| 72 | : mString(getEmptyString()) |
| 73 | { |
| 74 | } |
| 75 | |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 76 | String16::String16(StaticLinkage) |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame^] | 77 | : mString(nullptr) |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 78 | { |
| 79 | // this constructor is used when we can't rely on the static-initializers |
| 80 | // having run. In this case we always allocate an empty string. It's less |
| 81 | // efficient than using getEmptyString(), but we assume it's uncommon. |
| 82 | |
| 83 | char16_t* data = static_cast<char16_t*>( |
| 84 | SharedBuffer::alloc(sizeof(char16_t))->data()); |
| 85 | data[0] = 0; |
| 86 | mString = data; |
| 87 | } |
| 88 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | String16::String16(const String16& o) |
| 90 | : mString(o.mString) |
| 91 | { |
| 92 | SharedBuffer::bufferFromData(mString)->acquire(); |
| 93 | } |
| 94 | |
| 95 | String16::String16(const String16& o, size_t len, size_t begin) |
| 96 | : mString(getEmptyString()) |
| 97 | { |
| 98 | setTo(o, len, begin); |
| 99 | } |
| 100 | |
| 101 | String16::String16(const char16_t* o) |
| 102 | { |
| 103 | size_t len = strlen16(o); |
| 104 | SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t)); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 105 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | if (buf) { |
| 107 | char16_t* str = (char16_t*)buf->data(); |
| 108 | strcpy16(str, o); |
| 109 | mString = str; |
| 110 | return; |
| 111 | } |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 112 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | mString = getEmptyString(); |
| 114 | } |
| 115 | |
| 116 | String16::String16(const char16_t* o, size_t len) |
| 117 | { |
| 118 | SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t)); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 119 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | if (buf) { |
| 121 | char16_t* str = (char16_t*)buf->data(); |
| 122 | memcpy(str, o, len*sizeof(char16_t)); |
| 123 | str[len] = 0; |
| 124 | mString = str; |
| 125 | return; |
| 126 | } |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 127 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | mString = getEmptyString(); |
| 129 | } |
| 130 | |
| 131 | String16::String16(const String8& o) |
| 132 | : mString(allocFromUTF8(o.string(), o.size())) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | String16::String16(const char* o) |
| 137 | : mString(allocFromUTF8(o, strlen(o))) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | String16::String16(const char* o, size_t len) |
| 142 | : mString(allocFromUTF8(o, len)) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | String16::~String16() |
| 147 | { |
| 148 | SharedBuffer::bufferFromData(mString)->release(); |
| 149 | } |
| 150 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 151 | size_t String16::size() const |
| 152 | { |
| 153 | return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1; |
| 154 | } |
| 155 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | void String16::setTo(const String16& other) |
| 157 | { |
| 158 | SharedBuffer::bufferFromData(other.mString)->acquire(); |
| 159 | SharedBuffer::bufferFromData(mString)->release(); |
| 160 | mString = other.mString; |
| 161 | } |
| 162 | |
| 163 | status_t String16::setTo(const String16& other, size_t len, size_t begin) |
| 164 | { |
| 165 | const size_t N = other.size(); |
| 166 | if (begin >= N) { |
| 167 | SharedBuffer::bufferFromData(mString)->release(); |
| 168 | mString = getEmptyString(); |
| 169 | return NO_ERROR; |
| 170 | } |
| 171 | if ((begin+len) > N) len = N-begin; |
| 172 | if (begin == 0 && len == N) { |
| 173 | setTo(other); |
| 174 | return NO_ERROR; |
| 175 | } |
| 176 | |
| 177 | if (&other == this) { |
| 178 | LOG_ALWAYS_FATAL("Not implemented"); |
| 179 | } |
| 180 | |
| 181 | return setTo(other.string()+begin, len); |
| 182 | } |
| 183 | |
| 184 | status_t String16::setTo(const char16_t* other) |
| 185 | { |
| 186 | return setTo(other, strlen16(other)); |
| 187 | } |
| 188 | |
| 189 | status_t String16::setTo(const char16_t* other, size_t len) |
| 190 | { |
| 191 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 192 | ->editResize((len+1)*sizeof(char16_t)); |
| 193 | if (buf) { |
| 194 | char16_t* str = (char16_t*)buf->data(); |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 195 | memmove(str, other, len*sizeof(char16_t)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | str[len] = 0; |
| 197 | mString = str; |
| 198 | return NO_ERROR; |
| 199 | } |
| 200 | return NO_MEMORY; |
| 201 | } |
| 202 | |
| 203 | status_t String16::append(const String16& other) |
| 204 | { |
| 205 | const size_t myLen = size(); |
| 206 | const size_t otherLen = other.size(); |
| 207 | if (myLen == 0) { |
| 208 | setTo(other); |
| 209 | return NO_ERROR; |
| 210 | } else if (otherLen == 0) { |
| 211 | return NO_ERROR; |
| 212 | } |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 213 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 215 | ->editResize((myLen+otherLen+1)*sizeof(char16_t)); |
| 216 | if (buf) { |
| 217 | char16_t* str = (char16_t*)buf->data(); |
| 218 | memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t)); |
| 219 | mString = str; |
| 220 | return NO_ERROR; |
| 221 | } |
| 222 | return NO_MEMORY; |
| 223 | } |
| 224 | |
| 225 | status_t String16::append(const char16_t* chrs, size_t otherLen) |
| 226 | { |
| 227 | const size_t myLen = size(); |
| 228 | if (myLen == 0) { |
| 229 | setTo(chrs, otherLen); |
| 230 | return NO_ERROR; |
| 231 | } else if (otherLen == 0) { |
| 232 | return NO_ERROR; |
| 233 | } |
Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 234 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 236 | ->editResize((myLen+otherLen+1)*sizeof(char16_t)); |
| 237 | if (buf) { |
| 238 | char16_t* str = (char16_t*)buf->data(); |
| 239 | memcpy(str+myLen, chrs, otherLen*sizeof(char16_t)); |
| 240 | str[myLen+otherLen] = 0; |
| 241 | mString = str; |
| 242 | return NO_ERROR; |
| 243 | } |
| 244 | return NO_MEMORY; |
| 245 | } |
| 246 | |
| 247 | status_t String16::insert(size_t pos, const char16_t* chrs) |
| 248 | { |
| 249 | return insert(pos, chrs, strlen16(chrs)); |
| 250 | } |
| 251 | |
| 252 | status_t String16::insert(size_t pos, const char16_t* chrs, size_t len) |
| 253 | { |
| 254 | const size_t myLen = size(); |
| 255 | if (myLen == 0) { |
| 256 | return setTo(chrs, len); |
| 257 | return NO_ERROR; |
| 258 | } else if (len == 0) { |
| 259 | return NO_ERROR; |
| 260 | } |
| 261 | |
| 262 | if (pos > myLen) pos = myLen; |
| 263 | |
| 264 | #if 0 |
| 265 | printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n", |
| 266 | String8(*this).string(), pos, |
| 267 | len, myLen, String8(chrs, len).string()); |
| 268 | #endif |
| 269 | |
| 270 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 271 | ->editResize((myLen+len+1)*sizeof(char16_t)); |
| 272 | if (buf) { |
| 273 | char16_t* str = (char16_t*)buf->data(); |
| 274 | if (pos < myLen) { |
| 275 | memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t)); |
| 276 | } |
| 277 | memcpy(str+pos, chrs, len*sizeof(char16_t)); |
| 278 | str[myLen+len] = 0; |
| 279 | mString = str; |
| 280 | #if 0 |
| 281 | printf("Result (%d chrs): %s\n", size(), String8(*this).string()); |
| 282 | #endif |
| 283 | return NO_ERROR; |
| 284 | } |
| 285 | return NO_MEMORY; |
| 286 | } |
| 287 | |
| 288 | ssize_t String16::findFirst(char16_t c) const |
| 289 | { |
| 290 | const char16_t* str = string(); |
| 291 | const char16_t* p = str; |
| 292 | const char16_t* e = p + size(); |
| 293 | while (p < e) { |
| 294 | if (*p == c) { |
| 295 | return p-str; |
| 296 | } |
| 297 | p++; |
| 298 | } |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | ssize_t String16::findLast(char16_t c) const |
| 303 | { |
| 304 | const char16_t* str = string(); |
| 305 | const char16_t* p = str; |
| 306 | const char16_t* e = p + size(); |
| 307 | while (p < e) { |
| 308 | e--; |
| 309 | if (*e == c) { |
| 310 | return e-str; |
| 311 | } |
| 312 | } |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | bool String16::startsWith(const String16& prefix) const |
| 317 | { |
| 318 | const size_t ps = prefix.size(); |
| 319 | if (ps > size()) return false; |
| 320 | return strzcmp16(mString, ps, prefix.string(), ps) == 0; |
| 321 | } |
| 322 | |
| 323 | bool String16::startsWith(const char16_t* prefix) const |
| 324 | { |
| 325 | const size_t ps = strlen16(prefix); |
| 326 | if (ps > size()) return false; |
| 327 | return strncmp16(mString, prefix, ps) == 0; |
| 328 | } |
| 329 | |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 330 | bool String16::contains(const char16_t* chrs) const |
| 331 | { |
| 332 | return strstr16(mString, chrs) != nullptr; |
| 333 | } |
| 334 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | status_t String16::makeLower() |
| 336 | { |
| 337 | const size_t N = size(); |
| 338 | const char16_t* str = string(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame^] | 339 | char16_t* edit = nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | for (size_t i=0; i<N; i++) { |
| 341 | const char16_t v = str[i]; |
| 342 | if (v >= 'A' && v <= 'Z') { |
| 343 | if (!edit) { |
| 344 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit(); |
| 345 | if (!buf) { |
| 346 | return NO_MEMORY; |
| 347 | } |
| 348 | edit = (char16_t*)buf->data(); |
| 349 | mString = str = edit; |
| 350 | } |
| 351 | edit[i] = tolower((char)v); |
| 352 | } |
| 353 | } |
| 354 | return NO_ERROR; |
| 355 | } |
| 356 | |
| 357 | status_t String16::replaceAll(char16_t replaceThis, char16_t withThis) |
| 358 | { |
| 359 | const size_t N = size(); |
| 360 | const char16_t* str = string(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame^] | 361 | char16_t* edit = nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | for (size_t i=0; i<N; i++) { |
| 363 | if (str[i] == replaceThis) { |
| 364 | if (!edit) { |
| 365 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit(); |
| 366 | if (!buf) { |
| 367 | return NO_MEMORY; |
| 368 | } |
| 369 | edit = (char16_t*)buf->data(); |
| 370 | mString = str = edit; |
| 371 | } |
| 372 | edit[i] = withThis; |
| 373 | } |
| 374 | } |
| 375 | return NO_ERROR; |
| 376 | } |
| 377 | |
| 378 | status_t String16::remove(size_t len, size_t begin) |
| 379 | { |
| 380 | const size_t N = size(); |
| 381 | if (begin >= N) { |
| 382 | SharedBuffer::bufferFromData(mString)->release(); |
| 383 | mString = getEmptyString(); |
| 384 | return NO_ERROR; |
| 385 | } |
| 386 | if ((begin+len) > N) len = N-begin; |
| 387 | if (begin == 0 && len == N) { |
| 388 | return NO_ERROR; |
| 389 | } |
| 390 | |
| 391 | if (begin > 0) { |
| 392 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 393 | ->editResize((N+1)*sizeof(char16_t)); |
| 394 | if (!buf) { |
| 395 | return NO_MEMORY; |
| 396 | } |
| 397 | char16_t* str = (char16_t*)buf->data(); |
| 398 | memmove(str, str+begin, (N-begin+1)*sizeof(char16_t)); |
| 399 | mString = str; |
| 400 | } |
| 401 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 402 | ->editResize((len+1)*sizeof(char16_t)); |
| 403 | if (buf) { |
| 404 | char16_t* str = (char16_t*)buf->data(); |
| 405 | str[len] = 0; |
| 406 | mString = str; |
| 407 | return NO_ERROR; |
| 408 | } |
| 409 | return NO_MEMORY; |
| 410 | } |
| 411 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | }; // namespace android |