Overview
Comment: | Add -[stringWithUTF16String:] to OFString. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
ec666ede071edf0c6564b6c372643f15 |
User & Date: | js on 2011-05-14 23:52:25 |
Other Links: | manifest | tags |
Context
2011-05-15
| ||
00:32 | configure.ac: Fix two tests that would fail with Clang + new ABI. check-in: 56b1009167 user: js tags: trunk | |
2011-05-14
| ||
23:52 | Add -[stringWithUTF16String:] to OFString. check-in: ec666ede07 user: js tags: trunk | |
23:25 | Correctly check the length in of_string_check_utf8. check-in: 55368ab28a user: js tags: trunk | |
Changes
Modified src/OFString.h from [289337b95c] to [fd1c48329e].
︙ | |||
39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | + | #endif extern int of_string_check_utf8(const char*, 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 } #endif @class OFArray; @class OFURL; |
︙ | |||
130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 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 | + + + + + + + + + + + + + + + + + + + | * \param string The unicode string * \param length The length of the unicode string * \return A new autoreleased OFString */ + stringWithUnicodeString: (of_unichar_t*)string length: (size_t)length; /** * Creates a new OFString from a UTF-16 encoded string. * * \param string The UTF-16 string * \return A new autoreleased OFString */ + stringWithUTF16String: (uint16_t*)string; /** * Creates a new OFString from a UTF-16 encoded string with the specified * length. * * \param string The UTF-16 string * \param length The length of the unicode string * \return A new autoreleased OFString */ + stringWithUTF16String: (uint16_t*)string length: (size_t)length; /** * Creates a new OFString from a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString * \return A new autoreleased OFString */ |
︙ | |||
261 262 263 264 265 266 267 268 269 270 271 272 273 274 | 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | + + + + + + + + + + + + + + + + + + + | * \param string The unicode string * \param length The length of the unicode string * \return An initialized OFString */ - initWithUnicodeString: (of_unichar_t*)string length: (size_t)length; /** * Initializes an already allocated OFString with a UTF-16 string. * * \param string The UTF-16 string * \return An initialized OFString */ - initWithUTF16String: (uint16_t*)string; /** * Initializes an already allocated OFString with a UTF-16 string with the * specified length. * * \param string The UTF-16 string * \param length The length of the UTF-16 string * \return An initialized OFString */ - initWithUTF16String: (uint16_t*)string length: (size_t)length; /** * Initializes an already allocated OFString with a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString * \return An initialized OFString */ |
︙ |
Modified src/OFString.m from [c99ccfd8e4] to [3c842a2c2f].
︙ | |||
251 252 253 254 255 256 257 258 259 260 261 262 263 264 | 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | + + + + + + + + + + + | const of_unichar_t *string_ = string; while (*string_ != 0) string_++; return (size_t)(string_ - string); } size_t of_utf16_string_length(const uint16_t *string) { const uint16_t *string_ = string; while (*string_ != 0) string_++; return (size_t)(string_ - string); } @implementation OFString + string { return [[[self alloc] init] autorelease]; } |
︙ | |||
302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | + + + + + + + + + + + + | + stringWithUnicodeString: (of_unichar_t*)string length: (size_t)length { return [[[self alloc] initWithUnicodeString: string length: length] autorelease]; } + stringWithUTF16String: (uint16_t*)string { return [[[self alloc] initWithUTF16String: string] autorelease]; } + stringWithUTF16String: (uint16_t*)string length: (size_t)length { return [[[self alloc] initWithUTF16String: string length: length] autorelease]; } + stringWithFormat: (OFString*)format, ... { id ret; va_list arguments; va_start(arguments, format); |
︙ | |||
579 580 581 582 583 584 585 586 587 588 589 590 591 592 | 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | length = length_; string = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length_; i++) { size_t characterLen = of_string_unicode_to_utf8( (swap ? of_bswap32(string_[i]) : string_[i]), buffer); switch (characterLen) { case 1: string[j++] = buffer[0]; break; case 2: isUTF8 = YES; length++; memcpy(string + j, buffer, 2); j += 2; break; case 3: isUTF8 = YES; length += 2; memcpy(string + j, buffer, 3); j += 3; break; case 4: isUTF8 = YES; length += 3; memcpy(string + j, buffer, 4); j += 4; break; default: @throw [OFInvalidEncodingException newWithClass: isa]; } } string[j] = '\0'; @try { string = [self resizeMemory: string toSize: length + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ [e release]; } } @catch (id e) { [self release]; @throw e; } return self; } - initWithUTF16String: (uint16_t*)string_ { return [self initWithUTF16String: string_ length: of_utf16_string_length(string_)]; } - initWithUTF16String: (uint16_t*)string_ length: (size_t)length_ { self = [super init]; @try { char buffer[4]; size_t i, j = 0; BOOL swap = NO; if (*string_ == 0xFEFF) { string_++; length_--; } if (*string_ == 0xFFFE) { swap = YES; string_++; length_--; } length = length_; string = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length_; i++) { of_unichar_t character = (swap ? of_bswap16(string_[i]) : string_[i]); size_t characterLen; /* Missed the high surrogate */ if ((character & 0xFC00) == 0xDC00) @throw [OFInvalidEncodingException newWithClass: isa]; if ((character & 0xFC00) == 0xD800) { uint16_t nextCharacter; if (length <= i + 1) @throw [OFInvalidEncodingException newWithClass: isa]; nextCharacter = (swap ? of_bswap16(string_[i + 1]) : string_[i + 1]); character = (((character & 0x3FF) << 10) | (nextCharacter & 0x3FF)) + 0x10000; i++; } characterLen = of_string_unicode_to_utf8( character, buffer); switch (characterLen) { case 1: string[j++] = buffer[0]; break; case 2: isUTF8 = YES; |
︙ |
Modified tests/OFStringTests.m from [24fef6ba66] to [60a07bdb13].
︙ | |||
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | + + + + + + + | static of_unichar_t ucstr[] = { 0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0x1F03A, 0 }; static of_unichar_t sucstr[] = { 0xFFFE0000, 0x66000000, 0xF6000000, 0xF6000000, 0x62000000, 0xE4000000, 0x72000000, 0x3AF00100, 0 }; static uint16_t utf16str[] = { 0xFEFF, 'f', 0xF6, 0xF6, 'b', 0xE4, 'r', 0xD83C, 0xDC3A, 0 }; static uint16_t sutf16str[] = { 0xFFFE, 0x6600, 0xF600, 0xF600, 0x6200, 0xE400, 0x7200, 0x3CD8, 0x3ADC, 0 }; @interface EntityHandler: OFObject <OFStringXMLUnescapingDelegate> @end @implementation EntityHandler - (OFString*)string: (OFString*)string containsUnknownEntityNamed: (OFString*)entity |
︙ | |||
139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | + + + + + + | TEST(@"+[stringWithUnicodeString:]", (s[1] = [OFString stringWithUnicodeString: ucstr]) && [s[1] isEqual: @"fööbär🀺"] && (s[1] = [OFString stringWithUnicodeString: sucstr]) && [s[1] isEqual: @"fööbär🀺"]) TEST(@"+[stringWithUTF16String:]", (s[1] = [OFString stringWithUTF16String: utf16str]) && [s[1] isEqual: @"fööbär🀺"] && (s[1] = [OFString stringWithUTF16String: sutf16str]) && [s[1] isEqual: @"fööbär🀺"]) TEST(@"+[stringWithContentsOfFile:encoding]", (s[1] = [OFString stringWithContentsOfFile: @"testfile.txt" encoding: OF_STRING_ENCODING_ISO_8859_1]) && [s[1] isEqual: @"testäöü"]) TEST(@"+[stringWithContentsOfURL:encoding]", (s[1] = [OFString stringWithContentsOfURL: [OFURL URLWithString: |
︙ |