Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Marco Nelissen | 89b2a0a | 2016-05-03 11:10:42 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AString" |
| 18 | #include <utils/Log.h> |
| 19 | |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
Lajos Molnar | bcf0856 | 2014-04-04 18:09:35 -0700 | [diff] [blame] | 26 | #include <utils/String8.h> |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 27 | #include "ADebug.h" |
| 28 | #include "AString.h" |
| 29 | |
Colin Cross | f8b5fe2 | 2021-12-15 14:59:00 -0800 | [diff] [blame] | 30 | #if defined(__ANDROID__) && !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__) |
S Vasudev Prasad | 08ce0dc | 2020-05-05 11:39:34 +0530 | [diff] [blame] | 31 | #include <binder/Parcel.h> |
| 32 | #endif |
| 33 | |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | // static |
George Burgess IV | 3098ae5 | 2018-05-15 13:15:20 -0700 | [diff] [blame] | 37 | constexpr const char *AString::kEmptyString; |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 38 | |
| 39 | AString::AString() |
| 40 | : mData((char *)kEmptyString), |
| 41 | mSize(0), |
| 42 | mAllocSize(1) { |
| 43 | } |
| 44 | |
| 45 | AString::AString(const char *s) |
| 46 | : mData(NULL), |
| 47 | mSize(0), |
| 48 | mAllocSize(1) { |
Marco Nelissen | 89b2a0a | 2016-05-03 11:10:42 -0700 | [diff] [blame] | 49 | if (!s) { |
| 50 | ALOGW("ctor got NULL, using empty string instead"); |
| 51 | clear(); |
| 52 | } else { |
| 53 | setTo(s); |
| 54 | } |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | AString::AString(const char *s, size_t size) |
| 58 | : mData(NULL), |
| 59 | mSize(0), |
| 60 | mAllocSize(1) { |
Marco Nelissen | 89b2a0a | 2016-05-03 11:10:42 -0700 | [diff] [blame] | 61 | if (!s) { |
| 62 | ALOGW("ctor got NULL, using empty string instead"); |
| 63 | clear(); |
| 64 | } else { |
| 65 | setTo(s, size); |
| 66 | } |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Lajos Molnar | bcf0856 | 2014-04-04 18:09:35 -0700 | [diff] [blame] | 69 | AString::AString(const String8 &from) |
| 70 | : mData(NULL), |
| 71 | mSize(0), |
| 72 | mAllocSize(1) { |
Tomasz Wasilczyk | 03fc55f | 2023-08-11 17:05:05 +0000 | [diff] [blame] | 73 | setTo(from.c_str(), from.length()); |
Lajos Molnar | bcf0856 | 2014-04-04 18:09:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 76 | AString::AString(const AString &from) |
| 77 | : mData(NULL), |
| 78 | mSize(0), |
| 79 | mAllocSize(1) { |
| 80 | setTo(from, 0, from.size()); |
| 81 | } |
| 82 | |
| 83 | AString::AString(const AString &from, size_t offset, size_t n) |
| 84 | : mData(NULL), |
| 85 | mSize(0), |
| 86 | mAllocSize(1) { |
| 87 | setTo(from, offset, n); |
| 88 | } |
| 89 | |
| 90 | AString::~AString() { |
| 91 | clear(); |
| 92 | } |
| 93 | |
| 94 | AString &AString::operator=(const AString &from) { |
| 95 | if (&from != this) { |
| 96 | setTo(from, 0, from.size()); |
| 97 | } |
| 98 | |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | size_t AString::size() const { |
| 103 | return mSize; |
| 104 | } |
| 105 | |
| 106 | const char *AString::c_str() const { |
| 107 | return mData; |
| 108 | } |
| 109 | |
| 110 | bool AString::empty() const { |
| 111 | return mSize == 0; |
| 112 | } |
| 113 | |
| 114 | void AString::setTo(const char *s) { |
| 115 | setTo(s, strlen(s)); |
| 116 | } |
| 117 | |
| 118 | void AString::setTo(const char *s, size_t size) { |
| 119 | clear(); |
| 120 | append(s, size); |
| 121 | } |
| 122 | |
| 123 | void AString::setTo(const AString &from, size_t offset, size_t n) { |
| 124 | CHECK(&from != this); |
| 125 | |
| 126 | clear(); |
| 127 | setTo(from.mData + offset, n); |
| 128 | } |
| 129 | |
| 130 | void AString::clear() { |
Chih-Hung Hsieh | b37668e | 2018-08-30 16:22:52 -0700 | [diff] [blame] | 131 | if (mData != kEmptyString) { |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 132 | free(mData); |
Chih-Hung Hsieh | b37668e | 2018-08-30 16:22:52 -0700 | [diff] [blame] | 133 | mData = (char *)kEmptyString; |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 134 | } |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 135 | mSize = 0; |
| 136 | mAllocSize = 1; |
| 137 | } |
| 138 | |
| 139 | size_t AString::hash() const { |
| 140 | size_t x = 0; |
| 141 | for (size_t i = 0; i < mSize; ++i) { |
| 142 | x = (x * 31) + mData[i]; |
| 143 | } |
| 144 | |
| 145 | return x; |
| 146 | } |
| 147 | |
| 148 | bool AString::operator==(const AString &other) const { |
| 149 | return mSize == other.mSize && !memcmp(mData, other.mData, mSize); |
| 150 | } |
| 151 | |
| 152 | void AString::trim() { |
| 153 | makeMutable(); |
| 154 | |
| 155 | size_t i = 0; |
| 156 | while (i < mSize && isspace(mData[i])) { |
| 157 | ++i; |
| 158 | } |
| 159 | |
| 160 | size_t j = mSize; |
| 161 | while (j > i && isspace(mData[j - 1])) { |
| 162 | --j; |
| 163 | } |
| 164 | |
| 165 | memmove(mData, &mData[i], j - i); |
| 166 | mSize = j - i; |
| 167 | mData[mSize] = '\0'; |
| 168 | } |
| 169 | |
| 170 | void AString::erase(size_t start, size_t n) { |
| 171 | CHECK_LT(start, mSize); |
| 172 | CHECK_LE(start + n, mSize); |
| 173 | |
| 174 | makeMutable(); |
| 175 | |
| 176 | memmove(&mData[start], &mData[start + n], mSize - start - n); |
| 177 | mSize -= n; |
| 178 | mData[mSize] = '\0'; |
| 179 | } |
| 180 | |
| 181 | void AString::makeMutable() { |
| 182 | if (mData == kEmptyString) { |
| 183 | mData = strdup(kEmptyString); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void AString::append(const char *s) { |
| 188 | append(s, strlen(s)); |
| 189 | } |
| 190 | |
| 191 | void AString::append(const char *s, size_t size) { |
| 192 | makeMutable(); |
| 193 | |
| 194 | if (mSize + size + 1 > mAllocSize) { |
| 195 | mAllocSize = (mAllocSize + size + 31) & -32; |
| 196 | mData = (char *)realloc(mData, mAllocSize); |
| 197 | CHECK(mData != NULL); |
| 198 | } |
| 199 | |
| 200 | memcpy(&mData[mSize], s, size); |
| 201 | mSize += size; |
| 202 | mData[mSize] = '\0'; |
| 203 | } |
| 204 | |
| 205 | void AString::append(const AString &from) { |
| 206 | append(from.c_str(), from.size()); |
| 207 | } |
| 208 | |
| 209 | void AString::append(const AString &from, size_t offset, size_t n) { |
| 210 | append(from.c_str() + offset, n); |
| 211 | } |
| 212 | |
| 213 | void AString::append(int x) { |
| 214 | char s[16]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 215 | int result = snprintf(s, sizeof(s), "%d", x); |
| 216 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 217 | append(s); |
| 218 | } |
| 219 | |
| 220 | void AString::append(unsigned x) { |
| 221 | char s[16]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 222 | int result = snprintf(s, sizeof(s), "%u", x); |
| 223 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 224 | append(s); |
| 225 | } |
| 226 | |
| 227 | void AString::append(long x) { |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 228 | char s[32]; |
| 229 | int result = snprintf(s, sizeof(s), "%ld", x); |
| 230 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 231 | append(s); |
| 232 | } |
| 233 | |
| 234 | void AString::append(unsigned long x) { |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 235 | char s[32]; |
| 236 | int result = snprintf(s, sizeof(s), "%lu", x); |
| 237 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 238 | append(s); |
| 239 | } |
| 240 | |
| 241 | void AString::append(long long x) { |
| 242 | char s[32]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 243 | int result = snprintf(s, sizeof(s), "%lld", x); |
| 244 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 245 | append(s); |
| 246 | } |
| 247 | |
| 248 | void AString::append(unsigned long long x) { |
| 249 | char s[32]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 250 | int result = snprintf(s, sizeof(s), "%llu", x); |
| 251 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 252 | append(s); |
| 253 | } |
| 254 | |
| 255 | void AString::append(float x) { |
| 256 | char s[16]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 257 | int result = snprintf(s, sizeof(s), "%f", x); |
| 258 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 259 | append(s); |
| 260 | } |
| 261 | |
| 262 | void AString::append(double x) { |
| 263 | char s[16]; |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 264 | int result = snprintf(s, sizeof(s), "%f", x); |
| 265 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 266 | append(s); |
| 267 | } |
| 268 | |
| 269 | void AString::append(void *x) { |
Marcus Oakland | ef80764 | 2014-03-25 17:32:00 +0000 | [diff] [blame] | 270 | char s[32]; |
| 271 | int result = snprintf(s, sizeof(s), "%p", x); |
| 272 | CHECK((result > 0) && ((size_t) result) < sizeof(s)); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 273 | append(s); |
| 274 | } |
| 275 | |
| 276 | ssize_t AString::find(const char *substring, size_t start) const { |
| 277 | CHECK_LE(start, size()); |
| 278 | |
| 279 | const char *match = strstr(mData + start, substring); |
| 280 | |
| 281 | if (match == NULL) { |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | return match - mData; |
| 286 | } |
| 287 | |
| 288 | void AString::insert(const AString &from, size_t insertionPos) { |
| 289 | insert(from.c_str(), from.size(), insertionPos); |
| 290 | } |
| 291 | |
| 292 | void AString::insert(const char *from, size_t size, size_t insertionPos) { |
| 293 | CHECK_GE(insertionPos, 0u); |
| 294 | CHECK_LE(insertionPos, mSize); |
| 295 | |
| 296 | makeMutable(); |
| 297 | |
| 298 | if (mSize + size + 1 > mAllocSize) { |
| 299 | mAllocSize = (mAllocSize + size + 31) & -32; |
| 300 | mData = (char *)realloc(mData, mAllocSize); |
| 301 | CHECK(mData != NULL); |
| 302 | } |
| 303 | |
| 304 | memmove(&mData[insertionPos + size], |
| 305 | &mData[insertionPos], mSize - insertionPos + 1); |
| 306 | |
| 307 | memcpy(&mData[insertionPos], from, size); |
| 308 | |
| 309 | mSize += size; |
| 310 | } |
| 311 | |
| 312 | bool AString::operator<(const AString &other) const { |
| 313 | return compare(other) < 0; |
| 314 | } |
| 315 | |
| 316 | bool AString::operator>(const AString &other) const { |
| 317 | return compare(other) > 0; |
| 318 | } |
| 319 | |
| 320 | int AString::compare(const AString &other) const { |
| 321 | return strcmp(mData, other.mData); |
| 322 | } |
| 323 | |
Lajos Molnar | 8accee4 | 2014-08-06 11:32:00 -0700 | [diff] [blame] | 324 | int AString::compareIgnoreCase(const AString &other) const { |
| 325 | return strcasecmp(mData, other.mData); |
| 326 | } |
| 327 | |
| 328 | bool AString::equalsIgnoreCase(const AString &other) const { |
| 329 | return compareIgnoreCase(other) == 0; |
| 330 | } |
| 331 | |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 332 | void AString::tolower() { |
| 333 | makeMutable(); |
| 334 | |
| 335 | for (size_t i = 0; i < mSize; ++i) { |
| 336 | mData[i] = ::tolower(mData[i]); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | bool AString::startsWith(const char *prefix) const { |
| 341 | return !strncmp(mData, prefix, strlen(prefix)); |
| 342 | } |
| 343 | |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 344 | bool AString::endsWith(const char *suffix) const { |
| 345 | size_t suffixLen = strlen(suffix); |
| 346 | |
| 347 | if (mSize < suffixLen) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return !strcmp(mData + mSize - suffixLen, suffix); |
| 352 | } |
| 353 | |
Lajos Molnar | 3c1da72 | 2014-06-18 12:29:28 -0700 | [diff] [blame] | 354 | bool AString::startsWithIgnoreCase(const char *prefix) const { |
| 355 | return !strncasecmp(mData, prefix, strlen(prefix)); |
| 356 | } |
| 357 | |
| 358 | bool AString::endsWithIgnoreCase(const char *suffix) const { |
| 359 | size_t suffixLen = strlen(suffix); |
| 360 | |
| 361 | if (mSize < suffixLen) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | return !strcasecmp(mData + mSize - suffixLen, suffix); |
| 366 | } |
| 367 | |
Colin Cross | f8b5fe2 | 2021-12-15 14:59:00 -0800 | [diff] [blame] | 368 | #if defined(__ANDROID__) && !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__) |
Lajos Molnar | 8accee4 | 2014-08-06 11:32:00 -0700 | [diff] [blame] | 369 | // static |
| 370 | AString AString::FromParcel(const Parcel &parcel) { |
| 371 | size_t size = static_cast<size_t>(parcel.readInt32()); |
| 372 | return AString(static_cast<const char *>(parcel.readInplace(size)), size); |
| 373 | } |
| 374 | |
| 375 | status_t AString::writeToParcel(Parcel *parcel) const { |
Sasha Levitskiy | 55971df | 2014-08-08 09:47:37 -0700 | [diff] [blame] | 376 | CHECK_LE(mSize, static_cast<size_t>(INT32_MAX)); |
Lajos Molnar | 8accee4 | 2014-08-06 11:32:00 -0700 | [diff] [blame] | 377 | status_t err = parcel->writeInt32(mSize); |
| 378 | if (err == OK) { |
| 379 | err = parcel->write(mData, mSize); |
| 380 | } |
| 381 | return err; |
| 382 | } |
Colin Cross | f8b5fe2 | 2021-12-15 14:59:00 -0800 | [diff] [blame] | 383 | #endif // defined(__ANDROID__) && !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__) |
Lajos Molnar | 8accee4 | 2014-08-06 11:32:00 -0700 | [diff] [blame] | 384 | |
Elliott Hughes | a1e8944 | 2015-02-04 11:54:28 -0800 | [diff] [blame] | 385 | AString AStringPrintf(const char *format, ...) { |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 386 | va_list ap; |
| 387 | va_start(ap, format); |
| 388 | |
| 389 | char *buffer; |
S Vasudev Prasad | c2f146f | 2020-04-21 14:41:39 +0530 | [diff] [blame] | 390 | int bufferSize = vasprintf(&buffer, format, ap); |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 391 | |
| 392 | va_end(ap); |
| 393 | |
S Vasudev Prasad | c2f146f | 2020-04-21 14:41:39 +0530 | [diff] [blame] | 394 | if(bufferSize < 0) { |
| 395 | return AString(); |
| 396 | } |
| 397 | |
Andreas Huber | 7296123 | 2010-06-07 10:18:57 -0700 | [diff] [blame] | 398 | AString result(buffer); |
| 399 | |
| 400 | free(buffer); |
| 401 | buffer = NULL; |
| 402 | |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | } // namespace android |
| 407 | |