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