| 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 |  | 
| Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 128 | String8::String8(StaticLinkage) | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 129 |     : mString(nullptr) | 
| Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 130 | { | 
 | 131 |     // this constructor is used when we can't rely on the static-initializers | 
 | 132 |     // having run. In this case we always allocate an empty string. It's less | 
 | 133 |     // efficient than using getEmptyString(), but we assume it's uncommon. | 
 | 134 |  | 
 | 135 |     char* data = static_cast<char*>( | 
 | 136 |             SharedBuffer::alloc(sizeof(char))->data()); | 
 | 137 |     data[0] = 0; | 
 | 138 |     mString = data; | 
 | 139 | } | 
 | 140 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | String8::String8(const String8& o) | 
 | 142 |     : mString(o.mString) | 
 | 143 | { | 
 | 144 |     SharedBuffer::bufferFromData(mString)->acquire(); | 
 | 145 | } | 
 | 146 |  | 
 | 147 | String8::String8(const char* o) | 
 | 148 |     : mString(allocFromUTF8(o, strlen(o))) | 
 | 149 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 150 |     if (mString == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 |         mString = getEmptyString(); | 
 | 152 |     } | 
 | 153 | } | 
 | 154 |  | 
 | 155 | String8::String8(const char* o, size_t len) | 
 | 156 |     : mString(allocFromUTF8(o, len)) | 
 | 157 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 158 |     if (mString == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 |         mString = getEmptyString(); | 
 | 160 |     } | 
 | 161 | } | 
 | 162 |  | 
 | 163 | String8::String8(const String16& o) | 
 | 164 |     : mString(allocFromUTF16(o.string(), o.size())) | 
 | 165 | { | 
 | 166 | } | 
 | 167 |  | 
 | 168 | String8::String8(const char16_t* o) | 
 | 169 |     : mString(allocFromUTF16(o, strlen16(o))) | 
 | 170 | { | 
 | 171 | } | 
 | 172 |  | 
 | 173 | String8::String8(const char16_t* o, size_t len) | 
 | 174 |     : mString(allocFromUTF16(o, len)) | 
 | 175 | { | 
 | 176 | } | 
 | 177 |  | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 178 | String8::String8(const char32_t* o) | 
 | 179 |     : mString(allocFromUTF32(o, strlen32(o))) | 
 | 180 | { | 
 | 181 | } | 
 | 182 |  | 
 | 183 | String8::String8(const char32_t* o, size_t len) | 
 | 184 |     : mString(allocFromUTF32(o, len)) | 
 | 185 | { | 
 | 186 | } | 
 | 187 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | String8::~String8() | 
 | 189 | { | 
 | 190 |     SharedBuffer::bufferFromData(mString)->release(); | 
 | 191 | } | 
 | 192 |  | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 193 | size_t String8::length() const | 
 | 194 | { | 
 | 195 |     return SharedBuffer::sizeFromData(mString)-1; | 
 | 196 | } | 
 | 197 |  | 
| Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 198 | String8 String8::format(const char* fmt, ...) | 
 | 199 | { | 
 | 200 |     va_list args; | 
 | 201 |     va_start(args, fmt); | 
 | 202 |  | 
 | 203 |     String8 result(formatV(fmt, args)); | 
 | 204 |  | 
 | 205 |     va_end(args); | 
 | 206 |     return result; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | String8 String8::formatV(const char* fmt, va_list args) | 
 | 210 | { | 
 | 211 |     String8 result; | 
 | 212 |     result.appendFormatV(fmt, args); | 
 | 213 |     return result; | 
 | 214 | } | 
 | 215 |  | 
| Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 216 | void String8::clear() { | 
 | 217 |     SharedBuffer::bufferFromData(mString)->release(); | 
 | 218 |     mString = getEmptyString(); | 
 | 219 | } | 
 | 220 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | void String8::setTo(const String8& other) | 
 | 222 | { | 
 | 223 |     SharedBuffer::bufferFromData(other.mString)->acquire(); | 
 | 224 |     SharedBuffer::bufferFromData(mString)->release(); | 
 | 225 |     mString = other.mString; | 
 | 226 | } | 
 | 227 |  | 
 | 228 | status_t String8::setTo(const char* other) | 
 | 229 | { | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 230 |     const char *newString = allocFromUTF8(other, strlen(other)); | 
| 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 |  | 
 | 239 | status_t String8::setTo(const char* other, size_t len) | 
 | 240 | { | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 241 |     const char *newString = allocFromUTF8(other, len); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [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; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 |  | 
 | 246 |     mString = getEmptyString(); | 
 | 247 |     return NO_MEMORY; | 
 | 248 | } | 
 | 249 |  | 
 | 250 | status_t String8::setTo(const char16_t* other, size_t len) | 
 | 251 | { | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 252 |     const char *newString = allocFromUTF16(other, len); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 |     SharedBuffer::bufferFromData(mString)->release(); | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 254 |     mString = newString; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 255 |     if (mString) return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 |  | 
 | 257 |     mString = getEmptyString(); | 
 | 258 |     return NO_MEMORY; | 
 | 259 | } | 
 | 260 |  | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 261 | status_t String8::setTo(const char32_t* other, size_t len) | 
 | 262 | { | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 263 |     const char *newString = allocFromUTF32(other, len); | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 264 |     SharedBuffer::bufferFromData(mString)->release(); | 
| Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 265 |     mString = newString; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 266 |     if (mString) return OK; | 
| Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 267 |  | 
 | 268 |     mString = getEmptyString(); | 
 | 269 |     return NO_MEMORY; | 
 | 270 | } | 
 | 271 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | status_t String8::append(const String8& other) | 
 | 273 | { | 
 | 274 |     const size_t otherLen = other.bytes(); | 
 | 275 |     if (bytes() == 0) { | 
 | 276 |         setTo(other); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 277 |         return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 |     } else if (otherLen == 0) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 279 |         return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 |     } | 
 | 281 |  | 
 | 282 |     return real_append(other.string(), otherLen); | 
 | 283 | } | 
 | 284 |  | 
 | 285 | status_t String8::append(const char* other) | 
 | 286 | { | 
 | 287 |     return append(other, strlen(other)); | 
 | 288 | } | 
 | 289 |  | 
 | 290 | status_t String8::append(const char* other, size_t otherLen) | 
 | 291 | { | 
 | 292 |     if (bytes() == 0) { | 
 | 293 |         return setTo(other, otherLen); | 
 | 294 |     } else if (otherLen == 0) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 295 |         return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 |     } | 
 | 297 |  | 
 | 298 |     return real_append(other, otherLen); | 
 | 299 | } | 
 | 300 |  | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 301 | status_t String8::appendFormat(const char* fmt, ...) | 
 | 302 | { | 
| Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 303 |     va_list args; | 
 | 304 |     va_start(args, fmt); | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 305 |  | 
| Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 306 |     status_t result = appendFormatV(fmt, args); | 
 | 307 |  | 
 | 308 |     va_end(args); | 
 | 309 |     return result; | 
 | 310 | } | 
 | 311 |  | 
 | 312 | status_t String8::appendFormatV(const char* fmt, va_list args) | 
 | 313 | { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 314 |     int n, result = OK; | 
| Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 315 |     va_list tmp_args; | 
 | 316 |  | 
 | 317 |     /* args is undefined after vsnprintf. | 
 | 318 |      * So we need a copy here to avoid the | 
 | 319 |      * second vsnprintf access undefined args. | 
 | 320 |      */ | 
 | 321 |     va_copy(tmp_args, args); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 322 |     n = vsnprintf(nullptr, 0, fmt, tmp_args); | 
| Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 323 |     va_end(tmp_args); | 
 | 324 |  | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 325 |     if (n != 0) { | 
 | 326 |         size_t oldLength = length(); | 
 | 327 |         char* buf = lockBuffer(oldLength + n); | 
 | 328 |         if (buf) { | 
| Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 329 |             vsnprintf(buf + oldLength, n + 1, fmt, args); | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 330 |         } else { | 
 | 331 |             result = NO_MEMORY; | 
 | 332 |         } | 
 | 333 |     } | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 334 |     return result; | 
 | 335 | } | 
 | 336 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | status_t String8::real_append(const char* other, size_t otherLen) | 
 | 338 | { | 
 | 339 |     const size_t myLen = bytes(); | 
| Samuel Tan | 95fd527 | 2016-02-18 16:56:21 -0800 | [diff] [blame] | 340 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 |     SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
 | 342 |         ->editResize(myLen+otherLen+1); | 
 | 343 |     if (buf) { | 
 | 344 |         char* str = (char*)buf->data(); | 
 | 345 |         mString = str; | 
 | 346 |         str += myLen; | 
 | 347 |         memcpy(str, other, otherLen); | 
 | 348 |         str[otherLen] = '\0'; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 349 |         return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 |     } | 
 | 351 |     return NO_MEMORY; | 
 | 352 | } | 
 | 353 |  | 
 | 354 | char* String8::lockBuffer(size_t size) | 
 | 355 | { | 
 | 356 |     SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
 | 357 |         ->editResize(size+1); | 
 | 358 |     if (buf) { | 
 | 359 |         char* str = (char*)buf->data(); | 
 | 360 |         mString = str; | 
 | 361 |         return str; | 
 | 362 |     } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 363 |     return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } | 
 | 365 |  | 
 | 366 | void String8::unlockBuffer() | 
 | 367 | { | 
 | 368 |     unlockBuffer(strlen(mString)); | 
 | 369 | } | 
 | 370 |  | 
 | 371 | status_t String8::unlockBuffer(size_t size) | 
 | 372 | { | 
 | 373 |     if (size != this->size()) { | 
 | 374 |         SharedBuffer* buf = SharedBuffer::bufferFromData(mString) | 
 | 375 |             ->editResize(size+1); | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 376 |         if (! buf) { | 
 | 377 |             return NO_MEMORY; | 
| 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 |  | 
 | 380 |         char* str = (char*)buf->data(); | 
 | 381 |         str[size] = 0; | 
 | 382 |         mString = str; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 |     } | 
| Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 384 |  | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 385 |     return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } | 
 | 387 |  | 
 | 388 | ssize_t String8::find(const char* other, size_t start) const | 
 | 389 | { | 
 | 390 |     size_t len = size(); | 
 | 391 |     if (start >= len) { | 
 | 392 |         return -1; | 
 | 393 |     } | 
 | 394 |     const char* s = mString+start; | 
 | 395 |     const char* p = strstr(s, other); | 
 | 396 |     return p ? p-mString : -1; | 
 | 397 | } | 
 | 398 |  | 
| Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 399 | bool String8::removeAll(const char* other) { | 
 | 400 |     ssize_t index = find(other); | 
 | 401 |     if (index < 0) return false; | 
 | 402 |  | 
 | 403 |     char* buf = lockBuffer(size()); | 
 | 404 |     if (!buf) return false; // out of memory | 
 | 405 |  | 
 | 406 |     size_t skip = strlen(other); | 
 | 407 |     size_t len = size(); | 
 | 408 |     size_t tail = index; | 
 | 409 |     while (size_t(index) < len) { | 
 | 410 |         ssize_t next = find(other, index + skip); | 
 | 411 |         if (next < 0) { | 
 | 412 |             next = len; | 
 | 413 |         } | 
 | 414 |  | 
| Andreas Gampe | dd060f0 | 2014-11-13 15:50:17 -0800 | [diff] [blame] | 415 |         memmove(buf + tail, buf + index + skip, next - index - skip); | 
| Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 416 |         tail += next - index - skip; | 
 | 417 |         index = next; | 
 | 418 |     } | 
 | 419 |     unlockBuffer(tail); | 
 | 420 |     return true; | 
 | 421 | } | 
 | 422 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | void String8::toLower() | 
 | 424 | { | 
 | 425 |     toLower(0, size()); | 
 | 426 | } | 
 | 427 |  | 
 | 428 | void String8::toLower(size_t start, size_t length) | 
 | 429 | { | 
 | 430 |     const size_t len = size(); | 
 | 431 |     if (start >= len) { | 
 | 432 |         return; | 
 | 433 |     } | 
 | 434 |     if (start+length > len) { | 
 | 435 |         length = len-start; | 
 | 436 |     } | 
 | 437 |     char* buf = lockBuffer(len); | 
 | 438 |     buf += start; | 
 | 439 |     while (length > 0) { | 
 | 440 |         *buf = tolower(*buf); | 
 | 441 |         buf++; | 
 | 442 |         length--; | 
 | 443 |     } | 
 | 444 |     unlockBuffer(len); | 
 | 445 | } | 
 | 446 |  | 
 | 447 | void String8::toUpper() | 
 | 448 | { | 
 | 449 |     toUpper(0, size()); | 
 | 450 | } | 
 | 451 |  | 
 | 452 | void String8::toUpper(size_t start, size_t length) | 
 | 453 | { | 
 | 454 |     const size_t len = size(); | 
 | 455 |     if (start >= len) { | 
 | 456 |         return; | 
 | 457 |     } | 
 | 458 |     if (start+length > len) { | 
 | 459 |         length = len-start; | 
 | 460 |     } | 
 | 461 |     char* buf = lockBuffer(len); | 
 | 462 |     buf += start; | 
 | 463 |     while (length > 0) { | 
 | 464 |         *buf = toupper(*buf); | 
 | 465 |         buf++; | 
 | 466 |         length--; | 
 | 467 |     } | 
 | 468 |     unlockBuffer(len); | 
 | 469 | } | 
 | 470 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | // --------------------------------------------------------------------------- | 
 | 472 | // Path functions | 
 | 473 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | void String8::setPathName(const char* name) | 
 | 475 | { | 
 | 476 |     setPathName(name, strlen(name)); | 
 | 477 | } | 
 | 478 |  | 
 | 479 | void String8::setPathName(const char* name, size_t len) | 
 | 480 | { | 
 | 481 |     char* buf = lockBuffer(len); | 
 | 482 |  | 
 | 483 |     memcpy(buf, name, len); | 
 | 484 |  | 
 | 485 |     // remove trailing path separator, if present | 
 | 486 |     if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR) | 
 | 487 |         len--; | 
 | 488 |  | 
 | 489 |     buf[len] = '\0'; | 
 | 490 |  | 
 | 491 |     unlockBuffer(len); | 
 | 492 | } | 
 | 493 |  | 
 | 494 | String8 String8::getPathLeaf(void) const | 
 | 495 | { | 
 | 496 |     const char* cp; | 
 | 497 |     const char*const buf = mString; | 
 | 498 |  | 
 | 499 |     cp = strrchr(buf, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 500 |     if (cp == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 |         return String8(*this); | 
 | 502 |     else | 
 | 503 |         return String8(cp+1); | 
 | 504 | } | 
 | 505 |  | 
 | 506 | String8 String8::getPathDir(void) const | 
 | 507 | { | 
 | 508 |     const char* cp; | 
 | 509 |     const char*const str = mString; | 
 | 510 |  | 
 | 511 |     cp = strrchr(str, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 512 |     if (cp == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 |         return String8(""); | 
 | 514 |     else | 
 | 515 |         return String8(str, cp - str); | 
 | 516 | } | 
 | 517 |  | 
 | 518 | String8 String8::walkPath(String8* outRemains) const | 
 | 519 | { | 
 | 520 |     const char* cp; | 
 | 521 |     const char*const str = mString; | 
 | 522 |     const char* buf = str; | 
 | 523 |  | 
 | 524 |     cp = strchr(buf, OS_PATH_SEPARATOR); | 
 | 525 |     if (cp == buf) { | 
 | 526 |         // don't include a leading '/'. | 
 | 527 |         buf = buf+1; | 
 | 528 |         cp = strchr(buf, OS_PATH_SEPARATOR); | 
 | 529 |     } | 
 | 530 |  | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 531 |     if (cp == nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 |         String8 res = buf != str ? String8(buf) : *this; | 
 | 533 |         if (outRemains) *outRemains = String8(""); | 
 | 534 |         return res; | 
 | 535 |     } | 
 | 536 |  | 
 | 537 |     String8 res(buf, cp-buf); | 
 | 538 |     if (outRemains) *outRemains = String8(cp+1); | 
 | 539 |     return res; | 
 | 540 | } | 
 | 541 |  | 
 | 542 | /* | 
 | 543 |  * Helper function for finding the start of an extension in a pathname. | 
 | 544 |  * | 
 | 545 |  * Returns a pointer inside mString, or NULL if no extension was found. | 
 | 546 |  */ | 
 | 547 | char* String8::find_extension(void) const | 
 | 548 | { | 
 | 549 |     const char* lastSlash; | 
 | 550 |     const char* lastDot; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 |     const char* const str = mString; | 
 | 552 |  | 
 | 553 |     // only look at the filename | 
 | 554 |     lastSlash = strrchr(str, OS_PATH_SEPARATOR); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 555 |     if (lastSlash == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 |         lastSlash = str; | 
 | 557 |     else | 
 | 558 |         lastSlash++; | 
 | 559 |  | 
 | 560 |     // find the last dot | 
 | 561 |     lastDot = strrchr(lastSlash, '.'); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 562 |     if (lastDot == nullptr) | 
 | 563 |         return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 |  | 
 | 565 |     // looks good, ship it | 
 | 566 |     return const_cast<char*>(lastDot); | 
 | 567 | } | 
 | 568 |  | 
 | 569 | String8 String8::getPathExtension(void) const | 
 | 570 | { | 
 | 571 |     char* ext; | 
 | 572 |  | 
 | 573 |     ext = find_extension(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 574 |     if (ext != nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 |         return String8(ext); | 
 | 576 |     else | 
 | 577 |         return String8(""); | 
 | 578 | } | 
 | 579 |  | 
 | 580 | String8 String8::getBasePath(void) const | 
 | 581 | { | 
 | 582 |     char* ext; | 
 | 583 |     const char* const str = mString; | 
 | 584 |  | 
 | 585 |     ext = find_extension(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 586 |     if (ext == nullptr) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 |         return String8(*this); | 
 | 588 |     else | 
 | 589 |         return String8(str, ext - str); | 
 | 590 | } | 
 | 591 |  | 
 | 592 | String8& String8::appendPath(const char* name) | 
 | 593 | { | 
 | 594 |     // TODO: The test below will fail for Win32 paths. Fix later or ignore. | 
 | 595 |     if (name[0] != OS_PATH_SEPARATOR) { | 
 | 596 |         if (*name == '\0') { | 
 | 597 |             // nothing to do | 
 | 598 |             return *this; | 
 | 599 |         } | 
 | 600 |  | 
 | 601 |         size_t len = length(); | 
 | 602 |         if (len == 0) { | 
 | 603 |             // no existing filename, just use the new one | 
 | 604 |             setPathName(name); | 
 | 605 |             return *this; | 
 | 606 |         } | 
 | 607 |  | 
 | 608 |         // make room for oldPath + '/' + newPath | 
 | 609 |         int newlen = strlen(name); | 
 | 610 |  | 
 | 611 |         char* buf = lockBuffer(len+1+newlen); | 
 | 612 |  | 
 | 613 |         // insert a '/' if needed | 
 | 614 |         if (buf[len-1] != OS_PATH_SEPARATOR) | 
 | 615 |             buf[len++] = OS_PATH_SEPARATOR; | 
 | 616 |  | 
 | 617 |         memcpy(buf+len, name, newlen+1); | 
 | 618 |         len += newlen; | 
 | 619 |  | 
 | 620 |         unlockBuffer(len); | 
 | 621 |  | 
 | 622 |         return *this; | 
 | 623 |     } else { | 
 | 624 |         setPathName(name); | 
 | 625 |         return *this; | 
 | 626 |     } | 
 | 627 | } | 
 | 628 |  | 
 | 629 | String8& String8::convertToResPath() | 
 | 630 | { | 
 | 631 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR | 
 | 632 |     size_t len = length(); | 
 | 633 |     if (len > 0) { | 
 | 634 |         char * buf = lockBuffer(len); | 
 | 635 |         for (char * end = buf + len; buf < end; ++buf) { | 
 | 636 |             if (*buf == OS_PATH_SEPARATOR) | 
 | 637 |                 *buf = RES_PATH_SEPARATOR; | 
 | 638 |         } | 
 | 639 |         unlockBuffer(len); | 
 | 640 |     } | 
 | 641 | #endif | 
 | 642 |     return *this; | 
 | 643 | } | 
 | 644 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | }; // namespace android |