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/String8.h> |
| 18 | |
Steven Moreland | 066e625 | 2023-10-07 00:29:44 +0000 | [diff] [blame] | 19 | #include <log/log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <utils/String16.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Steven Moreland | d21cfab | 2017-03-10 08:58:36 -0800 | [diff] [blame] | 22 | #include <ctype.h> |
Elliott Hughes | 29cd071 | 2024-02-09 16:40:02 +0000 | [diff] [blame] | 23 | #include <stdint.h> |
Steven Moreland | d21cfab | 2017-03-10 08:58:36 -0800 | [diff] [blame] | 24 | |
Biswapriyo Nath | 9e25bf0 | 2022-10-21 10:26:01 +0530 | [diff] [blame] | 25 | #include <limits> |
Elliott Hughes | 4b7b4d6 | 2021-04-15 15:18:54 -0700 | [diff] [blame] | 26 | #include <string> |
| 27 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 28 | #include "SharedBuffer.h" |
| 29 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 30 | /* |
| 31 | * Functions outside android is below the namespace android, since they use |
| 32 | * functions and constants in android namespace. |
| 33 | */ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 39 | static inline char* getEmptyString() { |
| 40 | static SharedBuffer* gEmptyStringBuf = [] { |
| 41 | SharedBuffer* buf = SharedBuffer::alloc(1); |
| 42 | char* str = static_cast<char*>(buf->data()); |
| 43 | *str = 0; |
| 44 | return buf; |
| 45 | }(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | gEmptyStringBuf->acquire(); |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 48 | return static_cast<char*>(gEmptyStringBuf->data()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
| 53 | static char* allocFromUTF8(const char* in, size_t len) |
| 54 | { |
| 55 | if (len > 0) { |
Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 56 | if (len == SIZE_MAX) { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 57 | return nullptr; |
Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 58 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | SharedBuffer* buf = SharedBuffer::alloc(len+1); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 60 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | if (buf) { |
| 62 | char* str = (char*)buf->data(); |
| 63 | memcpy(str, in, len); |
| 64 | str[len] = 0; |
| 65 | return str; |
| 66 | } |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 67 | return nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return getEmptyString(); |
| 71 | } |
| 72 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 73 | static char* allocFromUTF16(const char16_t* in, size_t len) |
| 74 | { |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 75 | if (len == 0) return getEmptyString(); |
| 76 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 77 | // Allow for closing '\0' |
| 78 | const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1; |
| 79 | if (resultStrLen < 1) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 80 | return getEmptyString(); |
| 81 | } |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 82 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 83 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 84 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 85 | if (!buf) { |
| 86 | return getEmptyString(); |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 89 | char* resultStr = (char*)buf->data(); |
| 90 | utf16_to_utf8(in, len, resultStr, resultStrLen); |
| 91 | return resultStr; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static char* allocFromUTF32(const char32_t* in, size_t len) |
| 95 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 96 | if (len == 0) { |
| 97 | return getEmptyString(); |
| 98 | } |
| 99 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 100 | const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1; |
| 101 | if (resultStrLen < 1) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 102 | return getEmptyString(); |
| 103 | } |
| 104 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 105 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 106 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 107 | if (!buf) { |
| 108 | return getEmptyString(); |
| 109 | } |
| 110 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 111 | char* resultStr = (char*) buf->data(); |
| 112 | utf32_to_utf8(in, len, resultStr, resultStrLen); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 113 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 114 | return resultStr; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 115 | } |
| 116 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | // --------------------------------------------------------------------------- |
| 118 | |
| 119 | String8::String8() |
| 120 | : mString(getEmptyString()) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | String8::String8(const String8& o) |
| 125 | : mString(o.mString) |
| 126 | { |
| 127 | SharedBuffer::bufferFromData(mString)->acquire(); |
| 128 | } |
| 129 | |
| 130 | String8::String8(const char* o) |
| 131 | : mString(allocFromUTF8(o, strlen(o))) |
| 132 | { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 133 | if (mString == nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | mString = getEmptyString(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | String8::String8(const char* o, size_t len) |
| 139 | : mString(allocFromUTF8(o, len)) |
| 140 | { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 141 | if (mString == nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | mString = getEmptyString(); |
| 143 | } |
| 144 | } |
| 145 | |
Tomasz Wasilczyk | 90af415 | 2023-08-11 02:12:16 +0000 | [diff] [blame] | 146 | String8::String8(const String16& o) : mString(allocFromUTF16(o.c_str(), o.size())) {} |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | |
| 148 | String8::String8(const char16_t* o) |
| 149 | : mString(allocFromUTF16(o, strlen16(o))) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | String8::String8(const char16_t* o, size_t len) |
| 154 | : mString(allocFromUTF16(o, len)) |
| 155 | { |
| 156 | } |
| 157 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 158 | String8::String8(const char32_t* o) |
Elliott Hughes | 4b7b4d6 | 2021-04-15 15:18:54 -0700 | [diff] [blame] | 159 | : mString(allocFromUTF32(o, std::char_traits<char32_t>::length(o))) {} |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 160 | |
| 161 | String8::String8(const char32_t* o, size_t len) |
| 162 | : mString(allocFromUTF32(o, len)) |
| 163 | { |
| 164 | } |
| 165 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | String8::~String8() |
| 167 | { |
| 168 | SharedBuffer::bufferFromData(mString)->release(); |
| 169 | } |
| 170 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 171 | size_t String8::length() const |
| 172 | { |
| 173 | return SharedBuffer::sizeFromData(mString)-1; |
| 174 | } |
| 175 | |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 176 | String8 String8::format(const char* fmt, ...) |
| 177 | { |
| 178 | va_list args; |
| 179 | va_start(args, fmt); |
| 180 | |
| 181 | String8 result(formatV(fmt, args)); |
| 182 | |
| 183 | va_end(args); |
| 184 | return result; |
| 185 | } |
| 186 | |
| 187 | String8 String8::formatV(const char* fmt, va_list args) |
| 188 | { |
| 189 | String8 result; |
| 190 | result.appendFormatV(fmt, args); |
| 191 | return result; |
| 192 | } |
| 193 | |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 194 | void String8::clear() { |
| 195 | SharedBuffer::bufferFromData(mString)->release(); |
| 196 | mString = getEmptyString(); |
| 197 | } |
| 198 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | void String8::setTo(const String8& other) |
| 200 | { |
| 201 | SharedBuffer::bufferFromData(other.mString)->acquire(); |
| 202 | SharedBuffer::bufferFromData(mString)->release(); |
| 203 | mString = other.mString; |
| 204 | } |
| 205 | |
| 206 | status_t String8::setTo(const char* other) |
| 207 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 208 | const char *newString = allocFromUTF8(other, strlen(other)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 210 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 211 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | |
| 213 | mString = getEmptyString(); |
| 214 | return NO_MEMORY; |
| 215 | } |
| 216 | |
| 217 | status_t String8::setTo(const char* other, size_t len) |
| 218 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 219 | const char *newString = allocFromUTF8(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 221 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 222 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
| 224 | mString = getEmptyString(); |
| 225 | return NO_MEMORY; |
| 226 | } |
| 227 | |
| 228 | status_t String8::setTo(const char16_t* other, size_t len) |
| 229 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 230 | const char *newString = allocFromUTF16(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 232 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 233 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
| 235 | mString = getEmptyString(); |
| 236 | return NO_MEMORY; |
| 237 | } |
| 238 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 239 | status_t String8::setTo(const char32_t* other, size_t len) |
| 240 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 241 | const char *newString = allocFromUTF32(other, len); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 242 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 243 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 244 | if (mString) return OK; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 245 | |
| 246 | mString = getEmptyString(); |
| 247 | return NO_MEMORY; |
| 248 | } |
| 249 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | status_t String8::append(const String8& other) |
| 251 | { |
| 252 | const size_t otherLen = other.bytes(); |
| 253 | if (bytes() == 0) { |
| 254 | setTo(other); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 255 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } else if (otherLen == 0) { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 257 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Tomasz Wasilczyk | 90af415 | 2023-08-11 02:12:16 +0000 | [diff] [blame] | 260 | return real_append(other.c_str(), otherLen); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | status_t String8::append(const char* other) |
| 264 | { |
| 265 | return append(other, strlen(other)); |
| 266 | } |
| 267 | |
| 268 | status_t String8::append(const char* other, size_t otherLen) |
| 269 | { |
| 270 | if (bytes() == 0) { |
| 271 | return setTo(other, otherLen); |
| 272 | } else if (otherLen == 0) { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 273 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return real_append(other, otherLen); |
| 277 | } |
| 278 | |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 279 | status_t String8::appendFormat(const char* fmt, ...) |
| 280 | { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 281 | va_list args; |
| 282 | va_start(args, fmt); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 283 | |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 284 | status_t result = appendFormatV(fmt, args); |
| 285 | |
| 286 | va_end(args); |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | status_t String8::appendFormatV(const char* fmt, va_list args) |
| 291 | { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 292 | int n, result = OK; |
Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 293 | va_list tmp_args; |
| 294 | |
| 295 | /* args is undefined after vsnprintf. |
| 296 | * So we need a copy here to avoid the |
| 297 | * second vsnprintf access undefined args. |
| 298 | */ |
| 299 | va_copy(tmp_args, args); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 300 | n = vsnprintf(nullptr, 0, fmt, tmp_args); |
Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 301 | va_end(tmp_args); |
| 302 | |
Steven Moreland | 0f0cb95 | 2020-07-28 21:41:54 +0000 | [diff] [blame] | 303 | if (n < 0) return UNKNOWN_ERROR; |
| 304 | |
| 305 | if (n > 0) { |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 306 | size_t oldLength = length(); |
Andrei Homescu | 1a867dc | 2022-03-29 00:30:34 +0000 | [diff] [blame] | 307 | if (static_cast<size_t>(n) > std::numeric_limits<size_t>::max() - 1 || |
Steven Moreland | 0f0cb95 | 2020-07-28 21:41:54 +0000 | [diff] [blame] | 308 | oldLength > std::numeric_limits<size_t>::max() - n - 1) { |
| 309 | return NO_MEMORY; |
| 310 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 311 | char* buf = lockBuffer(oldLength + n); |
| 312 | if (buf) { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 313 | vsnprintf(buf + oldLength, n + 1, fmt, args); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 314 | } else { |
| 315 | result = NO_MEMORY; |
| 316 | } |
| 317 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 318 | return result; |
| 319 | } |
| 320 | |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 321 | status_t String8::real_append(const char* other, size_t otherLen) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | const size_t myLen = bytes(); |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 323 | |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 324 | SharedBuffer* buf; |
| 325 | size_t newLen; |
| 326 | if (__builtin_add_overflow(myLen, otherLen, &newLen) || |
| 327 | __builtin_add_overflow(newLen, 1, &newLen) || |
| 328 | (buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) { |
| 329 | return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 331 | |
| 332 | char* str = (char*)buf->data(); |
| 333 | mString = str; |
| 334 | str += myLen; |
| 335 | memcpy(str, other, otherLen); |
| 336 | str[otherLen] = '\0'; |
| 337 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | char* String8::lockBuffer(size_t size) |
| 341 | { |
| 342 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 343 | ->editResize(size+1); |
| 344 | if (buf) { |
| 345 | char* str = (char*)buf->data(); |
| 346 | mString = str; |
| 347 | return str; |
| 348 | } |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 349 | return nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void String8::unlockBuffer() |
| 353 | { |
| 354 | unlockBuffer(strlen(mString)); |
| 355 | } |
| 356 | |
| 357 | status_t String8::unlockBuffer(size_t size) |
| 358 | { |
| 359 | if (size != this->size()) { |
| 360 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 361 | ->editResize(size+1); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 362 | if (! buf) { |
| 363 | return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 365 | |
| 366 | char* str = (char*)buf->data(); |
| 367 | str[size] = 0; |
| 368 | mString = str; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 370 | |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 371 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | ssize_t String8::find(const char* other, size_t start) const |
| 375 | { |
| 376 | size_t len = size(); |
| 377 | if (start >= len) { |
| 378 | return -1; |
| 379 | } |
| 380 | const char* s = mString+start; |
| 381 | const char* p = strstr(s, other); |
| 382 | return p ? p-mString : -1; |
| 383 | } |
| 384 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 385 | bool String8::removeAll(const char* other) { |
Eric Miao | c6ce48e | 2023-07-14 16:35:09 -0700 | [diff] [blame] | 386 | ALOG_ASSERT(other, "String8::removeAll() requires a non-NULL string"); |
| 387 | |
| 388 | if (*other == '\0') |
| 389 | return true; |
| 390 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 391 | ssize_t index = find(other); |
| 392 | if (index < 0) return false; |
| 393 | |
| 394 | char* buf = lockBuffer(size()); |
| 395 | if (!buf) return false; // out of memory |
| 396 | |
| 397 | size_t skip = strlen(other); |
| 398 | size_t len = size(); |
| 399 | size_t tail = index; |
| 400 | while (size_t(index) < len) { |
| 401 | ssize_t next = find(other, index + skip); |
| 402 | if (next < 0) { |
| 403 | next = len; |
| 404 | } |
| 405 | |
Andreas Gampe | dd060f0 | 2014-11-13 15:50:17 -0800 | [diff] [blame] | 406 | memmove(buf + tail, buf + index + skip, next - index - skip); |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 407 | tail += next - index - skip; |
| 408 | index = next; |
| 409 | } |
| 410 | unlockBuffer(tail); |
| 411 | return true; |
| 412 | } |
| 413 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | void String8::toLower() |
| 415 | { |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 416 | const size_t length = size(); |
| 417 | if (length == 0) return; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 419 | char* buf = lockBuffer(length); |
| 420 | for (size_t i = length; i > 0; --i) { |
Steven Moreland | fdbc565 | 2020-07-13 23:31:45 +0000 | [diff] [blame] | 421 | *buf = static_cast<char>(tolower(*buf)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | buf++; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 424 | unlockBuffer(length); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | } |
| 426 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | // --------------------------------------------------------------------------- |
| 428 | // Path functions |
| 429 | |
Steven Moreland | c738370 | 2023-10-21 00:43:52 +0000 | [diff] [blame] | 430 | // TODO: we should remove all the path functions from String8 |
| 431 | #if defined(_WIN32) |
| 432 | #define OS_PATH_SEPARATOR '\\' |
| 433 | #else |
| 434 | #define OS_PATH_SEPARATOR '/' |
| 435 | #endif |
| 436 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | String8 String8::getPathDir(void) const |
| 438 | { |
| 439 | const char* cp; |
| 440 | const char*const str = mString; |
| 441 | |
| 442 | cp = strrchr(str, OS_PATH_SEPARATOR); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 443 | if (cp == nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | return String8(""); |
| 445 | else |
| 446 | return String8(str, cp - str); |
| 447 | } |
| 448 | |
Tomasz Wasilczyk | cff2e40 | 2023-09-08 17:08:39 +0000 | [diff] [blame] | 449 | /* |
| 450 | * Helper function for finding the start of an extension in a pathname. |
| 451 | * |
| 452 | * Returns a pointer inside mString, or NULL if no extension was found. |
| 453 | */ |
Tomasz Wasilczyk | a185351 | 2023-09-11 17:45:16 +0000 | [diff] [blame] | 454 | static const char* find_extension(const char* str) { |
Tomasz Wasilczyk | cff2e40 | 2023-09-08 17:08:39 +0000 | [diff] [blame] | 455 | const char* lastSlash; |
| 456 | const char* lastDot; |
Tomasz Wasilczyk | cff2e40 | 2023-09-08 17:08:39 +0000 | [diff] [blame] | 457 | |
| 458 | // only look at the filename |
| 459 | lastSlash = strrchr(str, OS_PATH_SEPARATOR); |
| 460 | if (lastSlash == nullptr) |
| 461 | lastSlash = str; |
| 462 | else |
| 463 | lastSlash++; |
| 464 | |
| 465 | // find the last dot |
| 466 | lastDot = strrchr(lastSlash, '.'); |
| 467 | if (lastDot == nullptr) |
| 468 | return nullptr; |
| 469 | |
| 470 | // looks good, ship it |
Tomasz Wasilczyk | a185351 | 2023-09-11 17:45:16 +0000 | [diff] [blame] | 471 | return lastDot; |
Tomasz Wasilczyk | cff2e40 | 2023-09-08 17:08:39 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | String8 String8::getPathExtension(void) const |
| 475 | { |
Tomasz Wasilczyk | a185351 | 2023-09-11 17:45:16 +0000 | [diff] [blame] | 476 | auto ext = find_extension(mString); |
Tomasz Wasilczyk | cff2e40 | 2023-09-08 17:08:39 +0000 | [diff] [blame] | 477 | if (ext != nullptr) |
| 478 | return String8(ext); |
| 479 | else |
| 480 | return String8(""); |
| 481 | } |
| 482 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | }; // namespace android |