The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <utils/String8.h> |
| 18 | |
Elliott Hughes | 1f8bc86 | 2015-07-29 14:02:29 -0700 | [diff] [blame] | 19 | #include <utils/Compat.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 21 | #include <utils/Unicode.h> |
| 22 | #include <utils/SharedBuffer.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/String16.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/threads.h> |
| 25 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 28 | /* |
| 29 | * Functions outside android is below the namespace android, since they use |
| 30 | * functions and constants in android namespace. |
| 31 | */ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 35 | namespace android { |
| 36 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | // Separator used by resource paths. This is not platform dependent contrary |
| 38 | // to OS_PATH_SEPARATOR. |
| 39 | #define RES_PATH_SEPARATOR '/' |
| 40 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | static SharedBuffer* gEmptyStringBuf = NULL; |
| 42 | static char* gEmptyString = NULL; |
| 43 | |
| 44 | extern int gDarwinCantLoadAllObjects; |
| 45 | int gDarwinIsReallyAnnoying; |
| 46 | |
Mathias Agopian | 9eb2a3b | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 47 | void initialize_string8(); |
| 48 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | static inline char* getEmptyString() |
| 50 | { |
| 51 | gEmptyStringBuf->acquire(); |
| 52 | return gEmptyString; |
| 53 | } |
| 54 | |
| 55 | void initialize_string8() |
| 56 | { |
Dan Egnor | 88753ae | 2010-05-06 00:55:09 -0700 | [diff] [blame] | 57 | // HACK: This dummy dependency forces linking libutils Static.cpp, |
| 58 | // which is needed to initialize String8/String16 classes. |
| 59 | // These variables are named for Darwin, but are needed elsewhere too, |
| 60 | // including static linking on any platform. |
| 61 | gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 62 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | SharedBuffer* buf = SharedBuffer::alloc(1); |
| 64 | char* str = (char*)buf->data(); |
| 65 | *str = 0; |
| 66 | gEmptyStringBuf = buf; |
| 67 | gEmptyString = str; |
| 68 | } |
| 69 | |
| 70 | void terminate_string8() |
| 71 | { |
| 72 | SharedBuffer::bufferFromData(gEmptyString)->release(); |
| 73 | gEmptyStringBuf = NULL; |
| 74 | gEmptyString = NULL; |
| 75 | } |
| 76 | |
| 77 | // --------------------------------------------------------------------------- |
| 78 | |
| 79 | static char* allocFromUTF8(const char* in, size_t len) |
| 80 | { |
| 81 | if (len > 0) { |
Sergio Giro | 4eeacbe | 2015-08-18 14:44:54 +0100 | [diff] [blame] | 82 | if (len == SIZE_MAX) { |
| 83 | return NULL; |
| 84 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | SharedBuffer* buf = SharedBuffer::alloc(len+1); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 86 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | if (buf) { |
| 88 | char* str = (char*)buf->data(); |
| 89 | memcpy(str, in, len); |
| 90 | str[len] = 0; |
| 91 | return str; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | return getEmptyString(); |
| 97 | } |
| 98 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 99 | static char* allocFromUTF16(const char16_t* in, size_t len) |
| 100 | { |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 101 | if (len == 0) return getEmptyString(); |
| 102 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 103 | const ssize_t bytes = utf16_to_utf8_length(in, len); |
| 104 | if (bytes < 0) { |
| 105 | return getEmptyString(); |
| 106 | } |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 107 | |
| 108 | SharedBuffer* buf = SharedBuffer::alloc(bytes+1); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 109 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 110 | if (!buf) { |
| 111 | return getEmptyString(); |
Kenny Root | 9a2d83e | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 114 | char* str = (char*)buf->data(); |
| 115 | utf16_to_utf8(in, len, str); |
| 116 | return str; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static char* allocFromUTF32(const char32_t* in, size_t len) |
| 120 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 121 | if (len == 0) { |
| 122 | return getEmptyString(); |
| 123 | } |
| 124 | |
| 125 | const ssize_t bytes = utf32_to_utf8_length(in, len); |
| 126 | if (bytes < 0) { |
| 127 | return getEmptyString(); |
| 128 | } |
| 129 | |
| 130 | SharedBuffer* buf = SharedBuffer::alloc(bytes+1); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 131 | ALOG_ASSERT(buf, "Unable to allocate shared buffer"); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 132 | if (!buf) { |
| 133 | return getEmptyString(); |
| 134 | } |
| 135 | |
| 136 | char* str = (char*) buf->data(); |
| 137 | utf32_to_utf8(in, len, str); |
| 138 | |
| 139 | return str; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 140 | } |
| 141 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | // --------------------------------------------------------------------------- |
| 143 | |
| 144 | String8::String8() |
| 145 | : mString(getEmptyString()) |
| 146 | { |
| 147 | } |
| 148 | |
Mathias Agopian | 4485d0d | 2013-05-08 16:04:13 -0700 | [diff] [blame] | 149 | String8::String8(StaticLinkage) |
| 150 | : mString(0) |
| 151 | { |
| 152 | // this constructor is used when we can't rely on the static-initializers |
| 153 | // having run. In this case we always allocate an empty string. It's less |
| 154 | // efficient than using getEmptyString(), but we assume it's uncommon. |
| 155 | |
| 156 | char* data = static_cast<char*>( |
| 157 | SharedBuffer::alloc(sizeof(char))->data()); |
| 158 | data[0] = 0; |
| 159 | mString = data; |
| 160 | } |
| 161 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | String8::String8(const String8& o) |
| 163 | : mString(o.mString) |
| 164 | { |
| 165 | SharedBuffer::bufferFromData(mString)->acquire(); |
| 166 | } |
| 167 | |
| 168 | String8::String8(const char* o) |
| 169 | : mString(allocFromUTF8(o, strlen(o))) |
| 170 | { |
| 171 | if (mString == NULL) { |
| 172 | mString = getEmptyString(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | String8::String8(const char* o, size_t len) |
| 177 | : mString(allocFromUTF8(o, len)) |
| 178 | { |
| 179 | if (mString == NULL) { |
| 180 | mString = getEmptyString(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | String8::String8(const String16& o) |
| 185 | : mString(allocFromUTF16(o.string(), o.size())) |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | String8::String8(const char16_t* o) |
| 190 | : mString(allocFromUTF16(o, strlen16(o))) |
| 191 | { |
| 192 | } |
| 193 | |
| 194 | String8::String8(const char16_t* o, size_t len) |
| 195 | : mString(allocFromUTF16(o, len)) |
| 196 | { |
| 197 | } |
| 198 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 199 | String8::String8(const char32_t* o) |
| 200 | : mString(allocFromUTF32(o, strlen32(o))) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | String8::String8(const char32_t* o, size_t len) |
| 205 | : mString(allocFromUTF32(o, len)) |
| 206 | { |
| 207 | } |
| 208 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | String8::~String8() |
| 210 | { |
| 211 | SharedBuffer::bufferFromData(mString)->release(); |
| 212 | } |
| 213 | |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 214 | String8 String8::format(const char* fmt, ...) |
| 215 | { |
| 216 | va_list args; |
| 217 | va_start(args, fmt); |
| 218 | |
| 219 | String8 result(formatV(fmt, args)); |
| 220 | |
| 221 | va_end(args); |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | String8 String8::formatV(const char* fmt, va_list args) |
| 226 | { |
| 227 | String8 result; |
| 228 | result.appendFormatV(fmt, args); |
| 229 | return result; |
| 230 | } |
| 231 | |
Jeff Brown | 48da31b | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 232 | void String8::clear() { |
| 233 | SharedBuffer::bufferFromData(mString)->release(); |
| 234 | mString = getEmptyString(); |
| 235 | } |
| 236 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | void String8::setTo(const String8& other) |
| 238 | { |
| 239 | SharedBuffer::bufferFromData(other.mString)->acquire(); |
| 240 | SharedBuffer::bufferFromData(mString)->release(); |
| 241 | mString = other.mString; |
| 242 | } |
| 243 | |
| 244 | status_t String8::setTo(const char* other) |
| 245 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 246 | const char *newString = allocFromUTF8(other, strlen(other)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 248 | mString = newString; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | if (mString) return NO_ERROR; |
| 250 | |
| 251 | mString = getEmptyString(); |
| 252 | return NO_MEMORY; |
| 253 | } |
| 254 | |
| 255 | status_t String8::setTo(const char* other, size_t len) |
| 256 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 257 | const char *newString = allocFromUTF8(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 259 | mString = newString; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | if (mString) return NO_ERROR; |
| 261 | |
| 262 | mString = getEmptyString(); |
| 263 | return NO_MEMORY; |
| 264 | } |
| 265 | |
| 266 | status_t String8::setTo(const char16_t* other, size_t len) |
| 267 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 268 | const char *newString = allocFromUTF16(other, len); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 270 | mString = newString; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | if (mString) return NO_ERROR; |
| 272 | |
| 273 | mString = getEmptyString(); |
| 274 | return NO_MEMORY; |
| 275 | } |
| 276 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 277 | status_t String8::setTo(const char32_t* other, size_t len) |
| 278 | { |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 279 | const char *newString = allocFromUTF32(other, len); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 280 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | 10e5da5 | 2010-06-10 11:14:26 -0700 | [diff] [blame] | 281 | mString = newString; |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 282 | if (mString) return NO_ERROR; |
| 283 | |
| 284 | mString = getEmptyString(); |
| 285 | return NO_MEMORY; |
| 286 | } |
| 287 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | status_t String8::append(const String8& other) |
| 289 | { |
| 290 | const size_t otherLen = other.bytes(); |
| 291 | if (bytes() == 0) { |
| 292 | setTo(other); |
| 293 | return NO_ERROR; |
| 294 | } else if (otherLen == 0) { |
| 295 | return NO_ERROR; |
| 296 | } |
| 297 | |
| 298 | return real_append(other.string(), otherLen); |
| 299 | } |
| 300 | |
| 301 | status_t String8::append(const char* other) |
| 302 | { |
| 303 | return append(other, strlen(other)); |
| 304 | } |
| 305 | |
| 306 | status_t String8::append(const char* other, size_t otherLen) |
| 307 | { |
| 308 | if (bytes() == 0) { |
| 309 | return setTo(other, otherLen); |
| 310 | } else if (otherLen == 0) { |
| 311 | return NO_ERROR; |
| 312 | } |
| 313 | |
| 314 | return real_append(other, otherLen); |
| 315 | } |
| 316 | |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 317 | status_t String8::appendFormat(const char* fmt, ...) |
| 318 | { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 319 | va_list args; |
| 320 | va_start(args, fmt); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 321 | |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 322 | status_t result = appendFormatV(fmt, args); |
| 323 | |
| 324 | va_end(args); |
| 325 | return result; |
| 326 | } |
| 327 | |
| 328 | status_t String8::appendFormatV(const char* fmt, va_list args) |
| 329 | { |
Fengwei Yin | fff9d11 | 2014-02-27 01:17:09 +0800 | [diff] [blame] | 330 | int n, result = NO_ERROR; |
| 331 | va_list tmp_args; |
| 332 | |
| 333 | /* args is undefined after vsnprintf. |
| 334 | * So we need a copy here to avoid the |
| 335 | * second vsnprintf access undefined args. |
| 336 | */ |
| 337 | va_copy(tmp_args, args); |
| 338 | n = vsnprintf(NULL, 0, fmt, tmp_args); |
| 339 | va_end(tmp_args); |
| 340 | |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 341 | if (n != 0) { |
| 342 | size_t oldLength = length(); |
| 343 | char* buf = lockBuffer(oldLength + n); |
| 344 | if (buf) { |
Jeff Brown | 647925d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 345 | vsnprintf(buf + oldLength, n + 1, fmt, args); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 346 | } else { |
| 347 | result = NO_MEMORY; |
| 348 | } |
| 349 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 350 | return result; |
| 351 | } |
| 352 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | status_t String8::real_append(const char* other, size_t otherLen) |
| 354 | { |
| 355 | const size_t myLen = bytes(); |
| 356 | |
| 357 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 358 | ->editResize(myLen+otherLen+1); |
| 359 | if (buf) { |
| 360 | char* str = (char*)buf->data(); |
| 361 | mString = str; |
| 362 | str += myLen; |
| 363 | memcpy(str, other, otherLen); |
| 364 | str[otherLen] = '\0'; |
| 365 | return NO_ERROR; |
| 366 | } |
| 367 | return NO_MEMORY; |
| 368 | } |
| 369 | |
| 370 | char* String8::lockBuffer(size_t size) |
| 371 | { |
| 372 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 373 | ->editResize(size+1); |
| 374 | if (buf) { |
| 375 | char* str = (char*)buf->data(); |
| 376 | mString = str; |
| 377 | return str; |
| 378 | } |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | void String8::unlockBuffer() |
| 383 | { |
| 384 | unlockBuffer(strlen(mString)); |
| 385 | } |
| 386 | |
| 387 | status_t String8::unlockBuffer(size_t size) |
| 388 | { |
| 389 | if (size != this->size()) { |
| 390 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 391 | ->editResize(size+1); |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 392 | if (! buf) { |
| 393 | return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 395 | |
| 396 | char* str = (char*)buf->data(); |
| 397 | str[size] = 0; |
| 398 | mString = str; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
Jeff Brown | 35a154e | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 400 | |
| 401 | return NO_ERROR; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | ssize_t String8::find(const char* other, size_t start) const |
| 405 | { |
| 406 | size_t len = size(); |
| 407 | if (start >= len) { |
| 408 | return -1; |
| 409 | } |
| 410 | const char* s = mString+start; |
| 411 | const char* p = strstr(s, other); |
| 412 | return p ? p-mString : -1; |
| 413 | } |
| 414 | |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 415 | bool String8::removeAll(const char* other) { |
| 416 | ssize_t index = find(other); |
| 417 | if (index < 0) return false; |
| 418 | |
| 419 | char* buf = lockBuffer(size()); |
| 420 | if (!buf) return false; // out of memory |
| 421 | |
| 422 | size_t skip = strlen(other); |
| 423 | size_t len = size(); |
| 424 | size_t tail = index; |
| 425 | while (size_t(index) < len) { |
| 426 | ssize_t next = find(other, index + skip); |
| 427 | if (next < 0) { |
| 428 | next = len; |
| 429 | } |
| 430 | |
Andreas Gampe | dd060f0 | 2014-11-13 15:50:17 -0800 | [diff] [blame] | 431 | memmove(buf + tail, buf + index + skip, next - index - skip); |
Jeff Brown | 5ee915a | 2014-06-06 19:30:15 -0700 | [diff] [blame] | 432 | tail += next - index - skip; |
| 433 | index = next; |
| 434 | } |
| 435 | unlockBuffer(tail); |
| 436 | return true; |
| 437 | } |
| 438 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | void String8::toLower() |
| 440 | { |
| 441 | toLower(0, size()); |
| 442 | } |
| 443 | |
| 444 | void String8::toLower(size_t start, size_t length) |
| 445 | { |
| 446 | const size_t len = size(); |
| 447 | if (start >= len) { |
| 448 | return; |
| 449 | } |
| 450 | if (start+length > len) { |
| 451 | length = len-start; |
| 452 | } |
| 453 | char* buf = lockBuffer(len); |
| 454 | buf += start; |
| 455 | while (length > 0) { |
| 456 | *buf = tolower(*buf); |
| 457 | buf++; |
| 458 | length--; |
| 459 | } |
| 460 | unlockBuffer(len); |
| 461 | } |
| 462 | |
| 463 | void String8::toUpper() |
| 464 | { |
| 465 | toUpper(0, size()); |
| 466 | } |
| 467 | |
| 468 | void String8::toUpper(size_t start, size_t length) |
| 469 | { |
| 470 | const size_t len = size(); |
| 471 | if (start >= len) { |
| 472 | return; |
| 473 | } |
| 474 | if (start+length > len) { |
| 475 | length = len-start; |
| 476 | } |
| 477 | char* buf = lockBuffer(len); |
| 478 | buf += start; |
| 479 | while (length > 0) { |
| 480 | *buf = toupper(*buf); |
| 481 | buf++; |
| 482 | length--; |
| 483 | } |
| 484 | unlockBuffer(len); |
| 485 | } |
| 486 | |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 487 | size_t String8::getUtf32Length() const |
| 488 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 489 | return utf8_to_utf32_length(mString, length()); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int32_t String8::getUtf32At(size_t index, size_t *next_index) const |
| 493 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 494 | return utf32_from_utf8_at(mString, length(), index, next_index); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 497 | void String8::getUtf32(char32_t* dst) const |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 498 | { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 499 | utf8_to_utf32(mString, length(), dst); |
Daisuke Miyakawa | 44dad3e | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 500 | } |
| 501 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | // --------------------------------------------------------------------------- |
| 503 | // Path functions |
| 504 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | void String8::setPathName(const char* name) |
| 506 | { |
| 507 | setPathName(name, strlen(name)); |
| 508 | } |
| 509 | |
| 510 | void String8::setPathName(const char* name, size_t len) |
| 511 | { |
| 512 | char* buf = lockBuffer(len); |
| 513 | |
| 514 | memcpy(buf, name, len); |
| 515 | |
| 516 | // remove trailing path separator, if present |
| 517 | if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR) |
| 518 | len--; |
| 519 | |
| 520 | buf[len] = '\0'; |
| 521 | |
| 522 | unlockBuffer(len); |
| 523 | } |
| 524 | |
| 525 | String8 String8::getPathLeaf(void) const |
| 526 | { |
| 527 | const char* cp; |
| 528 | const char*const buf = mString; |
| 529 | |
| 530 | cp = strrchr(buf, OS_PATH_SEPARATOR); |
| 531 | if (cp == NULL) |
| 532 | return String8(*this); |
| 533 | else |
| 534 | return String8(cp+1); |
| 535 | } |
| 536 | |
| 537 | String8 String8::getPathDir(void) const |
| 538 | { |
| 539 | const char* cp; |
| 540 | const char*const str = mString; |
| 541 | |
| 542 | cp = strrchr(str, OS_PATH_SEPARATOR); |
| 543 | if (cp == NULL) |
| 544 | return String8(""); |
| 545 | else |
| 546 | return String8(str, cp - str); |
| 547 | } |
| 548 | |
| 549 | String8 String8::walkPath(String8* outRemains) const |
| 550 | { |
| 551 | const char* cp; |
| 552 | const char*const str = mString; |
| 553 | const char* buf = str; |
| 554 | |
| 555 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 556 | if (cp == buf) { |
| 557 | // don't include a leading '/'. |
| 558 | buf = buf+1; |
| 559 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 560 | } |
| 561 | |
| 562 | if (cp == NULL) { |
| 563 | String8 res = buf != str ? String8(buf) : *this; |
| 564 | if (outRemains) *outRemains = String8(""); |
| 565 | return res; |
| 566 | } |
| 567 | |
| 568 | String8 res(buf, cp-buf); |
| 569 | if (outRemains) *outRemains = String8(cp+1); |
| 570 | return res; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Helper function for finding the start of an extension in a pathname. |
| 575 | * |
| 576 | * Returns a pointer inside mString, or NULL if no extension was found. |
| 577 | */ |
| 578 | char* String8::find_extension(void) const |
| 579 | { |
| 580 | const char* lastSlash; |
| 581 | const char* lastDot; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | const char* const str = mString; |
| 583 | |
| 584 | // only look at the filename |
| 585 | lastSlash = strrchr(str, OS_PATH_SEPARATOR); |
| 586 | if (lastSlash == NULL) |
| 587 | lastSlash = str; |
| 588 | else |
| 589 | lastSlash++; |
| 590 | |
| 591 | // find the last dot |
| 592 | lastDot = strrchr(lastSlash, '.'); |
| 593 | if (lastDot == NULL) |
| 594 | return NULL; |
| 595 | |
| 596 | // looks good, ship it |
| 597 | return const_cast<char*>(lastDot); |
| 598 | } |
| 599 | |
| 600 | String8 String8::getPathExtension(void) const |
| 601 | { |
| 602 | char* ext; |
| 603 | |
| 604 | ext = find_extension(); |
| 605 | if (ext != NULL) |
| 606 | return String8(ext); |
| 607 | else |
| 608 | return String8(""); |
| 609 | } |
| 610 | |
| 611 | String8 String8::getBasePath(void) const |
| 612 | { |
| 613 | char* ext; |
| 614 | const char* const str = mString; |
| 615 | |
| 616 | ext = find_extension(); |
| 617 | if (ext == NULL) |
| 618 | return String8(*this); |
| 619 | else |
| 620 | return String8(str, ext - str); |
| 621 | } |
| 622 | |
| 623 | String8& String8::appendPath(const char* name) |
| 624 | { |
| 625 | // TODO: The test below will fail for Win32 paths. Fix later or ignore. |
| 626 | if (name[0] != OS_PATH_SEPARATOR) { |
| 627 | if (*name == '\0') { |
| 628 | // nothing to do |
| 629 | return *this; |
| 630 | } |
| 631 | |
| 632 | size_t len = length(); |
| 633 | if (len == 0) { |
| 634 | // no existing filename, just use the new one |
| 635 | setPathName(name); |
| 636 | return *this; |
| 637 | } |
| 638 | |
| 639 | // make room for oldPath + '/' + newPath |
| 640 | int newlen = strlen(name); |
| 641 | |
| 642 | char* buf = lockBuffer(len+1+newlen); |
| 643 | |
| 644 | // insert a '/' if needed |
| 645 | if (buf[len-1] != OS_PATH_SEPARATOR) |
| 646 | buf[len++] = OS_PATH_SEPARATOR; |
| 647 | |
| 648 | memcpy(buf+len, name, newlen+1); |
| 649 | len += newlen; |
| 650 | |
| 651 | unlockBuffer(len); |
| 652 | |
| 653 | return *this; |
| 654 | } else { |
| 655 | setPathName(name); |
| 656 | return *this; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | String8& String8::convertToResPath() |
| 661 | { |
| 662 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR |
| 663 | size_t len = length(); |
| 664 | if (len > 0) { |
| 665 | char * buf = lockBuffer(len); |
| 666 | for (char * end = buf + len; buf < end; ++buf) { |
| 667 | if (*buf == OS_PATH_SEPARATOR) |
| 668 | *buf = RES_PATH_SEPARATOR; |
| 669 | } |
| 670 | unlockBuffer(len); |
| 671 | } |
| 672 | #endif |
| 673 | return *this; |
| 674 | } |
| 675 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | }; // namespace android |