| 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 |  | 
| Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 93 | String16::String16(StaticLinkage) | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 94 | : mString(nullptr) | 
| Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 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 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 100 | SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t))); | 
|  | 101 | char16_t* data = static_cast<char16_t*>(buf->data()); | 
| Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 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 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 109 | acquire(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 118 | String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {} | 
| Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 119 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 120 | 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] | 121 |  | 
|  | 122 | String16::String16(const String8& o) | 
|  | 123 | : mString(allocFromUTF8(o.string(), o.size())) | 
|  | 124 | { | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | String16::String16(const char* o) | 
|  | 128 | : mString(allocFromUTF8(o, strlen(o))) | 
|  | 129 | { | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | String16::String16(const char* o, size_t len) | 
|  | 133 | : mString(allocFromUTF8(o, len)) | 
|  | 134 | { | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | String16::~String16() | 
|  | 138 | { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 139 | release(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |  | 
|  | 202 | status_t String16::append(const String16& other) | 
|  | 203 | { | 
|  | 204 | const size_t myLen = size(); | 
|  | 205 | const size_t otherLen = other.size(); | 
|  | 206 | if (myLen == 0) { | 
|  | 207 | setTo(other); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 208 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } else if (otherLen == 0) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 210 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } | 
| Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 212 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 213 | if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) { | 
|  | 214 | android_errorWriteLog(0x534e4554, "73826242"); | 
|  | 215 | abort(); | 
|  | 216 | } | 
|  | 217 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 218 | SharedBuffer* buf = | 
|  | 219 | static_cast<SharedBuffer*>(editResize((myLen + otherLen + 1) * sizeof(char16_t))); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | if (buf) { | 
|  | 221 | char16_t* str = (char16_t*)buf->data(); | 
|  | 222 | memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t)); | 
|  | 223 | mString = str; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 224 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } | 
|  | 226 | return NO_MEMORY; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | status_t String16::append(const char16_t* chrs, size_t otherLen) | 
|  | 230 | { | 
|  | 231 | const size_t myLen = size(); | 
|  | 232 | if (myLen == 0) { | 
|  | 233 | setTo(chrs, otherLen); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 234 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } else if (otherLen == 0) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 236 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } | 
| Samuel Tan | f9d16ef | 2016-02-16 15:17:10 -0800 | [diff] [blame] | 238 |  | 
| Steven Moreland | 977f72f | 2018-03-01 11:03:04 -0800 | [diff] [blame] | 239 | if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) { | 
|  | 240 | android_errorWriteLog(0x534e4554, "73826242"); | 
|  | 241 | abort(); | 
|  | 242 | } | 
|  | 243 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 244 | SharedBuffer* buf = | 
|  | 245 | static_cast<SharedBuffer*>(editResize((myLen + otherLen + 1) * sizeof(char16_t))); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | if (buf) { | 
|  | 247 | char16_t* str = (char16_t*)buf->data(); | 
|  | 248 | memcpy(str+myLen, chrs, otherLen*sizeof(char16_t)); | 
|  | 249 | str[myLen+otherLen] = 0; | 
|  | 250 | mString = str; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 251 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } | 
|  | 253 | return NO_MEMORY; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | status_t String16::insert(size_t pos, const char16_t* chrs) | 
|  | 257 | { | 
|  | 258 | return insert(pos, chrs, strlen16(chrs)); | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | status_t String16::insert(size_t pos, const char16_t* chrs, size_t len) | 
|  | 262 | { | 
|  | 263 | const size_t myLen = size(); | 
|  | 264 | if (myLen == 0) { | 
|  | 265 | return setTo(chrs, len); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 266 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } else if (len == 0) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 268 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } | 
|  | 270 |  | 
|  | 271 | if (pos > myLen) pos = myLen; | 
|  | 272 |  | 
|  | 273 | #if 0 | 
|  | 274 | printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n", | 
|  | 275 | String8(*this).string(), pos, | 
|  | 276 | len, myLen, String8(chrs, len).string()); | 
|  | 277 | #endif | 
|  | 278 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 279 | SharedBuffer* buf = | 
|  | 280 | static_cast<SharedBuffer*>(editResize((myLen + len + 1) * sizeof(char16_t))); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | if (buf) { | 
|  | 282 | char16_t* str = (char16_t*)buf->data(); | 
|  | 283 | if (pos < myLen) { | 
|  | 284 | memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t)); | 
|  | 285 | } | 
|  | 286 | memcpy(str+pos, chrs, len*sizeof(char16_t)); | 
|  | 287 | str[myLen+len] = 0; | 
|  | 288 | mString = str; | 
|  | 289 | #if 0 | 
|  | 290 | printf("Result (%d chrs): %s\n", size(), String8(*this).string()); | 
|  | 291 | #endif | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 292 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } | 
|  | 294 | return NO_MEMORY; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | ssize_t String16::findFirst(char16_t c) const | 
|  | 298 | { | 
|  | 299 | const char16_t* str = string(); | 
|  | 300 | const char16_t* p = str; | 
|  | 301 | const char16_t* e = p + size(); | 
|  | 302 | while (p < e) { | 
|  | 303 | if (*p == c) { | 
|  | 304 | return p-str; | 
|  | 305 | } | 
|  | 306 | p++; | 
|  | 307 | } | 
|  | 308 | return -1; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | ssize_t String16::findLast(char16_t c) const | 
|  | 312 | { | 
|  | 313 | const char16_t* str = string(); | 
|  | 314 | const char16_t* p = str; | 
|  | 315 | const char16_t* e = p + size(); | 
|  | 316 | while (p < e) { | 
|  | 317 | e--; | 
|  | 318 | if (*e == c) { | 
|  | 319 | return e-str; | 
|  | 320 | } | 
|  | 321 | } | 
|  | 322 | return -1; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | bool String16::startsWith(const String16& prefix) const | 
|  | 326 | { | 
|  | 327 | const size_t ps = prefix.size(); | 
|  | 328 | if (ps > size()) return false; | 
|  | 329 | return strzcmp16(mString, ps, prefix.string(), ps) == 0; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | bool String16::startsWith(const char16_t* prefix) const | 
|  | 333 | { | 
|  | 334 | const size_t ps = strlen16(prefix); | 
|  | 335 | if (ps > size()) return false; | 
|  | 336 | return strncmp16(mString, prefix, ps) == 0; | 
|  | 337 | } | 
|  | 338 |  | 
| Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 339 | bool String16::contains(const char16_t* chrs) const | 
|  | 340 | { | 
|  | 341 | return strstr16(mString, chrs) != nullptr; | 
|  | 342 | } | 
|  | 343 |  | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 344 | void* String16::edit() { | 
|  | 345 | SharedBuffer* buf; | 
|  | 346 | if (isStaticString()) { | 
|  | 347 | buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t))); | 
|  | 348 | if (buf) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 349 | memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t)); | 
|  | 350 | } | 
|  | 351 | } else { | 
|  | 352 | buf = SharedBuffer::bufferFromData(mString)->edit(); | 
|  | 353 | buf->mClientMetadata = kIsSharedBufferAllocated; | 
|  | 354 | } | 
|  | 355 | return buf; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | void* String16::editResize(size_t newSize) { | 
|  | 359 | SharedBuffer* buf; | 
|  | 360 | if (isStaticString()) { | 
|  | 361 | size_t copySize = (size() + 1) * sizeof(char16_t); | 
|  | 362 | if (newSize < copySize) { | 
|  | 363 | copySize = newSize; | 
|  | 364 | } | 
|  | 365 | buf = static_cast<SharedBuffer*>(alloc(newSize)); | 
|  | 366 | if (buf) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 367 | memcpy(buf->data(), mString, copySize); | 
|  | 368 | } | 
|  | 369 | } else { | 
|  | 370 | buf = SharedBuffer::bufferFromData(mString)->editResize(newSize); | 
|  | 371 | buf->mClientMetadata = kIsSharedBufferAllocated; | 
|  | 372 | } | 
|  | 373 | return buf; | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | void String16::acquire() | 
|  | 377 | { | 
|  | 378 | if (!isStaticString()) { | 
|  | 379 | SharedBuffer::bufferFromData(mString)->acquire(); | 
|  | 380 | } | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | void String16::release() | 
|  | 384 | { | 
|  | 385 | if (!isStaticString()) { | 
|  | 386 | SharedBuffer::bufferFromData(mString)->release(); | 
|  | 387 | } | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | bool String16::isStaticString() const { | 
|  | 391 | // See String16.h for notes on the memory layout of String16::StaticData and | 
|  | 392 | // SharedBuffer. | 
|  | 393 | static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4); | 
|  | 394 | const uint32_t* p = reinterpret_cast<const uint32_t*>(mString); | 
|  | 395 | return (*(p - 1) & kIsSharedBufferAllocated) == 0; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | size_t String16::staticStringSize() const { | 
|  | 399 | // See String16.h for notes on the memory layout of String16::StaticData and | 
|  | 400 | // SharedBuffer. | 
|  | 401 | static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4); | 
|  | 402 | const uint32_t* p = reinterpret_cast<const uint32_t*>(mString); | 
|  | 403 | return static_cast<size_t>(*(p - 1)); | 
|  | 404 | } | 
|  | 405 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | status_t String16::makeLower() | 
|  | 407 | { | 
|  | 408 | const size_t N = size(); | 
|  | 409 | const char16_t* str = string(); | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 410 | char16_t* edited = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | for (size_t i=0; i<N; i++) { | 
|  | 412 | const char16_t v = str[i]; | 
|  | 413 | if (v >= 'A' && v <= 'Z') { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 414 | if (!edited) { | 
|  | 415 | SharedBuffer* buf = static_cast<SharedBuffer*>(edit()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | if (!buf) { | 
|  | 417 | return NO_MEMORY; | 
|  | 418 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 419 | edited = (char16_t*)buf->data(); | 
|  | 420 | mString = str = edited; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 422 | edited[i] = tolower((char)v); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } | 
|  | 424 | } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 425 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
|  | 428 | status_t String16::replaceAll(char16_t replaceThis, char16_t withThis) | 
|  | 429 | { | 
|  | 430 | const size_t N = size(); | 
|  | 431 | const char16_t* str = string(); | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 432 | char16_t* edited = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | for (size_t i=0; i<N; i++) { | 
|  | 434 | if (str[i] == replaceThis) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 435 | if (!edited) { | 
|  | 436 | SharedBuffer* buf = static_cast<SharedBuffer*>(edit()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | if (!buf) { | 
|  | 438 | return NO_MEMORY; | 
|  | 439 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 440 | edited = (char16_t*)buf->data(); | 
|  | 441 | mString = str = edited; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 443 | edited[i] = withThis; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } | 
|  | 445 | } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 446 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } | 
|  | 448 |  | 
|  | 449 | status_t String16::remove(size_t len, size_t begin) | 
|  | 450 | { | 
|  | 451 | const size_t N = size(); | 
|  | 452 | if (begin >= N) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 453 | release(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | mString = getEmptyString(); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 455 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } | 
|  | 457 | if ((begin+len) > N) len = N-begin; | 
|  | 458 | if (begin == 0 && len == N) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 459 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } | 
|  | 461 |  | 
|  | 462 | if (begin > 0) { | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 463 | SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((N + 1) * sizeof(char16_t))); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | if (!buf) { | 
|  | 465 | return NO_MEMORY; | 
|  | 466 | } | 
|  | 467 | char16_t* str = (char16_t*)buf->data(); | 
|  | 468 | memmove(str, str+begin, (N-begin+1)*sizeof(char16_t)); | 
|  | 469 | mString = str; | 
|  | 470 | } | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 471 | 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] | 472 | if (buf) { | 
|  | 473 | char16_t* str = (char16_t*)buf->data(); | 
|  | 474 | str[len] = 0; | 
|  | 475 | mString = str; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 476 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } | 
|  | 478 | return NO_MEMORY; | 
|  | 479 | } | 
|  | 480 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | }; // namespace android |