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