Overview
Comment: | Cache the length of a string.
Also removes -[appendCStringWithoutUTF8Checking:] as it is dangerous and |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
36e8a94f3412cd36448e32cd99e5ad2a |
User & Date: | js on 2011-07-09 14:45:44 |
Other Links: | manifest | tags |
Context
2011-07-09
| ||
21:58 | Only add -fgnu89-inline if it doesn't generate a warning. check-in: 4f8cf7e3ec user: js tags: trunk | |
14:45 | Cache the length of a string. check-in: 36e8a94f34 user: js tags: trunk | |
14:34 | Use OFDataArray instead of OFMutableString for cache in OFXMLParser. check-in: bbe98ea6c2 user: js tags: trunk | |
Changes
Modified src/OFConstantString.m from [c1cbc7f938] to [87992a22fc].
︙ | ︙ | |||
51 52 53 54 55 56 57 | @throw [OFOutOfMemoryException newWithClass: isa requestedSize: sizeof(*ivars)]; memset(ivars, 0, sizeof(*ivars)); ivars->cString = (char*)s; ivars->cStringLength = initialized; | | > | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | @throw [OFOutOfMemoryException newWithClass: isa requestedSize: sizeof(*ivars)]; memset(ivars, 0, sizeof(*ivars)); ivars->cString = (char*)s; ivars->cStringLength = initialized; switch (of_string_check_utf8(ivars->cString, ivars->cStringLength, &ivars->length)) { case 1: ivars->isUTF8 = YES; break; case -1: free(ivars); @throw [OFInvalidEncodingException newWithClass: isa]; } |
︙ | ︙ |
Modified src/OFMutableString.h from [b015e271e1] to [903f2cd12d].
︙ | ︙ | |||
55 56 57 58 59 60 61 | * \param encoding The encoding of the C string * \param cStringLength The length of the UTF-8 encoded C string */ - (void)appendCString: (const char*)cString withEncoding: (of_string_encoding_t)encoding length: (size_t)cStringLength; | < < < < < < < < < < < < < < < < < < < < < < < < | 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | * \param encoding The encoding of the C string * \param cStringLength The length of the UTF-8 encoded C string */ - (void)appendCString: (const char*)cString withEncoding: (of_string_encoding_t)encoding length: (size_t)cStringLength; /** * \brief Appends another OFString to the OFMutableString. * * \param string An OFString to append */ - (void)appendString: (OFString*)string; |
︙ | ︙ |
Modified src/OFMutableString.m from [8b9a1eb927] to [3c8f4c2bc5].
︙ | ︙ | |||
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | assert(j == newCStringLength); newCString[j] = 0; [self freeMemory: unicodeString]; [self freeMemory: s->cString]; s->cString = newCString; s->cStringLength = newCStringLength; } - (void)setToCString: (const char*)newCString { size_t newCStringLength = strlen(newCString); if (newCStringLength >= 3 && !memcmp(newCString, "\xEF\xBB\xBF", 3)) { newCString += 3; newCStringLength -= 3; } | > > > > > > | > > > > | > > > > | > > > | | | | < < | < < | | | | < | | < < | < < < | | 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | assert(j == newCStringLength); newCString[j] = 0; [self freeMemory: unicodeString]; [self freeMemory: s->cString]; s->cString = newCString; s->cStringLength = newCStringLength; /* * Even though cStringLength can change, length can not, therefore no * need to change it. */ } - (void)setToCString: (const char*)newCString { size_t newCStringLength = strlen(newCString); size_t newLength; if (newCStringLength >= 3 && !memcmp(newCString, "\xEF\xBB\xBF", 3)) { newCString += 3; newCStringLength -= 3; } switch (of_string_check_utf8(newCString, newCStringLength, &newLength)) { case 0: s->isUTF8 = NO; break; case 1: s->isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } [self freeMemory: s->cString]; s->cStringLength = newCStringLength; s->length = newLength; s->cString = [self allocMemoryWithSize: newCStringLength + 1]; memcpy(s->cString, newCString, newCStringLength + 1); } - (void)appendCString: (const char*)cString { size_t cStringLength = strlen(cString); size_t length; if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) { cString += 3; cStringLength -= 3; } switch (of_string_check_utf8(cString, cStringLength, &length)) { case 1: s->isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + cStringLength + 1]; memcpy(s->cString + s->cStringLength, cString, cStringLength + 1); s->cStringLength += cStringLength; s->length += length; } - (void)appendCString: (const char*)cString withLength: (size_t)cStringLength { size_t length; if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) { cString += 3; cStringLength -= 3; } switch (of_string_check_utf8(cString, cStringLength, &length)) { case 1: s->isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + cStringLength + 1]; memcpy(s->cString + s->cStringLength, cString, cStringLength); s->cStringLength += cStringLength; s->length += length; s->cString[s->cStringLength] = 0; } - (void)appendCString: (const char*)cString withEncoding: (of_string_encoding_t)encoding length: (size_t)cStringLength { if (encoding == OF_STRING_ENCODING_UTF_8) [self appendCString: cString withLength: cStringLength]; else { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; [self appendString: [OFString stringWithCString: cString encoding: encoding length: cStringLength]]; [pool release]; } } - (void)appendString: (OFString*)string { size_t cStringLength; if (string == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; cStringLength = [string cStringLength]; s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + cStringLength + 1]; memcpy(s->cString + s->cStringLength, string->s->cString, cStringLength); s->cStringLength += cStringLength; s->length += string->s->length; s->cString[s->cStringLength] = 0; if (string->s->isUTF8) s->isUTF8 = YES; } - (void)appendFormat: (OFConstantString*)format, ... { |
︙ | ︙ | |||
401 402 403 404 405 406 407 408 409 410 411 | } - (void)insertString: (OFString*)string atIndex: (size_t)index { size_t newCStringLength; if (s->isUTF8) index = of_string_index_to_position(s->cString, index, s->cStringLength); | > > > < < < | | | > > > > > > > > > > > < < < < < < < | 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | } - (void)insertString: (OFString*)string atIndex: (size_t)index { size_t newCStringLength; if (index > s->length) @throw [OFOutOfRangeException newWithClass: isa]; if (s->isUTF8) index = of_string_index_to_position(s->cString, index, s->cStringLength); newCStringLength = s->cStringLength + [string cStringLength]; s->cString = [self resizeMemory: s->cString toSize: newCStringLength + 1]; memmove(s->cString + index + string->s->cStringLength, s->cString + index, s->cStringLength - index); memcpy(s->cString + index, string->s->cString, string->s->cStringLength); s->cString[newCStringLength] = '\0'; s->cStringLength = newCStringLength; s->length += string->s->length; } - (void)deleteCharactersFromIndex: (size_t)start toIndex: (size_t)end { if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if (end > s->length) @throw [OFOutOfRangeException newWithClass: isa]; s->length -= end - start; if (s->isUTF8) { start = of_string_index_to_position(s->cString, start, s->cStringLength); end = of_string_index_to_position(s->cString, end, s->cStringLength); } memmove(s->cString + start, s->cString + end, s->cStringLength - end); s->cStringLength -= end - start; s->cString[s->cStringLength] = 0; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; |
︙ | ︙ | |||
460 461 462 463 464 465 466 | toIndex: range.start + range.length]; } - (void)replaceCharactersFromIndex: (size_t)start toIndex: (size_t)end withString: (OFString*)replacement { | | > > > > > > > > > < < < < < < < | | | | > | | | > > > > > | 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | toIndex: range.start + range.length]; } - (void)replaceCharactersFromIndex: (size_t)start toIndex: (size_t)end withString: (OFString*)replacement { size_t newCStringLength, newLength; if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if (end > s->length) @throw [OFOutOfRangeException newWithClass: isa]; newLength = s->length - (end - start) + [replacement length]; if (s->isUTF8) { start = of_string_index_to_position(s->cString, start, s->cStringLength); end = of_string_index_to_position(s->cString, end, s->cStringLength); } newCStringLength = s->cStringLength - (end - start) + replacement->s->cStringLength; s->cString = [self resizeMemory: s->cString toSize: newCStringLength + 1]; memmove(s->cString + end, s->cString + start + replacement->s->cStringLength, s->cStringLength - end); memcpy(s->cString + start, replacement->s->cString, replacement->s->cStringLength); s->cString[newCStringLength] = '\0'; s->cStringLength = newCStringLength; s->length = newLength; } - (void)replaceCharactersInRange: (of_range_t)range withString: (OFString*)replacement { [self replaceCharactersFromIndex: range.start toIndex: range.start + range.length withString: replacement]; } - (void)replaceOccurrencesOfString: (OFString*)string withString: (OFString*)replacement { const char *cString = [string cString]; const char *replacementCString = [replacement cString]; size_t cStringLength = string->s->cStringLength; size_t replacementCStringLength = replacement->s->cStringLength; size_t i, last, newCStringLength, newLength; char *newCString; if (cStringLength > s->cStringLength) return; newCString = NULL; newCStringLength = 0; newLength = s->length; for (i = 0, last = 0; i <= s->cStringLength - cStringLength; i++) { if (memcmp(s->cString + i, cString, cStringLength)) continue; @try { newCString = [self resizeMemory: newCString toSize: newCStringLength + i - last + replacementCStringLength + 1]; } @catch (id e) { [self freeMemory: newCString]; @throw e; } memcpy(newCString + newCStringLength, s->cString + last, i - last); memcpy(newCString + newCStringLength + i - last, replacementCString, replacementCStringLength); newCStringLength += i - last + replacementCStringLength; newLength = newLength - string->s->length + replacement->s->length; i += cStringLength - 1; last = i + 1; } @try { newCString = [self resizeMemory: newCString |
︙ | ︙ | |||
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | s->cStringLength - last); newCStringLength += s->cStringLength - last; newCString[newCStringLength] = 0; [self freeMemory: s->cString]; s->cString = newCString; s->cStringLength = newCStringLength; } - (void)deleteLeadingWhitespaces { size_t i; for (i = 0; i < s->cStringLength; i++) if (s->cString[i] != ' ' && s->cString[i] != '\t' && s->cString[i] != '\n' && s->cString[i] != '\r') break; s->cStringLength -= i; memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { | > > > | 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | s->cStringLength - last); newCStringLength += s->cStringLength - last; newCString[newCStringLength] = 0; [self freeMemory: s->cString]; s->cString = newCString; s->cStringLength = newCStringLength; s->length = newLength; } - (void)deleteLeadingWhitespaces { size_t i; for (i = 0; i < s->cStringLength; i++) if (s->cString[i] != ' ' && s->cString[i] != '\t' && s->cString[i] != '\n' && s->cString[i] != '\r') break; s->cStringLength -= i; s->length -= i; memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { |
︙ | ︙ | |||
592 593 594 595 596 597 598 599 600 601 602 603 604 605 | break; *p = '\0'; d++; } s->cStringLength -= d; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ [e release]; | > | 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | break; *p = '\0'; d++; } s->cStringLength -= d; s->length -= d; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ [e release]; |
︙ | ︙ | |||
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | break; *p = '\0'; d++; } s->cStringLength -= d; for (i = 0; i < s->cStringLength; i++) if (s->cString[i] != ' ' && s->cString[i] != '\t' && s->cString[i] != '\n' && s->cString[i] != '\r') break; s->cStringLength -= i; memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { | > > > | 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | break; *p = '\0'; d++; } s->cStringLength -= d; s->length -= d; for (i = 0; i < s->cStringLength; i++) if (s->cString[i] != ' ' && s->cString[i] != '\t' && s->cString[i] != '\n' && s->cString[i] != '\r') break; s->cStringLength -= i; s->length -= i; memmove(s->cString, s->cString + i, s->cStringLength); s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { |
︙ | ︙ |
Modified src/OFString.h from [3fb5b4e15d] to [d7444f1cd5].
︙ | ︙ | |||
35 36 37 38 39 40 41 | OF_STRING_ENCODING_WINDOWS_1252, OF_STRING_ENCODING_AUTODETECT = 0xFF } of_string_encoding_t; #ifdef __cplusplus extern "C" { #endif | | | 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | OF_STRING_ENCODING_WINDOWS_1252, OF_STRING_ENCODING_AUTODETECT = 0xFF } of_string_encoding_t; #ifdef __cplusplus extern "C" { #endif extern int of_string_check_utf8(const char*, size_t, size_t*); extern size_t of_string_unicode_to_utf8(of_unichar_t, char*); extern size_t of_string_utf8_to_unicode(const char*, size_t, of_unichar_t*); extern size_t of_string_position_to_index(const char*, size_t); extern size_t of_string_index_to_position(const char*, size_t, size_t); extern size_t of_unicode_string_length(const of_unichar_t*); extern size_t of_utf16_string_length(const uint16_t*); #ifdef __cplusplus |
︙ | ︙ | |||
74 75 76 77 78 79 80 81 82 83 84 85 86 87 | * struct on the first call to a constant string so we can have more * than those two ivars. */ struct of_string_ivars { char *cString; size_t cStringLength; BOOL isUTF8; } *restrict s; /* * Unused in OFString, however, OFConstantString sets this to SIZE_MAX * once it allocated and initialized the struct. */ size_t initialized; } | > | 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | * struct on the first call to a constant string so we can have more * than those two ivars. */ struct of_string_ivars { char *cString; size_t cStringLength; BOOL isUTF8; size_t length; } *restrict s; /* * Unused in OFString, however, OFConstantString sets this to SIZE_MAX * once it allocated and initialized the struct. */ size_t initialized; } |
︙ | ︙ |
Modified src/OFString.m from [d68d151905] to [fee37530e3].
︙ | ︙ | |||
78 79 80 81 82 83 84 | return OF_ORDERED_ASCENDING; } return OF_ORDERED_SAME; } int | | | | | | | | | | | | | | > | | | | > | | | | | > > | > > | 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | return OF_ORDERED_ASCENDING; } return OF_ORDERED_SAME; } int of_string_check_utf8(const char *cString, size_t cStringLength, size_t *length) { size_t i, tmpLength = cStringLength; int isUTF8 = 0; madvise((void*)cString, cStringLength, MADV_SEQUENTIAL); for (i = 0; i < cStringLength; i++) { /* No sign of UTF-8 here */ if (OF_LIKELY(!(cString[i] & 0x80))) continue; isUTF8 = 1; /* We're missing a start byte here */ if (OF_UNLIKELY(!(cString[i] & 0x40))) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } /* 2 byte sequences for code points 0 - 127 are forbidden */ if (OF_UNLIKELY((cString[i] & 0x7E) == 0x40)) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } /* We have at minimum a 2 byte character -> check next byte */ if (OF_UNLIKELY(cStringLength <= i + 1 || (cString[i + 1] & 0xC0) != 0x80)) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } /* Check if we have at minimum a 3 byte character */ if (OF_LIKELY(!(cString[i] & 0x20))) { i++; tmpLength--; continue; } /* We have at minimum a 3 byte char -> check second next byte */ if (OF_UNLIKELY(cStringLength <= i + 2 || (cString[i + 2] & 0xC0) != 0x80)) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } /* Check if we have a 4 byte character */ if (OF_LIKELY(!(cString[i] & 0x10))) { i += 2; tmpLength -= 2; continue; } /* We have a 4 byte character -> check third next byte */ if (OF_UNLIKELY(cStringLength <= i + 3 || (cString[i + 3] & 0xC0) != 0x80)) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } /* * Just in case, check if there's a 5th character, which is * forbidden by UTF-8 */ if (OF_UNLIKELY(cString[i] & 0x08)) { madvise((void*)cString, cStringLength, MADV_NORMAL); return -1; } i += 3; tmpLength -= 3; } madvise((void*)cString, cStringLength, MADV_NORMAL); if (length != NULL) *length = tmpLength; return isUTF8; } size_t of_string_unicode_to_utf8(of_unichar_t character, char *buffer) { |
︙ | ︙ | |||
472 473 474 475 476 477 478 | s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cString = [self allocMemoryWithSize: cStringLength + 1]; s->cStringLength = cStringLength; if (encoding == OF_STRING_ENCODING_UTF_8) { | | > > > > | 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cString = [self allocMemoryWithSize: cStringLength + 1]; s->cStringLength = cStringLength; if (encoding == OF_STRING_ENCODING_UTF_8) { switch (of_string_check_utf8(cString, cStringLength, &s->length)) { case 1: s->isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } memcpy(s->cString, cString, cStringLength); s->cString[cStringLength] = 0; return self; } /* All other encodings we support are single byte encodings */ s->length = cStringLength; if (encoding == OF_STRING_ENCODING_ISO_8859_1) { for (i = j = 0; i < cStringLength; i++) { char buffer[4]; size_t bytes; if (!(cString[i] & 0x80)) { |
︙ | ︙ | |||
587 588 589 590 591 592 593 594 595 596 597 | { self = [super init]; @try { s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cStringLength = [string cStringLength]; s->isUTF8 = string->s->isUTF8; s->cString = [self allocMemoryWithSize: s->cStringLength + 1]; | > > > > > | | 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | { self = [super init]; @try { s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); /* * We need one call to make sure it's initialized (in case it's * a constant string). */ s->cStringLength = [string cStringLength]; s->isUTF8 = string->s->isUTF8; s->length = string->s->length; s->cString = [self allocMemoryWithSize: s->cStringLength + 1]; memcpy(s->cString, string->s->cString, s->cStringLength + 1); } @catch (id e) { [self release]; @throw e; } return self; } |
︙ | ︙ | |||
648 649 650 651 652 653 654 655 656 657 658 659 660 661 | swap = YES; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cStringLength = length; s->cString = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length; i++) { char buffer[4]; size_t characterLen = of_string_unicode_to_utf8( (swap ? of_bswap32(string[i]) : string[i]), buffer); | > | 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | swap = YES; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cStringLength = length; s->cString = [self allocMemoryWithSize: (length * 4) + 1]; s->length = length; for (i = 0; i < length; i++) { char buffer[4]; size_t characterLen = of_string_unicode_to_utf8( (swap ? of_bswap32(string[i]) : string[i]), buffer); |
︙ | ︙ | |||
754 755 756 757 758 759 760 761 762 763 764 765 766 767 | swap = YES; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cStringLength = length; s->cString = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length; i++) { char buffer[4]; of_unichar_t character = (swap ? of_bswap16(string[i]) : string[i]); size_t characterLen; | > | 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | swap = YES; s = [self allocMemoryWithSize: sizeof(*s)]; memset(s, 0, sizeof(*s)); s->cStringLength = length; s->cString = [self allocMemoryWithSize: (length * 4) + 1]; s->length = length; for (i = 0; i < length; i++) { char buffer[4]; of_unichar_t character = (swap ? of_bswap16(string[i]) : string[i]); size_t characterLen; |
︙ | ︙ | |||
781 782 783 784 785 786 787 788 789 790 791 792 793 794 | ? of_bswap16(string[i + 1]) : string[i + 1]); character = (((character & 0x3FF) << 10) | (nextCharacter & 0x3FF)) + 0x10000; i++; s->cStringLength--; } characterLen = of_string_unicode_to_utf8( character, buffer); switch (characterLen) { case 1: | > | 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | ? of_bswap16(string[i + 1]) : string[i + 1]); character = (((character & 0x3FF) << 10) | (nextCharacter & 0x3FF)) + 0x10000; i++; s->cStringLength--; s->length--; } characterLen = of_string_unicode_to_utf8( character, buffer); switch (characterLen) { case 1: |
︙ | ︙ | |||
873 874 875 876 877 878 879 | arguments)) == -1) @throw [OFInvalidFormatException newWithClass: isa]; s->cStringLength = cStringLength; @try { switch (of_string_check_utf8(s->cString, | | | 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | arguments)) == -1) @throw [OFInvalidFormatException newWithClass: isa]; s->cStringLength = cStringLength; @try { switch (of_string_check_utf8(s->cString, cStringLength, &s->length)) { case 1: s->isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } |
︙ | ︙ | |||
926 927 928 929 930 931 932 933 934 935 936 937 | /* * First needs to be a call to be sure it is initialized, in * case it's a constant string. */ s->cStringLength = [firstComponent cStringLength]; s->isUTF8 = firstComponent->s->isUTF8; /* Calculate length and see if we need UTF-8 */ va_copy(argumentsCopy, arguments); while ((component = va_arg(argumentsCopy, OFString*)) != nil) { /* First needs to be a call, see above */ | > | | | 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | /* * First needs to be a call to be sure it is initialized, in * case it's a constant string. */ s->cStringLength = [firstComponent cStringLength]; s->isUTF8 = firstComponent->s->isUTF8; s->length = firstComponent->s->length; /* Calculate length and see if we need UTF-8 */ va_copy(argumentsCopy, arguments); while ((component = va_arg(argumentsCopy, OFString*)) != nil) { /* First needs to be a call, see above */ s->cStringLength += 1 + [component cStringLength]; s->length += 1 + component->s->length; if (component->s->isUTF8) s->isUTF8 = YES; } s->cString = [self allocMemoryWithSize: s->cStringLength + 1]; |
︙ | ︙ | |||
1108 1109 1110 1111 1112 1113 1114 | - (const char*)cString { return s->cString; } - (size_t)length { | < < < | < < > > > > | > | | 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 | - (const char*)cString { return s->cString; } - (size_t)length { return s->length; } - (size_t)cStringLength { return s->cStringLength; } - (BOOL)isEqual: (id)object { OFString *otherString; if (![object isKindOfClass: [OFString class]]) return NO; otherString = object; if ([otherString cStringLength] != s->cStringLength || otherString->s->length != s->length) return NO; if (strcmp(s->cString, otherString->s->cString)) return NO; return YES; } - copy { |
︙ | ︙ | |||
1301 1302 1303 1304 1305 1306 1307 | return element; } - (of_unichar_t)characterAtIndex: (size_t)index { of_unichar_t character; | < | > < < < < | 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 | return element; } - (of_unichar_t)characterAtIndex: (size_t)index { of_unichar_t character; if (index >= s->length) @throw [OFOutOfRangeException newWithClass: isa]; if (!s->isUTF8) return s->cString[index]; index = of_string_index_to_position(s->cString, index, s->cStringLength); if (!of_string_utf8_to_unicode(s->cString + index, s->cStringLength - index, &character)) @throw [OFInvalidEncodingException newWithClass: isa]; return character; } |
︙ | ︙ | |||
1364 1365 1366 1367 1368 1369 1370 | return OF_INVALID_INDEX; } } - (BOOL)containsString: (OFString*)string { const char *cString = [string cString]; | | > > > > > > > < < < < < < < | 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 | return OF_INVALID_INDEX; } } - (BOOL)containsString: (OFString*)string { const char *cString = [string cString]; size_t i, cStringLength = string->s->cStringLength; if (cStringLength == 0) return YES; if (cStringLength > s->cStringLength) return NO; for (i = 0; i <= s->cStringLength - cStringLength; i++) if (!memcmp(s->cString + i, cString, cStringLength)) return YES; return NO; } - (OFString*)substringFromIndex: (size_t)start toIndex: (size_t)end { if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if (end > s->length) @throw [OFOutOfRangeException newWithClass: isa]; if (s->isUTF8) { start = of_string_index_to_position(s->cString, start, s->cStringLength); end = of_string_index_to_position(s->cString, end, s->cStringLength); } return [OFString stringWithCString: s->cString + start length: end - start]; } - (OFString*)substringWithRange: (of_range_t)range { return [self substringFromIndex: range.start |
︙ | ︙ | |||
1858 1859 1860 1861 1862 1863 1864 | - (of_unichar_t*)unicodeString { OFObject *object = [[[OFObject alloc] init] autorelease]; of_unichar_t *ret; size_t i, j; | | | 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 | - (of_unichar_t*)unicodeString { OFObject *object = [[[OFObject alloc] init] autorelease]; of_unichar_t *ret; size_t i, j; ret = [object allocMemoryForNItems: s->length + 2 withSize: sizeof(of_unichar_t)]; i = 0; j = 0; ret[j++] = 0xFEFF; |
︙ | ︙ |