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