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