The Android Open Source Project | edbf3b6 | 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> |
| 20 | #include <utils/String16.h> |
| 21 | #include <utils/TextOutput.h> |
| 22 | #include <utils/threads.h> |
| 23 | |
| 24 | #include <private/utils/Static.h> |
| 25 | |
| 26 | #include <ctype.h> |
| 27 | |
Daisuke Miyakawa | 9f22024 | 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 | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | static const char32_t kByteMask = 0x000000BF; |
| 38 | static const char32_t kByteMark = 0x00000080; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | // Surrogates aren't valid for UTF-32 characters, so define some |
| 41 | // constants that will let us screen them out. |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 42 | static const char32_t kUnicodeSurrogateHighStart = 0x0000D800; |
| 43 | static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF; |
| 44 | static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00; |
| 45 | static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF; |
| 46 | static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart; |
| 47 | static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd; |
| 48 | static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
| 50 | // Mask used to set appropriate bits in first byte of UTF-8 sequence, |
| 51 | // indexed by number of bytes in the sequence. |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 52 | // 0xxxxxxx |
| 53 | // -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000 |
| 54 | // 110yyyyx 10xxxxxx |
| 55 | // -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0 |
| 56 | // 1110yyyy 10yxxxxx 10xxxxxx |
| 57 | // -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0 |
| 58 | // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx |
| 59 | // -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0 |
| 60 | static const char32_t kFirstByteMark[] = { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0 |
| 62 | }; |
| 63 | |
| 64 | // Separator used by resource paths. This is not platform dependent contrary |
| 65 | // to OS_PATH_SEPARATOR. |
| 66 | #define RES_PATH_SEPARATOR '/' |
| 67 | |
| 68 | // Return number of utf8 bytes required for the character. |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 69 | static size_t utf32_to_utf8_bytes(char32_t srcChar) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | { |
| 71 | size_t bytesToWrite; |
| 72 | |
| 73 | // Figure out how many bytes the result will require. |
| 74 | if (srcChar < 0x00000080) |
| 75 | { |
| 76 | bytesToWrite = 1; |
| 77 | } |
| 78 | else if (srcChar < 0x00000800) |
| 79 | { |
| 80 | bytesToWrite = 2; |
| 81 | } |
| 82 | else if (srcChar < 0x00010000) |
| 83 | { |
| 84 | if ((srcChar < kUnicodeSurrogateStart) |
| 85 | || (srcChar > kUnicodeSurrogateEnd)) |
| 86 | { |
| 87 | bytesToWrite = 3; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // Surrogates are invalid UTF-32 characters. |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | // Max code point for Unicode is 0x0010FFFF. |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 96 | else if (srcChar <= kUnicodeMaxCodepoint) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | { |
| 98 | bytesToWrite = 4; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | // Invalid UTF-32 character. |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | return bytesToWrite; |
| 107 | } |
| 108 | |
| 109 | // Write out the source character to <dstP>. |
| 110 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 111 | static void utf32_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | { |
| 113 | dstP += bytes; |
| 114 | switch (bytes) |
| 115 | { /* note: everything falls through. */ |
| 116 | case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 117 | case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 118 | case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 119 | case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // --------------------------------------------------------------------------- |
| 124 | |
| 125 | static SharedBuffer* gEmptyStringBuf = NULL; |
| 126 | static char* gEmptyString = NULL; |
| 127 | |
| 128 | extern int gDarwinCantLoadAllObjects; |
| 129 | int gDarwinIsReallyAnnoying; |
| 130 | |
| 131 | static inline char* getEmptyString() |
| 132 | { |
| 133 | gEmptyStringBuf->acquire(); |
| 134 | return gEmptyString; |
| 135 | } |
| 136 | |
| 137 | void initialize_string8() |
| 138 | { |
Dan Egnor | 386a332 | 2010-05-06 00:55:09 -0700 | [diff] [blame] | 139 | // HACK: This dummy dependency forces linking libutils Static.cpp, |
| 140 | // which is needed to initialize String8/String16 classes. |
| 141 | // These variables are named for Darwin, but are needed elsewhere too, |
| 142 | // including static linking on any platform. |
| 143 | gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects; |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 144 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | SharedBuffer* buf = SharedBuffer::alloc(1); |
| 146 | char* str = (char*)buf->data(); |
| 147 | *str = 0; |
| 148 | gEmptyStringBuf = buf; |
| 149 | gEmptyString = str; |
| 150 | } |
| 151 | |
| 152 | void terminate_string8() |
| 153 | { |
| 154 | SharedBuffer::bufferFromData(gEmptyString)->release(); |
| 155 | gEmptyStringBuf = NULL; |
| 156 | gEmptyString = NULL; |
| 157 | } |
| 158 | |
| 159 | // --------------------------------------------------------------------------- |
| 160 | |
| 161 | static char* allocFromUTF8(const char* in, size_t len) |
| 162 | { |
| 163 | if (len > 0) { |
| 164 | SharedBuffer* buf = SharedBuffer::alloc(len+1); |
| 165 | LOG_ASSERT(buf, "Unable to allocate shared buffer"); |
| 166 | if (buf) { |
| 167 | char* str = (char*)buf->data(); |
| 168 | memcpy(str, in, len); |
| 169 | str[len] = 0; |
| 170 | return str; |
| 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | return getEmptyString(); |
| 176 | } |
| 177 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 178 | template<typename T, typename L> |
| 179 | static char* allocFromUTF16OrUTF32(const T* in, L len) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | { |
| 181 | if (len == 0) return getEmptyString(); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 182 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | size_t bytes = 0; |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 184 | const T* end = in+len; |
| 185 | const T* p = in; |
| 186 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | while (p < end) { |
| 188 | bytes += utf32_to_utf8_bytes(*p); |
| 189 | p++; |
| 190 | } |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 191 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | SharedBuffer* buf = SharedBuffer::alloc(bytes+1); |
| 193 | LOG_ASSERT(buf, "Unable to allocate shared buffer"); |
| 194 | if (buf) { |
| 195 | p = in; |
| 196 | char* str = (char*)buf->data(); |
| 197 | char* d = str; |
| 198 | while (p < end) { |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 199 | const T c = *p++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | size_t len = utf32_to_utf8_bytes(c); |
| 201 | utf32_to_utf8((uint8_t*)d, c, len); |
| 202 | d += len; |
| 203 | } |
| 204 | *d = 0; |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 205 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | return str; |
| 207 | } |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 208 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | return getEmptyString(); |
| 210 | } |
| 211 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 212 | static char* allocFromUTF16(const char16_t* in, size_t len) |
| 213 | { |
Kenny Root | 92f5984 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 214 | if (len == 0) return getEmptyString(); |
| 215 | |
| 216 | const size_t bytes = utf8_length_from_utf16(in, len); |
| 217 | |
| 218 | SharedBuffer* buf = SharedBuffer::alloc(bytes+1); |
| 219 | LOG_ASSERT(buf, "Unable to allocate shared buffer"); |
| 220 | if (buf) { |
| 221 | char* str = (char*)buf->data(); |
| 222 | |
| 223 | utf16_to_utf8(in, len, str, bytes+1); |
| 224 | |
| 225 | return str; |
| 226 | } |
| 227 | |
| 228 | return getEmptyString(); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static char* allocFromUTF32(const char32_t* in, size_t len) |
| 232 | { |
| 233 | return allocFromUTF16OrUTF32<char32_t, size_t>(in, len); |
| 234 | } |
| 235 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | // --------------------------------------------------------------------------- |
| 237 | |
| 238 | String8::String8() |
| 239 | : mString(getEmptyString()) |
| 240 | { |
| 241 | } |
| 242 | |
| 243 | String8::String8(const String8& o) |
| 244 | : mString(o.mString) |
| 245 | { |
| 246 | SharedBuffer::bufferFromData(mString)->acquire(); |
| 247 | } |
| 248 | |
| 249 | String8::String8(const char* o) |
| 250 | : mString(allocFromUTF8(o, strlen(o))) |
| 251 | { |
| 252 | if (mString == NULL) { |
| 253 | mString = getEmptyString(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | String8::String8(const char* o, size_t len) |
| 258 | : mString(allocFromUTF8(o, len)) |
| 259 | { |
| 260 | if (mString == NULL) { |
| 261 | mString = getEmptyString(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | String8::String8(const String16& o) |
| 266 | : mString(allocFromUTF16(o.string(), o.size())) |
| 267 | { |
| 268 | } |
| 269 | |
| 270 | String8::String8(const char16_t* o) |
| 271 | : mString(allocFromUTF16(o, strlen16(o))) |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | String8::String8(const char16_t* o, size_t len) |
| 276 | : mString(allocFromUTF16(o, len)) |
| 277 | { |
| 278 | } |
| 279 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 280 | String8::String8(const char32_t* o) |
| 281 | : mString(allocFromUTF32(o, strlen32(o))) |
| 282 | { |
| 283 | } |
| 284 | |
| 285 | String8::String8(const char32_t* o, size_t len) |
| 286 | : mString(allocFromUTF32(o, len)) |
| 287 | { |
| 288 | } |
| 289 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | String8::~String8() |
| 291 | { |
| 292 | SharedBuffer::bufferFromData(mString)->release(); |
| 293 | } |
| 294 | |
| 295 | void String8::setTo(const String8& other) |
| 296 | { |
| 297 | SharedBuffer::bufferFromData(other.mString)->acquire(); |
| 298 | SharedBuffer::bufferFromData(mString)->release(); |
| 299 | mString = other.mString; |
| 300 | } |
| 301 | |
| 302 | status_t String8::setTo(const char* other) |
| 303 | { |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 304 | const char *newString = allocFromUTF8(other, strlen(other)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 306 | mString = newString; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | if (mString) return NO_ERROR; |
| 308 | |
| 309 | mString = getEmptyString(); |
| 310 | return NO_MEMORY; |
| 311 | } |
| 312 | |
| 313 | status_t String8::setTo(const char* other, size_t len) |
| 314 | { |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 315 | const char *newString = allocFromUTF8(other, len); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 317 | mString = newString; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | if (mString) return NO_ERROR; |
| 319 | |
| 320 | mString = getEmptyString(); |
| 321 | return NO_MEMORY; |
| 322 | } |
| 323 | |
| 324 | status_t String8::setTo(const char16_t* other, size_t len) |
| 325 | { |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 326 | const char *newString = allocFromUTF16(other, len); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 328 | mString = newString; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | if (mString) return NO_ERROR; |
| 330 | |
| 331 | mString = getEmptyString(); |
| 332 | return NO_MEMORY; |
| 333 | } |
| 334 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 335 | status_t String8::setTo(const char32_t* other, size_t len) |
| 336 | { |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 337 | const char *newString = allocFromUTF32(other, len); |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 338 | SharedBuffer::bufferFromData(mString)->release(); |
Andreas Huber | a637eb4 | 2010-06-10 11:14:26 -0700 | [diff] [blame^] | 339 | mString = newString; |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 340 | if (mString) return NO_ERROR; |
| 341 | |
| 342 | mString = getEmptyString(); |
| 343 | return NO_MEMORY; |
| 344 | } |
| 345 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | status_t String8::append(const String8& other) |
| 347 | { |
| 348 | const size_t otherLen = other.bytes(); |
| 349 | if (bytes() == 0) { |
| 350 | setTo(other); |
| 351 | return NO_ERROR; |
| 352 | } else if (otherLen == 0) { |
| 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
| 356 | return real_append(other.string(), otherLen); |
| 357 | } |
| 358 | |
| 359 | status_t String8::append(const char* other) |
| 360 | { |
| 361 | return append(other, strlen(other)); |
| 362 | } |
| 363 | |
| 364 | status_t String8::append(const char* other, size_t otherLen) |
| 365 | { |
| 366 | if (bytes() == 0) { |
| 367 | return setTo(other, otherLen); |
| 368 | } else if (otherLen == 0) { |
| 369 | return NO_ERROR; |
| 370 | } |
| 371 | |
| 372 | return real_append(other, otherLen); |
| 373 | } |
| 374 | |
| 375 | status_t String8::real_append(const char* other, size_t otherLen) |
| 376 | { |
| 377 | const size_t myLen = bytes(); |
| 378 | |
| 379 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 380 | ->editResize(myLen+otherLen+1); |
| 381 | if (buf) { |
| 382 | char* str = (char*)buf->data(); |
| 383 | mString = str; |
| 384 | str += myLen; |
| 385 | memcpy(str, other, otherLen); |
| 386 | str[otherLen] = '\0'; |
| 387 | return NO_ERROR; |
| 388 | } |
| 389 | return NO_MEMORY; |
| 390 | } |
| 391 | |
| 392 | char* String8::lockBuffer(size_t size) |
| 393 | { |
| 394 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 395 | ->editResize(size+1); |
| 396 | if (buf) { |
| 397 | char* str = (char*)buf->data(); |
| 398 | mString = str; |
| 399 | return str; |
| 400 | } |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | void String8::unlockBuffer() |
| 405 | { |
| 406 | unlockBuffer(strlen(mString)); |
| 407 | } |
| 408 | |
| 409 | status_t String8::unlockBuffer(size_t size) |
| 410 | { |
| 411 | if (size != this->size()) { |
| 412 | SharedBuffer* buf = SharedBuffer::bufferFromData(mString) |
| 413 | ->editResize(size+1); |
| 414 | if (buf) { |
| 415 | char* str = (char*)buf->data(); |
| 416 | str[size] = 0; |
| 417 | mString = str; |
| 418 | return NO_ERROR; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return NO_MEMORY; |
| 423 | } |
| 424 | |
| 425 | ssize_t String8::find(const char* other, size_t start) const |
| 426 | { |
| 427 | size_t len = size(); |
| 428 | if (start >= len) { |
| 429 | return -1; |
| 430 | } |
| 431 | const char* s = mString+start; |
| 432 | const char* p = strstr(s, other); |
| 433 | return p ? p-mString : -1; |
| 434 | } |
| 435 | |
| 436 | void String8::toLower() |
| 437 | { |
| 438 | toLower(0, size()); |
| 439 | } |
| 440 | |
| 441 | void String8::toLower(size_t start, size_t length) |
| 442 | { |
| 443 | const size_t len = size(); |
| 444 | if (start >= len) { |
| 445 | return; |
| 446 | } |
| 447 | if (start+length > len) { |
| 448 | length = len-start; |
| 449 | } |
| 450 | char* buf = lockBuffer(len); |
| 451 | buf += start; |
| 452 | while (length > 0) { |
| 453 | *buf = tolower(*buf); |
| 454 | buf++; |
| 455 | length--; |
| 456 | } |
| 457 | unlockBuffer(len); |
| 458 | } |
| 459 | |
| 460 | void String8::toUpper() |
| 461 | { |
| 462 | toUpper(0, size()); |
| 463 | } |
| 464 | |
| 465 | void String8::toUpper(size_t start, size_t length) |
| 466 | { |
| 467 | const size_t len = size(); |
| 468 | if (start >= len) { |
| 469 | return; |
| 470 | } |
| 471 | if (start+length > len) { |
| 472 | length = len-start; |
| 473 | } |
| 474 | char* buf = lockBuffer(len); |
| 475 | buf += start; |
| 476 | while (length > 0) { |
| 477 | *buf = toupper(*buf); |
| 478 | buf++; |
| 479 | length--; |
| 480 | } |
| 481 | unlockBuffer(len); |
| 482 | } |
| 483 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 484 | size_t String8::getUtf32Length() const |
| 485 | { |
| 486 | return utf32_length(mString, length()); |
| 487 | } |
| 488 | |
| 489 | int32_t String8::getUtf32At(size_t index, size_t *next_index) const |
| 490 | { |
| 491 | return utf32_at(mString, length(), index, next_index); |
| 492 | } |
| 493 | |
| 494 | size_t String8::getUtf32(char32_t* dst, size_t dst_len) const |
| 495 | { |
| 496 | return utf8_to_utf32(mString, length(), dst, dst_len); |
| 497 | } |
| 498 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | TextOutput& operator<<(TextOutput& to, const String8& val) |
| 500 | { |
| 501 | to << val.string(); |
| 502 | return to; |
| 503 | } |
| 504 | |
| 505 | // --------------------------------------------------------------------------- |
| 506 | // Path functions |
| 507 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | void String8::setPathName(const char* name) |
| 509 | { |
| 510 | setPathName(name, strlen(name)); |
| 511 | } |
| 512 | |
| 513 | void String8::setPathName(const char* name, size_t len) |
| 514 | { |
| 515 | char* buf = lockBuffer(len); |
| 516 | |
| 517 | memcpy(buf, name, len); |
| 518 | |
| 519 | // remove trailing path separator, if present |
| 520 | if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR) |
| 521 | len--; |
| 522 | |
| 523 | buf[len] = '\0'; |
| 524 | |
| 525 | unlockBuffer(len); |
| 526 | } |
| 527 | |
| 528 | String8 String8::getPathLeaf(void) const |
| 529 | { |
| 530 | const char* cp; |
| 531 | const char*const buf = mString; |
| 532 | |
| 533 | cp = strrchr(buf, OS_PATH_SEPARATOR); |
| 534 | if (cp == NULL) |
| 535 | return String8(*this); |
| 536 | else |
| 537 | return String8(cp+1); |
| 538 | } |
| 539 | |
| 540 | String8 String8::getPathDir(void) const |
| 541 | { |
| 542 | const char* cp; |
| 543 | const char*const str = mString; |
| 544 | |
| 545 | cp = strrchr(str, OS_PATH_SEPARATOR); |
| 546 | if (cp == NULL) |
| 547 | return String8(""); |
| 548 | else |
| 549 | return String8(str, cp - str); |
| 550 | } |
| 551 | |
| 552 | String8 String8::walkPath(String8* outRemains) const |
| 553 | { |
| 554 | const char* cp; |
| 555 | const char*const str = mString; |
| 556 | const char* buf = str; |
| 557 | |
| 558 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 559 | if (cp == buf) { |
| 560 | // don't include a leading '/'. |
| 561 | buf = buf+1; |
| 562 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 563 | } |
| 564 | |
| 565 | if (cp == NULL) { |
| 566 | String8 res = buf != str ? String8(buf) : *this; |
| 567 | if (outRemains) *outRemains = String8(""); |
| 568 | return res; |
| 569 | } |
| 570 | |
| 571 | String8 res(buf, cp-buf); |
| 572 | if (outRemains) *outRemains = String8(cp+1); |
| 573 | return res; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Helper function for finding the start of an extension in a pathname. |
| 578 | * |
| 579 | * Returns a pointer inside mString, or NULL if no extension was found. |
| 580 | */ |
| 581 | char* String8::find_extension(void) const |
| 582 | { |
| 583 | const char* lastSlash; |
| 584 | const char* lastDot; |
| 585 | int extLen; |
| 586 | const char* const str = mString; |
| 587 | |
| 588 | // only look at the filename |
| 589 | lastSlash = strrchr(str, OS_PATH_SEPARATOR); |
| 590 | if (lastSlash == NULL) |
| 591 | lastSlash = str; |
| 592 | else |
| 593 | lastSlash++; |
| 594 | |
| 595 | // find the last dot |
| 596 | lastDot = strrchr(lastSlash, '.'); |
| 597 | if (lastDot == NULL) |
| 598 | return NULL; |
| 599 | |
| 600 | // looks good, ship it |
| 601 | return const_cast<char*>(lastDot); |
| 602 | } |
| 603 | |
| 604 | String8 String8::getPathExtension(void) const |
| 605 | { |
| 606 | char* ext; |
| 607 | |
| 608 | ext = find_extension(); |
| 609 | if (ext != NULL) |
| 610 | return String8(ext); |
| 611 | else |
| 612 | return String8(""); |
| 613 | } |
| 614 | |
| 615 | String8 String8::getBasePath(void) const |
| 616 | { |
| 617 | char* ext; |
| 618 | const char* const str = mString; |
| 619 | |
| 620 | ext = find_extension(); |
| 621 | if (ext == NULL) |
| 622 | return String8(*this); |
| 623 | else |
| 624 | return String8(str, ext - str); |
| 625 | } |
| 626 | |
| 627 | String8& String8::appendPath(const char* name) |
| 628 | { |
| 629 | // TODO: The test below will fail for Win32 paths. Fix later or ignore. |
| 630 | if (name[0] != OS_PATH_SEPARATOR) { |
| 631 | if (*name == '\0') { |
| 632 | // nothing to do |
| 633 | return *this; |
| 634 | } |
| 635 | |
| 636 | size_t len = length(); |
| 637 | if (len == 0) { |
| 638 | // no existing filename, just use the new one |
| 639 | setPathName(name); |
| 640 | return *this; |
| 641 | } |
| 642 | |
| 643 | // make room for oldPath + '/' + newPath |
| 644 | int newlen = strlen(name); |
| 645 | |
| 646 | char* buf = lockBuffer(len+1+newlen); |
| 647 | |
| 648 | // insert a '/' if needed |
| 649 | if (buf[len-1] != OS_PATH_SEPARATOR) |
| 650 | buf[len++] = OS_PATH_SEPARATOR; |
| 651 | |
| 652 | memcpy(buf+len, name, newlen+1); |
| 653 | len += newlen; |
| 654 | |
| 655 | unlockBuffer(len); |
| 656 | |
| 657 | return *this; |
| 658 | } else { |
| 659 | setPathName(name); |
| 660 | return *this; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | String8& String8::convertToResPath() |
| 665 | { |
| 666 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR |
| 667 | size_t len = length(); |
| 668 | if (len > 0) { |
| 669 | char * buf = lockBuffer(len); |
| 670 | for (char * end = buf + len; buf < end; ++buf) { |
| 671 | if (*buf == OS_PATH_SEPARATOR) |
| 672 | *buf = RES_PATH_SEPARATOR; |
| 673 | } |
| 674 | unlockBuffer(len); |
| 675 | } |
| 676 | #endif |
| 677 | return *this; |
| 678 | } |
| 679 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | }; // namespace android |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 681 | |
| 682 | // --------------------------------------------------------------------------- |
| 683 | |
| 684 | size_t strlen32(const char32_t *s) |
| 685 | { |
| 686 | const char32_t *ss = s; |
| 687 | while ( *ss ) |
| 688 | ss++; |
| 689 | return ss-s; |
| 690 | } |
| 691 | |
| 692 | size_t strnlen32(const char32_t *s, size_t maxlen) |
| 693 | { |
| 694 | const char32_t *ss = s; |
| 695 | while ((maxlen > 0) && *ss) { |
| 696 | ss++; |
| 697 | maxlen--; |
| 698 | } |
| 699 | return ss-s; |
| 700 | } |
| 701 | |
Daisuke Miyakawa | 3228a10 | 2009-07-09 13:05:24 +0900 | [diff] [blame] | 702 | size_t utf8_length(const char *src) |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 703 | { |
| 704 | const char *cur = src; |
| 705 | size_t ret = 0; |
| 706 | while (*cur != '\0') { |
| 707 | const char first_char = *cur++; |
| 708 | if ((first_char & 0x80) == 0) { // ASCII |
| 709 | ret += 1; |
| 710 | continue; |
| 711 | } |
| 712 | // (UTF-8's character must not be like 10xxxxxx, |
| 713 | // but 110xxxxx, 1110xxxx, ... or 1111110x) |
| 714 | if ((first_char & 0x40) == 0) { |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | int32_t mask, to_ignore_mask; |
| 719 | size_t num_to_read = 0; |
| 720 | char32_t utf32 = 0; |
| 721 | for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80; |
| 722 | num_to_read < 5 && (first_char & mask); |
| 723 | num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
| 724 | if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx |
| 725 | return 0; |
| 726 | } |
| 727 | // 0x3F == 00111111 |
| 728 | utf32 = (utf32 << 6) + (*cur++ & 0x3F); |
| 729 | } |
| 730 | // "first_char" must be (110xxxxx - 11110xxx) |
| 731 | if (num_to_read == 5) { |
| 732 | return 0; |
| 733 | } |
| 734 | to_ignore_mask |= mask; |
| 735 | utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1)); |
| 736 | if (utf32 > android::kUnicodeMaxCodepoint) { |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | ret += num_to_read; |
| 741 | } |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | size_t utf32_length(const char *src, size_t src_len) |
| 746 | { |
| 747 | if (src == NULL || src_len == 0) { |
| 748 | return 0; |
| 749 | } |
| 750 | size_t ret = 0; |
| 751 | const char* cur; |
| 752 | const char* end; |
| 753 | size_t num_to_skip; |
| 754 | for (cur = src, end = src + src_len, num_to_skip = 1; |
| 755 | cur < end; |
| 756 | cur += num_to_skip, ret++) { |
| 757 | const char first_char = *cur; |
| 758 | num_to_skip = 1; |
| 759 | if ((first_char & 0x80) == 0) { // ASCII |
| 760 | continue; |
| 761 | } |
| 762 | int32_t mask; |
| 763 | |
| 764 | for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) { |
| 765 | } |
| 766 | } |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | size_t utf8_length_from_utf32(const char32_t *src, size_t src_len) |
| 771 | { |
| 772 | if (src == NULL || src_len == 0) { |
| 773 | return 0; |
| 774 | } |
| 775 | size_t ret = 0; |
| 776 | const char32_t *end = src + src_len; |
| 777 | while (src < end) { |
| 778 | ret += android::utf32_to_utf8_bytes(*src++); |
| 779 | } |
| 780 | return ret; |
| 781 | } |
| 782 | |
Kenny Root | 92f5984 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 783 | size_t utf8_length_from_utf16(const char16_t *src, size_t src_len) |
| 784 | { |
| 785 | if (src == NULL || src_len == 0) { |
| 786 | return 0; |
| 787 | } |
| 788 | size_t ret = 0; |
| 789 | const char16_t* const end = src + src_len; |
| 790 | while (src < end) { |
| 791 | if ((*src & 0xFC00) == 0xD800 && (src + 1) < end |
| 792 | && (*++src & 0xFC00) == 0xDC00) { |
| 793 | // surrogate pairs are always 4 bytes. |
| 794 | ret += 4; |
| 795 | src++; |
| 796 | } else { |
| 797 | ret += android::utf32_to_utf8_bytes((char32_t) *src++); |
| 798 | } |
| 799 | } |
| 800 | return ret; |
| 801 | } |
| 802 | |
Daisuke Miyakawa | 9f22024 | 2009-06-30 20:40:42 +0900 | [diff] [blame] | 803 | static int32_t utf32_at_internal(const char* cur, size_t *num_read) |
| 804 | { |
| 805 | const char first_char = *cur; |
| 806 | if ((first_char & 0x80) == 0) { // ASCII |
| 807 | *num_read = 1; |
| 808 | return *cur; |
| 809 | } |
| 810 | cur++; |
| 811 | char32_t mask, to_ignore_mask; |
| 812 | size_t num_to_read = 0; |
| 813 | char32_t utf32 = first_char; |
| 814 | for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80; |
| 815 | (first_char & mask); |
| 816 | num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
| 817 | // 0x3F == 00111111 |
| 818 | utf32 = (utf32 << 6) + (*cur++ & 0x3F); |
| 819 | } |
| 820 | to_ignore_mask |= mask; |
| 821 | utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1))); |
| 822 | |
| 823 | *num_read = num_to_read; |
| 824 | return static_cast<int32_t>(utf32); |
| 825 | } |
| 826 | |
| 827 | int32_t utf32_at(const char *src, size_t src_len, |
| 828 | size_t index, size_t *next_index) |
| 829 | { |
| 830 | if (index >= src_len) { |
| 831 | return -1; |
| 832 | } |
| 833 | size_t dummy_index; |
| 834 | if (next_index == NULL) { |
| 835 | next_index = &dummy_index; |
| 836 | } |
| 837 | size_t num_read; |
| 838 | int32_t ret = utf32_at_internal(src + index, &num_read); |
| 839 | if (ret >= 0) { |
| 840 | *next_index = index + num_read; |
| 841 | } |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | size_t utf8_to_utf32(const char* src, size_t src_len, |
| 847 | char32_t* dst, size_t dst_len) |
| 848 | { |
| 849 | if (src == NULL || src_len == 0 || dst == NULL || dst_len == 0) { |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | const char* cur = src; |
| 854 | const char* end = src + src_len; |
| 855 | char32_t* cur_utf32 = dst; |
| 856 | const char32_t* end_utf32 = dst + dst_len; |
| 857 | while (cur_utf32 < end_utf32 && cur < end) { |
| 858 | size_t num_read; |
| 859 | *cur_utf32++ = |
| 860 | static_cast<char32_t>(utf32_at_internal(cur, &num_read)); |
| 861 | cur += num_read; |
| 862 | } |
| 863 | if (cur_utf32 < end_utf32) { |
| 864 | *cur_utf32 = 0; |
| 865 | } |
| 866 | return static_cast<size_t>(cur_utf32 - dst); |
| 867 | } |
| 868 | |
| 869 | size_t utf32_to_utf8(const char32_t* src, size_t src_len, |
| 870 | char* dst, size_t dst_len) |
| 871 | { |
| 872 | if (src == NULL || src_len == 0 || dst == NULL || dst_len == 0) { |
| 873 | return 0; |
| 874 | } |
| 875 | const char32_t *cur_utf32 = src; |
| 876 | const char32_t *end_utf32 = src + src_len; |
| 877 | char *cur = dst; |
| 878 | const char *end = dst + dst_len; |
| 879 | while (cur_utf32 < end_utf32 && cur < end) { |
| 880 | size_t len = android::utf32_to_utf8_bytes(*cur_utf32); |
| 881 | android::utf32_to_utf8((uint8_t *)cur, *cur_utf32++, len); |
| 882 | cur += len; |
| 883 | } |
| 884 | if (cur < end) { |
| 885 | *cur = '\0'; |
| 886 | } |
| 887 | return cur - dst; |
| 888 | } |
Kenny Root | 92f5984 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 889 | |
| 890 | size_t utf16_to_utf8(const char16_t* src, size_t src_len, |
| 891 | char* dst, size_t dst_len) |
| 892 | { |
| 893 | if (src == NULL || src_len == 0 || dst == NULL || dst_len == 0) { |
| 894 | return 0; |
| 895 | } |
| 896 | const char16_t* cur_utf16 = src; |
| 897 | const char16_t* const end_utf16 = src + src_len; |
| 898 | char *cur = dst; |
| 899 | const char* const end = dst + dst_len; |
| 900 | while (cur_utf16 < end_utf16 && cur < end) { |
| 901 | char32_t utf32; |
| 902 | // surrogate pairs |
| 903 | if ((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16) { |
| 904 | utf32 = (*cur_utf16++ - 0xD800) << 10; |
| 905 | utf32 |= *cur_utf16++ - 0xDC00; |
| 906 | utf32 += 0x10000; |
| 907 | } else { |
| 908 | utf32 = (char32_t) *cur_utf16++; |
| 909 | } |
| 910 | size_t len = android::utf32_to_utf8_bytes(utf32); |
| 911 | android::utf32_to_utf8((uint8_t*)cur, utf32, len); |
| 912 | cur += len; |
| 913 | } |
| 914 | if (cur < end) { |
| 915 | *cur = '\0'; |
| 916 | } |
| 917 | return cur - dst; |
| 918 | } |