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 | |
Elliott Hughes | 1f8bc86 | 2015-07-29 14:02:29 -0700 | [diff] [blame] | 22 | #include <utils/Compat.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include <utils/String16.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Steven Moreland | d21cfab | 2017-03-10 08:58:36 -0800 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | |
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 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | // Separator used by resource paths. This is not platform dependent contrary |
| 42 | // to OS_PATH_SEPARATOR. |
| 43 | #define RES_PATH_SEPARATOR '/' |
| 44 | |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 45 | static inline char* getEmptyString() { |
| 46 | static SharedBuffer* gEmptyStringBuf = [] { |
| 47 | SharedBuffer* buf = SharedBuffer::alloc(1); |
| 48 | char* str = static_cast<char*>(buf->data()); |
| 49 | *str = 0; |
| 50 | return buf; |
| 51 | }(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | gEmptyStringBuf->acquire(); |
Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 54 | return static_cast<char*>(gEmptyStringBuf->data()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // --------------------------------------------------------------------------- |
| 58 | |
| 59 | static char* allocFromUTF8(const char* in, size_t len) |
| 60 | { |
| 61 | if (len > 0) { |
Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 62 | if (len == SIZE_MAX) { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 63 | return nullptr; |
Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 64 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | SharedBuffer* buf = SharedBuffer::alloc(len+1); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 66 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | if (buf) { |
| 68 | char* str = (char*)buf->data(); |
| 69 | memcpy(str, in, len); |
| 70 | str[len] = 0; |
| 71 | return str; |
| 72 | } |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 73 | return nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return getEmptyString(); |
| 77 | } |
| 78 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 79 | static char* allocFromUTF16(const char16_t* in, size_t len) |
| 80 | { |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 81 | if (len == 0) return getEmptyString(); |
| 82 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 83 | // Allow for closing '\0' |
| 84 | const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1; |
| 85 | if (resultStrLen < 1) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 86 | return getEmptyString(); |
| 87 | } |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 88 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 89 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 90 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 91 | if (!buf) { |
| 92 | return getEmptyString(); |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 95 | char* resultStr = (char*)buf->data(); |
| 96 | utf16_to_utf8(in, len, resultStr, resultStrLen); |
| 97 | return resultStr; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static char* allocFromUTF32(const char32_t* in, size_t len) |
| 101 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 102 | if (len == 0) { |
| 103 | return getEmptyString(); |
| 104 | } |
| 105 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 106 | const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1; |
| 107 | if (resultStrLen < 1) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 108 | return getEmptyString(); |
| 109 | } |
| 110 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 111 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 112 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 113 | if (!buf) { |
| 114 | return getEmptyString(); |
| 115 | } |
| 116 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 117 | char* resultStr = (char*) buf->data(); |
| 118 | utf32_to_utf8(in, len, resultStr, resultStrLen); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 119 | |
Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 120 | return resultStr; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 121 | } |
| 122 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | // --------------------------------------------------------------------------- |
| 124 | |
| 125 | String8::String8() |
| 126 | : mString(getEmptyString()) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | String8::String8(const String8& o) |
| 131 | : mString(o.mString) |
| 132 | { |
| 133 | SharedBuffer::bufferFromData(mString)->acquire(); |
| 134 | } |
| 135 | |
| 136 | String8::String8(const char* o) |
| 137 | : mString(allocFromUTF8(o, strlen(o))) |
| 138 | { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 139 | if (mString == nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | mString = getEmptyString(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | String8::String8(const char* o, size_t len) |
| 145 | : mString(allocFromUTF8(o, len)) |
| 146 | { |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 147 | if (mString == nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | mString = getEmptyString(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | String8::String8(const String16& o) |
| 153 | : mString(allocFromUTF16(o.string(), o.size())) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | String8::String8(const char16_t* o) |
| 158 | : mString(allocFromUTF16(o, strlen16(o))) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | String8::String8(const char16_t* o, size_t len) |
| 163 | : mString(allocFromUTF16(o, len)) |
| 164 | { |
| 165 | } |
| 166 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 167 | String8::String8(const char32_t* o) |
Elliott Hughes | 4b7b4d6 | 2021-04-15 15:18:54 -0700 | [diff] [blame] | 168 | : mString(allocFromUTF32(o, std::char_traits<char32_t>::length(o))) {} |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 169 | |
| 170 | String8::String8(const char32_t* o, size_t len) |
| 171 | : mString(allocFromUTF32(o, len)) |
| 172 | { |
| 173 | } |
| 174 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | String8::~String8() |
| 176 | { |
| 177 | SharedBuffer::bufferFromData(mString)->release(); |
| 178 | } |
| 179 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 180 | size_t String8::length() const |
| 181 | { |
| 182 | return SharedBuffer::sizeFromData(mString)-1; |
| 183 | } |
| 184 | |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 185 | String8 String8::format(const char* fmt, ...) |
| 186 | { |
| 187 | va_list args; |
| 188 | va_start(args, fmt); |
| 189 | |
| 190 | String8 result(formatV(fmt, args)); |
| 191 | |
| 192 | va_end(args); |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | String8 String8::formatV(const char* fmt, va_list args) |
| 197 | { |
| 198 | String8 result; |
| 199 | result.appendFormatV(fmt, args); |
| 200 | return result; |
| 201 | } |
| 202 | |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 203 | void String8::clear() { |
| 204 | SharedBuffer::bufferFromData(mString)->release(); |
| 205 | mString = getEmptyString(); |
| 206 | } |
| 207 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | void String8::setTo(const String8& other) |
| 209 | { |
| 210 | SharedBuffer::bufferFromData(other.mString)->acquire(); |
| 211 | SharedBuffer::bufferFromData(mString)->release(); |
| 212 | mString = other.mString; |
| 213 | } |
| 214 | |
| 215 | status_t String8::setTo(const char* other) |
| 216 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 217 | const char *newString = allocFromUTF8(other, strlen(other)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 219 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 220 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
| 222 | mString = getEmptyString(); |
| 223 | return NO_MEMORY; |
| 224 | } |
| 225 | |
| 226 | status_t String8::setTo(const char* other, size_t len) |
| 227 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 228 | const char *newString = allocFromUTF8(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 230 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 231 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
| 233 | mString = getEmptyString(); |
| 234 | return NO_MEMORY; |
| 235 | } |
| 236 | |
| 237 | status_t String8::setTo(const char16_t* other, size_t len) |
| 238 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 239 | const char *newString = allocFromUTF16(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 241 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 242 | if (mString) return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | |
| 244 | mString = getEmptyString(); |
| 245 | return NO_MEMORY; |
| 246 | } |
| 247 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 248 | status_t String8::setTo(const char32_t* other, size_t len) |
| 249 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 250 | const char *newString = allocFromUTF32(other, len); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 251 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 252 | mString = newString; |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 253 | if (mString) return OK; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 254 | |
| 255 | mString = getEmptyString(); |
| 256 | return NO_MEMORY; |
| 257 | } |
| 258 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | status_t String8::append(const String8& other) |
| 260 | { |
| 261 | const size_t otherLen = other.bytes(); |
| 262 | if (bytes() == 0) { |
| 263 | setTo(other); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 264 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } else if (otherLen == 0) { |
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 | } |
| 268 | |
| 269 | return real_append(other.string(), otherLen); |
| 270 | } |
| 271 | |
| 272 | status_t String8::append(const char* other) |
| 273 | { |
| 274 | return append(other, strlen(other)); |
| 275 | } |
| 276 | |
| 277 | status_t String8::append(const char* other, size_t otherLen) |
| 278 | { |
| 279 | if (bytes() == 0) { |
| 280 | return setTo(other, otherLen); |
| 281 | } else if (otherLen == 0) { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 282 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return real_append(other, otherLen); |
| 286 | } |
| 287 | |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 288 | status_t String8::appendFormat(const char* fmt, ...) |
| 289 | { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 290 | va_list args; |
| 291 | va_start(args, fmt); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 292 | |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 293 | status_t result = appendFormatV(fmt, args); |
| 294 | |
| 295 | va_end(args); |
| 296 | return result; |
| 297 | } |
| 298 | |
| 299 | status_t String8::appendFormatV(const char* fmt, va_list args) |
| 300 | { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 301 | int n, result = OK; |
Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 302 | va_list tmp_args; |
| 303 | |
| 304 | /* args is undefined after vsnprintf. |
| 305 | * So we need a copy here to avoid the |
| 306 | * second vsnprintf access undefined args. |
| 307 | */ |
| 308 | va_copy(tmp_args, args); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 309 | n = vsnprintf(nullptr, 0, fmt, tmp_args); |
Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 310 | va_end(tmp_args); |
| 311 | |
Steven Moreland | 0f0cb95 | 2020-07-28 21:41:54 +0000 | [diff] [blame] | 312 | if (n < 0) return UNKNOWN_ERROR; |
| 313 | |
| 314 | if (n > 0) { |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 315 | size_t oldLength = length(); |
Andrei Homescu | 1a867dc | 2022-03-29 00:30:34 +0000 | [diff] [blame] | 316 | 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] | 317 | oldLength > std::numeric_limits<size_t>::max() - n - 1) { |
| 318 | return NO_MEMORY; |
| 319 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 320 | char* buf = lockBuffer(oldLength + n); |
| 321 | if (buf) { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 322 | vsnprintf(buf + oldLength, n + 1, fmt, args); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 323 | } else { |
| 324 | result = NO_MEMORY; |
| 325 | } |
| 326 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 327 | return result; |
| 328 | } |
| 329 | |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 330 | 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] | 331 | const size_t myLen = bytes(); |
Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 332 | |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 333 | SharedBuffer* buf; |
| 334 | size_t newLen; |
| 335 | if (__builtin_add_overflow(myLen, otherLen, &newLen) || |
| 336 | __builtin_add_overflow(newLen, 1, &newLen) || |
| 337 | (buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) { |
| 338 | return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 340 | |
| 341 | char* str = (char*)buf->data(); |
| 342 | mString = str; |
| 343 | str += myLen; |
| 344 | memcpy(str, other, otherLen); |
| 345 | str[otherLen] = '\0'; |
| 346 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | char* String8::lockBuffer(size_t size) |
| 350 | { |
| 351 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 352 | ->editResize(size+1); |
| 353 | if (buf) { |
| 354 | char* str = (char*)buf->data(); |
| 355 | mString = str; |
| 356 | return str; |
| 357 | } |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 358 | return nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void String8::unlockBuffer() |
| 362 | { |
| 363 | unlockBuffer(strlen(mString)); |
| 364 | } |
| 365 | |
| 366 | status_t String8::unlockBuffer(size_t size) |
| 367 | { |
| 368 | if (size != this->size()) { |
| 369 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 370 | ->editResize(size+1); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 371 | if (! buf) { |
| 372 | return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 374 | |
| 375 | char* str = (char*)buf->data(); |
| 376 | str[size] = 0; |
| 377 | mString = str; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 379 | |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 380 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | ssize_t String8::find(const char* other, size_t start) const |
| 384 | { |
| 385 | size_t len = size(); |
| 386 | if (start >= len) { |
| 387 | return -1; |
| 388 | } |
| 389 | const char* s = mString+start; |
| 390 | const char* p = strstr(s, other); |
| 391 | return p ? p-mString : -1; |
| 392 | } |
| 393 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 394 | bool String8::removeAll(const char* other) { |
| 395 | ssize_t index = find(other); |
| 396 | if (index < 0) return false; |
| 397 | |
| 398 | char* buf = lockBuffer(size()); |
| 399 | if (!buf) return false; // out of memory |
| 400 | |
| 401 | size_t skip = strlen(other); |
| 402 | size_t len = size(); |
| 403 | size_t tail = index; |
| 404 | while (size_t(index) < len) { |
| 405 | ssize_t next = find(other, index + skip); |
| 406 | if (next < 0) { |
| 407 | next = len; |
| 408 | } |
| 409 | |
Andreas Gampe | dd060f0 | 2014-11-13 15:50:17 -0800 | [diff] [blame] | 410 | memmove(buf + tail, buf + index + skip, next - index - skip); |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 411 | tail += next - index - skip; |
| 412 | index = next; |
| 413 | } |
| 414 | unlockBuffer(tail); |
| 415 | return true; |
| 416 | } |
| 417 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | void String8::toLower() |
| 419 | { |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 420 | const size_t length = size(); |
| 421 | if (length == 0) return; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 423 | char* buf = lockBuffer(length); |
| 424 | for (size_t i = length; i > 0; --i) { |
Steven Moreland | fdbc565 | 2020-07-13 23:31:45 +0000 | [diff] [blame] | 425 | *buf = static_cast<char>(tolower(*buf)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | buf++; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
Elliott Hughes | a858395 | 2021-04-08 13:26:49 -0700 | [diff] [blame] | 428 | unlockBuffer(length); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | } |
| 430 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | // --------------------------------------------------------------------------- |
| 432 | // Path functions |
| 433 | |
Greg Kaiser | d03851e | 2021-07-19 20:19:44 +0000 | [diff] [blame] | 434 | static void setPathName(String8& s, const char* name) { |
| 435 | size_t len = strlen(name); |
| 436 | char* buf = s.lockBuffer(len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
| 438 | memcpy(buf, name, len); |
| 439 | |
| 440 | // remove trailing path separator, if present |
Greg Kaiser | d03851e | 2021-07-19 20:19:44 +0000 | [diff] [blame] | 441 | if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | buf[len] = '\0'; |
| 443 | |
Greg Kaiser | d03851e | 2021-07-19 20:19:44 +0000 | [diff] [blame] | 444 | s.unlockBuffer(len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | String8 String8::getPathLeaf(void) const |
| 448 | { |
| 449 | const char* cp; |
| 450 | const char*const buf = mString; |
| 451 | |
| 452 | cp = strrchr(buf, OS_PATH_SEPARATOR); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 453 | if (cp == nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | return String8(*this); |
| 455 | else |
| 456 | return String8(cp+1); |
| 457 | } |
| 458 | |
| 459 | String8 String8::getPathDir(void) const |
| 460 | { |
| 461 | const char* cp; |
| 462 | const char*const str = mString; |
| 463 | |
| 464 | cp = strrchr(str, OS_PATH_SEPARATOR); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 465 | if (cp == nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | return String8(""); |
| 467 | else |
| 468 | return String8(str, cp - str); |
| 469 | } |
| 470 | |
| 471 | String8 String8::walkPath(String8* outRemains) const |
| 472 | { |
| 473 | const char* cp; |
| 474 | const char*const str = mString; |
| 475 | const char* buf = str; |
| 476 | |
| 477 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 478 | if (cp == buf) { |
| 479 | // don't include a leading '/'. |
| 480 | buf = buf+1; |
| 481 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 482 | } |
| 483 | |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 484 | if (cp == nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | String8 res = buf != str ? String8(buf) : *this; |
| 486 | if (outRemains) *outRemains = String8(""); |
| 487 | return res; |
| 488 | } |
| 489 | |
| 490 | String8 res(buf, cp-buf); |
| 491 | if (outRemains) *outRemains = String8(cp+1); |
| 492 | return res; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Helper function for finding the start of an extension in a pathname. |
| 497 | * |
| 498 | * Returns a pointer inside mString, or NULL if no extension was found. |
| 499 | */ |
| 500 | char* String8::find_extension(void) const |
| 501 | { |
| 502 | const char* lastSlash; |
| 503 | const char* lastDot; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | const char* const str = mString; |
| 505 | |
| 506 | // only look at the filename |
| 507 | lastSlash = strrchr(str, OS_PATH_SEPARATOR); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 508 | if (lastSlash == nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | lastSlash = str; |
| 510 | else |
| 511 | lastSlash++; |
| 512 | |
| 513 | // find the last dot |
| 514 | lastDot = strrchr(lastSlash, '.'); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 515 | if (lastDot == nullptr) |
| 516 | return nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | |
| 518 | // looks good, ship it |
| 519 | return const_cast<char*>(lastDot); |
| 520 | } |
| 521 | |
| 522 | String8 String8::getPathExtension(void) const |
| 523 | { |
| 524 | char* ext; |
| 525 | |
| 526 | ext = find_extension(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 527 | if (ext != nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | return String8(ext); |
| 529 | else |
| 530 | return String8(""); |
| 531 | } |
| 532 | |
| 533 | String8 String8::getBasePath(void) const |
| 534 | { |
| 535 | char* ext; |
| 536 | const char* const str = mString; |
| 537 | |
| 538 | ext = find_extension(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 539 | if (ext == nullptr) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | return String8(*this); |
| 541 | else |
| 542 | return String8(str, ext - str); |
| 543 | } |
| 544 | |
| 545 | String8& String8::appendPath(const char* name) |
| 546 | { |
| 547 | // TODO: The test below will fail for Win32 paths. Fix later or ignore. |
| 548 | if (name[0] != OS_PATH_SEPARATOR) { |
| 549 | if (*name == '\0') { |
| 550 | // nothing to do |
| 551 | return *this; |
| 552 | } |
| 553 | |
| 554 | size_t len = length(); |
| 555 | if (len == 0) { |
| 556 | // no existing filename, just use the new one |
Greg Kaiser | d03851e | 2021-07-19 20:19:44 +0000 | [diff] [blame] | 557 | setPathName(*this, name); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | return *this; |
| 559 | } |
| 560 | |
| 561 | // make room for oldPath + '/' + newPath |
| 562 | int newlen = strlen(name); |
| 563 | |
| 564 | char* buf = lockBuffer(len+1+newlen); |
| 565 | |
| 566 | // insert a '/' if needed |
| 567 | if (buf[len-1] != OS_PATH_SEPARATOR) |
| 568 | buf[len++] = OS_PATH_SEPARATOR; |
| 569 | |
| 570 | memcpy(buf+len, name, newlen+1); |
| 571 | len += newlen; |
| 572 | |
| 573 | unlockBuffer(len); |
| 574 | |
| 575 | return *this; |
| 576 | } else { |
Greg Kaiser | d03851e | 2021-07-19 20:19:44 +0000 | [diff] [blame] | 577 | setPathName(*this, name); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | return *this; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | String8& String8::convertToResPath() |
| 583 | { |
| 584 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR |
| 585 | size_t len = length(); |
| 586 | if (len > 0) { |
| 587 | char * buf = lockBuffer(len); |
| 588 | for (char * end = buf + len; buf < end; ++buf) { |
| 589 | if (*buf == OS_PATH_SEPARATOR) |
| 590 | *buf = RES_PATH_SEPARATOR; |
| 591 | } |
| 592 | unlockBuffer(len); |
| 593 | } |
| 594 | #endif |
| 595 | return *this; |
| 596 | } |
| 597 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | }; // namespace android |