| 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 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 27 | static const StaticString16 emptyString(u""); | 
| Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 28 | static inline char16_t* getEmptyString() { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 29 | return const_cast<char16_t*>(emptyString.string()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
|  | 32 | // --------------------------------------------------------------------------- | 
|  | 33 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 34 | void* String16::alloc(size_t size) | 
|  | 35 | { | 
|  | 36 | SharedBuffer* buf = SharedBuffer::alloc(size); | 
|  | 37 | buf->mClientMetadata = kIsSharedBufferAllocated; | 
|  | 38 | return buf; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | char16_t* String16::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 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 52 | SharedBuffer* buf = static_cast<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 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 69 | char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) { | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 70 | if (u16len >= SIZE_MAX / sizeof(char16_t)) { | 
|  | 71 | android_errorWriteLog(0x534e4554, "73826242"); | 
|  | 72 | abort(); | 
|  | 73 | } | 
|  | 74 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 75 | SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t))); | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 76 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); | 
|  | 77 | if (buf) { | 
|  | 78 | char16_t* str = (char16_t*)buf->data(); | 
|  | 79 | memcpy(str, u16str, u16len * sizeof(char16_t)); | 
|  | 80 | str[u16len] = 0; | 
|  | 81 | return str; | 
|  | 82 | } | 
|  | 83 | return getEmptyString(); | 
|  | 84 | } | 
|  | 85 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | // --------------------------------------------------------------------------- | 
|  | 87 |  | 
|  | 88 | String16::String16() | 
|  | 89 | : mString(getEmptyString()) | 
|  | 90 | { | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | String16::String16(const String16& o) | 
|  | 94 | : mString(o.mString) | 
|  | 95 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 96 | acquire(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 99 | String16::String16(String16&& o) noexcept | 
|  | 100 | : mString(o.mString) | 
|  | 101 | { | 
|  | 102 | o.mString = getEmptyString(); | 
|  | 103 | } | 
|  | 104 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | String16::String16(const String16& o, size_t len, size_t begin) | 
|  | 106 | : mString(getEmptyString()) | 
|  | 107 | { | 
|  | 108 | setTo(o, len, begin); | 
|  | 109 | } | 
|  | 110 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 111 | String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {} | 
| Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 112 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 113 | String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {} | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 |  | 
|  | 115 | String16::String16(const String8& o) | 
|  | 116 | : mString(allocFromUTF8(o.string(), o.size())) | 
|  | 117 | { | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | String16::String16(const char* o) | 
|  | 121 | : mString(allocFromUTF8(o, strlen(o))) | 
|  | 122 | { | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | String16::String16(const char* o, size_t len) | 
|  | 126 | : mString(allocFromUTF8(o, len)) | 
|  | 127 | { | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | String16::~String16() | 
|  | 131 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 132 | release(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 135 | String16& String16::operator=(String16&& other) noexcept { | 
|  | 136 | release(); | 
|  | 137 | mString = other.mString; | 
|  | 138 | other.mString = getEmptyString(); | 
|  | 139 | return *this; | 
|  | 140 | } | 
|  | 141 |  | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 142 | size_t String16::size() const | 
|  | 143 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 144 | if (isStaticString()) { | 
|  | 145 | return staticStringSize(); | 
|  | 146 | } else { | 
|  | 147 | return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1; | 
|  | 148 | } | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | void String16::setTo(const String16& other) | 
|  | 152 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 153 | release(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | mString = other.mString; | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 155 | acquire(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | status_t String16::setTo(const String16& other, size_t len, size_t begin) | 
|  | 159 | { | 
|  | 160 | const size_t N = other.size(); | 
|  | 161 | if (begin >= N) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 162 | release(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | mString = getEmptyString(); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 164 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } | 
|  | 166 | if ((begin+len) > N) len = N-begin; | 
|  | 167 | if (begin == 0 && len == N) { | 
|  | 168 | setTo(other); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 169 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | if (&other == this) { | 
|  | 173 | LOG_ALWAYS_FATAL("Not implemented"); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | return setTo(other.string()+begin, len); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | status_t String16::setTo(const char16_t* other) | 
|  | 180 | { | 
|  | 181 | return setTo(other, strlen16(other)); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | status_t String16::setTo(const char16_t* other, size_t len) | 
|  | 185 | { | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 186 | if (len >= SIZE_MAX / sizeof(char16_t)) { | 
|  | 187 | android_errorWriteLog(0x534e4554, "73826242"); | 
|  | 188 | abort(); | 
|  | 189 | } | 
|  | 190 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 191 | SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t))); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | if (buf) { | 
|  | 193 | char16_t* str = (char16_t*)buf->data(); | 
| The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 194 | memmove(str, other, len*sizeof(char16_t)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | str[len] = 0; | 
|  | 196 | mString = str; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 197 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } | 
|  | 199 | return NO_MEMORY; | 
|  | 200 | } | 
|  | 201 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 202 | status_t String16::append(const String16& other) { | 
|  | 203 | return append(other.string(), other.size()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 206 | status_t String16::append(const char16_t* chrs, size_t otherLen) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | const size_t myLen = size(); | 
| Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 208 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 209 | if (myLen == 0) return setTo(chrs, otherLen); | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 210 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 211 | if (otherLen == 0) return OK; | 
|  | 212 |  | 
|  | 213 | size_t size = myLen; | 
|  | 214 | if (__builtin_add_overflow(size, otherLen, &size) || | 
|  | 215 | __builtin_add_overflow(size, 1, &size) || | 
|  | 216 | __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY; | 
|  | 217 |  | 
|  | 218 | SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size)); | 
|  | 219 | if (!buf) return NO_MEMORY; | 
|  | 220 |  | 
|  | 221 | char16_t* str = static_cast<char16_t*>(buf->data()); | 
|  | 222 | memcpy(str + myLen, chrs, otherLen * sizeof(char16_t)); | 
|  | 223 | str[myLen + otherLen] = 0; | 
|  | 224 | mString = str; | 
|  | 225 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 228 | status_t String16::insert(size_t pos, const char16_t* chrs) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | return insert(pos, chrs, strlen16(chrs)); | 
|  | 230 | } | 
|  | 231 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 232 | status_t String16::insert(size_t pos, const char16_t* chrs, size_t otherLen) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | const size_t myLen = size(); | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 234 |  | 
|  | 235 | if (myLen == 0) return setTo(chrs, otherLen); | 
|  | 236 |  | 
|  | 237 | if (otherLen == 0) return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | if (pos > myLen) pos = myLen; | 
|  | 240 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 241 | size_t size = myLen; | 
|  | 242 | if (__builtin_add_overflow(size, otherLen, &size) || | 
|  | 243 | __builtin_add_overflow(size, 1, &size) || | 
|  | 244 | __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 |  | 
| Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 246 | SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size)); | 
|  | 247 | if (!buf) return NO_MEMORY; | 
|  | 248 |  | 
|  | 249 | char16_t* str = static_cast<char16_t*>(buf->data()); | 
|  | 250 | if (pos < myLen) memmove(str + pos + otherLen, str + pos, (myLen - pos) * sizeof(char16_t)); | 
|  | 251 | memcpy(str + pos, chrs, otherLen * sizeof(char16_t)); | 
|  | 252 | str[myLen + otherLen] = 0; | 
|  | 253 | mString = str; | 
|  | 254 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
|  | 257 | ssize_t String16::findFirst(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 | if (*p == c) { | 
|  | 264 | return p-str; | 
|  | 265 | } | 
|  | 266 | p++; | 
|  | 267 | } | 
|  | 268 | return -1; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | ssize_t String16::findLast(char16_t c) const | 
|  | 272 | { | 
|  | 273 | const char16_t* str = string(); | 
|  | 274 | const char16_t* p = str; | 
|  | 275 | const char16_t* e = p + size(); | 
|  | 276 | while (p < e) { | 
|  | 277 | e--; | 
|  | 278 | if (*e == c) { | 
|  | 279 | return e-str; | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 | return -1; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | bool String16::startsWith(const String16& prefix) const | 
|  | 286 | { | 
|  | 287 | const size_t ps = prefix.size(); | 
|  | 288 | if (ps > size()) return false; | 
|  | 289 | return strzcmp16(mString, ps, prefix.string(), ps) == 0; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | bool String16::startsWith(const char16_t* prefix) const | 
|  | 293 | { | 
|  | 294 | const size_t ps = strlen16(prefix); | 
|  | 295 | if (ps > size()) return false; | 
|  | 296 | return strncmp16(mString, prefix, ps) == 0; | 
|  | 297 | } | 
|  | 298 |  | 
| Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 299 | bool String16::contains(const char16_t* chrs) const | 
|  | 300 | { | 
|  | 301 | return strstr16(mString, chrs) != nullptr; | 
|  | 302 | } | 
|  | 303 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 304 | void* String16::edit() { | 
|  | 305 | SharedBuffer* buf; | 
|  | 306 | if (isStaticString()) { | 
|  | 307 | buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t))); | 
|  | 308 | if (buf) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 309 | memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t)); | 
|  | 310 | } | 
|  | 311 | } else { | 
|  | 312 | buf = SharedBuffer::bufferFromData(mString)->edit(); | 
|  | 313 | buf->mClientMetadata = kIsSharedBufferAllocated; | 
|  | 314 | } | 
|  | 315 | return buf; | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | void* String16::editResize(size_t newSize) { | 
|  | 319 | SharedBuffer* buf; | 
|  | 320 | if (isStaticString()) { | 
|  | 321 | size_t copySize = (size() + 1) * sizeof(char16_t); | 
|  | 322 | if (newSize < copySize) { | 
|  | 323 | copySize = newSize; | 
|  | 324 | } | 
|  | 325 | buf = static_cast<SharedBuffer*>(alloc(newSize)); | 
|  | 326 | if (buf) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 327 | memcpy(buf->data(), mString, copySize); | 
|  | 328 | } | 
|  | 329 | } else { | 
|  | 330 | buf = SharedBuffer::bufferFromData(mString)->editResize(newSize); | 
|  | 331 | buf->mClientMetadata = kIsSharedBufferAllocated; | 
|  | 332 | } | 
|  | 333 | return buf; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | void String16::acquire() | 
|  | 337 | { | 
|  | 338 | if (!isStaticString()) { | 
|  | 339 | SharedBuffer::bufferFromData(mString)->acquire(); | 
|  | 340 | } | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | void String16::release() | 
|  | 344 | { | 
|  | 345 | if (!isStaticString()) { | 
|  | 346 | SharedBuffer::bufferFromData(mString)->release(); | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | bool String16::isStaticString() const { | 
|  | 351 | // See String16.h for notes on the memory layout of String16::StaticData and | 
|  | 352 | // SharedBuffer. | 
|  | 353 | static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4); | 
|  | 354 | const uint32_t* p = reinterpret_cast<const uint32_t*>(mString); | 
|  | 355 | return (*(p - 1) & kIsSharedBufferAllocated) == 0; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | size_t String16::staticStringSize() const { | 
|  | 359 | // See String16.h for notes on the memory layout of String16::StaticData and | 
|  | 360 | // SharedBuffer. | 
|  | 361 | static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4); | 
|  | 362 | const uint32_t* p = reinterpret_cast<const uint32_t*>(mString); | 
|  | 363 | return static_cast<size_t>(*(p - 1)); | 
|  | 364 | } | 
|  | 365 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | status_t String16::replaceAll(char16_t replaceThis, char16_t withThis) | 
|  | 367 | { | 
|  | 368 | const size_t N = size(); | 
|  | 369 | const char16_t* str = string(); | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 370 | char16_t* edited = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | for (size_t i=0; i<N; i++) { | 
|  | 372 | if (str[i] == replaceThis) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 373 | if (!edited) { | 
|  | 374 | SharedBuffer* buf = static_cast<SharedBuffer*>(edit()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | if (!buf) { | 
|  | 376 | return NO_MEMORY; | 
|  | 377 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 378 | edited = (char16_t*)buf->data(); | 
|  | 379 | mString = str = edited; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 381 | edited[i] = withThis; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | } | 
|  | 383 | } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 384 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | }; // namespace android |