| 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 |  | 
| 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 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | // Separator used by resource paths. This is not platform dependent contrary | 
|  | 40 | // to OS_PATH_SEPARATOR. | 
|  | 41 | #define RES_PATH_SEPARATOR '/' | 
|  | 42 |  | 
| Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 43 | static inline char* getEmptyString() { | 
|  | 44 | static SharedBuffer* gEmptyStringBuf = [] { | 
|  | 45 | SharedBuffer* buf = SharedBuffer::alloc(1); | 
|  | 46 | char* str = static_cast<char*>(buf->data()); | 
|  | 47 | *str = 0; | 
|  | 48 | return buf; | 
|  | 49 | }(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | gEmptyStringBuf->acquire(); | 
| Steven Moreland | 241b93c | 2018-03-06 09:11:29 -0800 | [diff] [blame] | 52 | return static_cast<char*>(gEmptyStringBuf->data()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | // --------------------------------------------------------------------------- | 
|  | 56 |  | 
|  | 57 | static char* allocFromUTF8(const char* in, size_t len) | 
|  | 58 | { | 
|  | 59 | if (len > 0) { | 
| Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 60 | if (len == SIZE_MAX) { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 61 | return nullptr; | 
| Sergio Giro | ebabef2 | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 62 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | SharedBuffer* buf = SharedBuffer::alloc(len+1); | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 64 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | if (buf) { | 
|  | 66 | char* str = (char*)buf->data(); | 
|  | 67 | memcpy(str, in, len); | 
|  | 68 | str[len] = 0; | 
|  | 69 | return str; | 
|  | 70 | } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 71 | return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | return getEmptyString(); | 
|  | 75 | } | 
|  | 76 |  | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 77 | static char* allocFromUTF16(const char16_t* in, size_t len) | 
|  | 78 | { | 
| Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 79 | if (len == 0) return getEmptyString(); | 
|  | 80 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 81 | // Allow for closing '\0' | 
|  | 82 | const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1; | 
|  | 83 | if (resultStrLen < 1) { | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 84 | return getEmptyString(); | 
|  | 85 | } | 
| Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 86 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 87 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 88 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 89 | if (!buf) { | 
|  | 90 | return getEmptyString(); | 
| Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 93 | char* resultStr = (char*)buf->data(); | 
|  | 94 | utf16_to_utf8(in, len, resultStr, resultStrLen); | 
|  | 95 | return resultStr; | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
|  | 98 | static char* allocFromUTF32(const char32_t* in, size_t len) | 
|  | 99 | { | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 100 | if (len == 0) { | 
|  | 101 | return getEmptyString(); | 
|  | 102 | } | 
|  | 103 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 104 | const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1; | 
|  | 105 | if (resultStrLen < 1) { | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 106 | return getEmptyString(); | 
|  | 107 | } | 
|  | 108 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 109 | SharedBuffer* buf = SharedBuffer::alloc(resultStrLen); | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 110 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 111 | if (!buf) { | 
|  | 112 | return getEmptyString(); | 
|  | 113 | } | 
|  | 114 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 115 | char* resultStr = (char*) buf->data(); | 
|  | 116 | utf32_to_utf8(in, len, resultStr, resultStrLen); | 
| Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 117 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 118 | return resultStr; | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | // --------------------------------------------------------------------------- | 
|  | 122 |  | 
|  | 123 | String8::String8() | 
|  | 124 | : mString(getEmptyString()) | 
|  | 125 | { | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | String8::String8(const String8& o) | 
|  | 129 | : mString(o.mString) | 
|  | 130 | { | 
|  | 131 | SharedBuffer::bufferFromData(mString)->acquire(); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | String8::String8(const char* o) | 
|  | 135 | : mString(allocFromUTF8(o, strlen(o))) | 
|  | 136 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 137 | if (mString == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | mString = getEmptyString(); | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | String8::String8(const char* o, size_t len) | 
|  | 143 | : mString(allocFromUTF8(o, len)) | 
|  | 144 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 145 | if (mString == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | mString = getEmptyString(); | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | String8::String8(const String16& o) | 
|  | 151 | : mString(allocFromUTF16(o.string(), o.size())) | 
|  | 152 | { | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | String8::String8(const char16_t* o) | 
|  | 156 | : mString(allocFromUTF16(o, strlen16(o))) | 
|  | 157 | { | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | String8::String8(const char16_t* o, size_t len) | 
|  | 161 | : mString(allocFromUTF16(o, len)) | 
|  | 162 | { | 
|  | 163 | } | 
|  | 164 |  | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 165 | String8::String8(const char32_t* o) | 
|  | 166 | : mString(allocFromUTF32(o, strlen32(o))) | 
|  | 167 | { | 
|  | 168 | } | 
|  | 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 |  | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 312 | if (n != 0) { | 
|  | 313 | size_t oldLength = length(); | 
|  | 314 | char* buf = lockBuffer(oldLength + n); | 
|  | 315 | if (buf) { | 
| Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 316 | vsnprintf(buf + oldLength, n + 1, fmt, args); | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 317 | } else { | 
|  | 318 | result = NO_MEMORY; | 
|  | 319 | } | 
|  | 320 | } | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 321 | return result; | 
|  | 322 | } | 
|  | 323 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | status_t String8::real_append(const char* other, size_t otherLen) | 
|  | 325 | { | 
|  | 326 | const size_t myLen = bytes(); | 
| Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 327 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
|  | 329 | ->editResize(myLen+otherLen+1); | 
|  | 330 | if (buf) { | 
|  | 331 | char* str = (char*)buf->data(); | 
|  | 332 | mString = str; | 
|  | 333 | str += myLen; | 
|  | 334 | memcpy(str, other, otherLen); | 
|  | 335 | str[otherLen] = '\0'; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 336 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } | 
|  | 338 | return NO_MEMORY; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | char* String8::lockBuffer(size_t size) | 
|  | 342 | { | 
|  | 343 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
|  | 344 | ->editResize(size+1); | 
|  | 345 | if (buf) { | 
|  | 346 | char* str = (char*)buf->data(); | 
|  | 347 | mString = str; | 
|  | 348 | return str; | 
|  | 349 | } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 350 | return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
|  | 353 | void String8::unlockBuffer() | 
|  | 354 | { | 
|  | 355 | unlockBuffer(strlen(mString)); | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | status_t String8::unlockBuffer(size_t size) | 
|  | 359 | { | 
|  | 360 | if (size != this->size()) { | 
|  | 361 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
|  | 362 | ->editResize(size+1); | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 363 | if (! buf) { | 
|  | 364 | return NO_MEMORY; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 366 |  | 
|  | 367 | char* str = (char*)buf->data(); | 
|  | 368 | str[size] = 0; | 
|  | 369 | mString = str; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 371 |  | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 372 | return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
|  | 375 | ssize_t String8::find(const char* other, size_t start) const | 
|  | 376 | { | 
|  | 377 | size_t len = size(); | 
|  | 378 | if (start >= len) { | 
|  | 379 | return -1; | 
|  | 380 | } | 
|  | 381 | const char* s = mString+start; | 
|  | 382 | const char* p = strstr(s, other); | 
|  | 383 | return p ? p-mString : -1; | 
|  | 384 | } | 
|  | 385 |  | 
| Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 386 | bool String8::removeAll(const char* other) { | 
|  | 387 | ssize_t index = find(other); | 
|  | 388 | if (index < 0) return false; | 
|  | 389 |  | 
|  | 390 | char* buf = lockBuffer(size()); | 
|  | 391 | if (!buf) return false; // out of memory | 
|  | 392 |  | 
|  | 393 | size_t skip = strlen(other); | 
|  | 394 | size_t len = size(); | 
|  | 395 | size_t tail = index; | 
|  | 396 | while (size_t(index) < len) { | 
|  | 397 | ssize_t next = find(other, index + skip); | 
|  | 398 | if (next < 0) { | 
|  | 399 | next = len; | 
|  | 400 | } | 
|  | 401 |  | 
| Andreas Gampe | dd060f0 | 2014-11-13 15:50:17 -0800 | [diff] [blame] | 402 | memmove(buf + tail, buf + index + skip, next - index - skip); | 
| Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 403 | tail += next - index - skip; | 
|  | 404 | index = next; | 
|  | 405 | } | 
|  | 406 | unlockBuffer(tail); | 
|  | 407 | return true; | 
|  | 408 | } | 
|  | 409 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | void String8::toLower() | 
|  | 411 | { | 
|  | 412 | toLower(0, size()); | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | void String8::toLower(size_t start, size_t length) | 
|  | 416 | { | 
|  | 417 | const size_t len = size(); | 
|  | 418 | if (start >= len) { | 
|  | 419 | return; | 
|  | 420 | } | 
|  | 421 | if (start+length > len) { | 
|  | 422 | length = len-start; | 
|  | 423 | } | 
|  | 424 | char* buf = lockBuffer(len); | 
|  | 425 | buf += start; | 
|  | 426 | while (length > 0) { | 
|  | 427 | *buf = tolower(*buf); | 
|  | 428 | buf++; | 
|  | 429 | length--; | 
|  | 430 | } | 
|  | 431 | unlockBuffer(len); | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | void String8::toUpper() | 
|  | 435 | { | 
|  | 436 | toUpper(0, size()); | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | void String8::toUpper(size_t start, size_t length) | 
|  | 440 | { | 
|  | 441 | const size_t len = size(); | 
|  | 442 | if (start >= len) { | 
|  | 443 | return; | 
|  | 444 | } | 
|  | 445 | if (start+length > len) { | 
|  | 446 | length = len-start; | 
|  | 447 | } | 
|  | 448 | char* buf = lockBuffer(len); | 
|  | 449 | buf += start; | 
|  | 450 | while (length > 0) { | 
|  | 451 | *buf = toupper(*buf); | 
|  | 452 | buf++; | 
|  | 453 | length--; | 
|  | 454 | } | 
|  | 455 | unlockBuffer(len); | 
|  | 456 | } | 
|  | 457 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | // --------------------------------------------------------------------------- | 
|  | 459 | // Path functions | 
|  | 460 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | void String8::setPathName(const char* name) | 
|  | 462 | { | 
|  | 463 | setPathName(name, strlen(name)); | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | void String8::setPathName(const char* name, size_t len) | 
|  | 467 | { | 
|  | 468 | char* buf = lockBuffer(len); | 
|  | 469 |  | 
|  | 470 | memcpy(buf, name, len); | 
|  | 471 |  | 
|  | 472 | // remove trailing path separator, if present | 
|  | 473 | if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR) | 
|  | 474 | len--; | 
|  | 475 |  | 
|  | 476 | buf[len] = '\0'; | 
|  | 477 |  | 
|  | 478 | unlockBuffer(len); | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | String8 String8::getPathLeaf(void) const | 
|  | 482 | { | 
|  | 483 | const char* cp; | 
|  | 484 | const char*const buf = mString; | 
|  | 485 |  | 
|  | 486 | cp = strrchr(buf, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 487 | if (cp == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | return String8(*this); | 
|  | 489 | else | 
|  | 490 | return String8(cp+1); | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | String8 String8::getPathDir(void) const | 
|  | 494 | { | 
|  | 495 | const char* cp; | 
|  | 496 | const char*const str = mString; | 
|  | 497 |  | 
|  | 498 | cp = strrchr(str, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 499 | if (cp == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | return String8(""); | 
|  | 501 | else | 
|  | 502 | return String8(str, cp - str); | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | String8 String8::walkPath(String8* outRemains) const | 
|  | 506 | { | 
|  | 507 | const char* cp; | 
|  | 508 | const char*const str = mString; | 
|  | 509 | const char* buf = str; | 
|  | 510 |  | 
|  | 511 | cp = strchr(buf, OS_PATH_SEPARATOR); | 
|  | 512 | if (cp == buf) { | 
|  | 513 | // don't include a leading '/'. | 
|  | 514 | buf = buf+1; | 
|  | 515 | cp = strchr(buf, OS_PATH_SEPARATOR); | 
|  | 516 | } | 
|  | 517 |  | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 518 | if (cp == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | String8 res = buf != str ? String8(buf) : *this; | 
|  | 520 | if (outRemains) *outRemains = String8(""); | 
|  | 521 | return res; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | String8 res(buf, cp-buf); | 
|  | 525 | if (outRemains) *outRemains = String8(cp+1); | 
|  | 526 | return res; | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | /* | 
|  | 530 | * Helper function for finding the start of an extension in a pathname. | 
|  | 531 | * | 
|  | 532 | * Returns a pointer inside mString, or NULL if no extension was found. | 
|  | 533 | */ | 
|  | 534 | char* String8::find_extension(void) const | 
|  | 535 | { | 
|  | 536 | const char* lastSlash; | 
|  | 537 | const char* lastDot; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | const char* const str = mString; | 
|  | 539 |  | 
|  | 540 | // only look at the filename | 
|  | 541 | lastSlash = strrchr(str, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 542 | if (lastSlash == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | lastSlash = str; | 
|  | 544 | else | 
|  | 545 | lastSlash++; | 
|  | 546 |  | 
|  | 547 | // find the last dot | 
|  | 548 | lastDot = strrchr(lastSlash, '.'); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 549 | if (lastDot == nullptr) | 
|  | 550 | return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 |  | 
|  | 552 | // looks good, ship it | 
|  | 553 | return const_cast<char*>(lastDot); | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | String8 String8::getPathExtension(void) const | 
|  | 557 | { | 
|  | 558 | char* ext; | 
|  | 559 |  | 
|  | 560 | ext = find_extension(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 561 | if (ext != nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | return String8(ext); | 
|  | 563 | else | 
|  | 564 | return String8(""); | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | String8 String8::getBasePath(void) const | 
|  | 568 | { | 
|  | 569 | char* ext; | 
|  | 570 | const char* const str = mString; | 
|  | 571 |  | 
|  | 572 | ext = find_extension(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 573 | if (ext == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | return String8(*this); | 
|  | 575 | else | 
|  | 576 | return String8(str, ext - str); | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | String8& String8::appendPath(const char* name) | 
|  | 580 | { | 
|  | 581 | // TODO: The test below will fail for Win32 paths. Fix later or ignore. | 
|  | 582 | if (name[0] != OS_PATH_SEPARATOR) { | 
|  | 583 | if (*name == '\0') { | 
|  | 584 | // nothing to do | 
|  | 585 | return *this; | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | size_t len = length(); | 
|  | 589 | if (len == 0) { | 
|  | 590 | // no existing filename, just use the new one | 
|  | 591 | setPathName(name); | 
|  | 592 | return *this; | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | // make room for oldPath + '/' + newPath | 
|  | 596 | int newlen = strlen(name); | 
|  | 597 |  | 
|  | 598 | char* buf = lockBuffer(len+1+newlen); | 
|  | 599 |  | 
|  | 600 | // insert a '/' if needed | 
|  | 601 | if (buf[len-1] != OS_PATH_SEPARATOR) | 
|  | 602 | buf[len++] = OS_PATH_SEPARATOR; | 
|  | 603 |  | 
|  | 604 | memcpy(buf+len, name, newlen+1); | 
|  | 605 | len += newlen; | 
|  | 606 |  | 
|  | 607 | unlockBuffer(len); | 
|  | 608 |  | 
|  | 609 | return *this; | 
|  | 610 | } else { | 
|  | 611 | setPathName(name); | 
|  | 612 | return *this; | 
|  | 613 | } | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | String8& String8::convertToResPath() | 
|  | 617 | { | 
|  | 618 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR | 
|  | 619 | size_t len = length(); | 
|  | 620 | if (len > 0) { | 
|  | 621 | char * buf = lockBuffer(len); | 
|  | 622 | for (char * end = buf + len; buf < end; ++buf) { | 
|  | 623 | if (*buf == OS_PATH_SEPARATOR) | 
|  | 624 | *buf = RES_PATH_SEPARATOR; | 
|  | 625 | } | 
|  | 626 | unlockBuffer(len); | 
|  | 627 | } | 
|  | 628 | #endif | 
|  | 629 | return *this; | 
|  | 630 | } | 
|  | 631 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | }; // namespace android |