GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — livecode/libfoundation/src/foundation-string.cpp at develop · livecode/livecode
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Expand file tree/ Copy pathfoundation-string.cppMore file actions executable file ·7359 lines (5972 loc) · 222 KB / Copy pathfoundation-string.cppFile metadata and controlsexecutable file ·7359 lines (5972 loc) · 222 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 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 269 270 271 272 273 274 275 276 277 278 279 280 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 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 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 462 463 464 465 466 467 468 469 470 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 565 566 567 568 569 570 571 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 601 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 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* Copyright (C) 2003-2015 LiveCode Ltd. This file is part of LiveCode. LiveCode is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v3 as published by the Free Software Foundation. LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ #include <foundation.h> #include <foundation-auto.h> #include <foundation-unicode.h> #include "foundation-private.h" #include "foundation-bidi.h" #include "foundation-chunk.h" #ifdef __LINUX__ #include <errno.h> #include <iconv.h> #include <langinfo.h> #include <locale.h> #endif #ifdef __WINDOWS__ #include <Windows.h> #endif //////////////////////////////////////////////////////////////////////////////// // This method ensures there is 'count' chars of empty space starting at 'at' // in 'string'. It returns false if allocation fails. static bool __MCStringExpandAt(MCStringRef string, uindex_t at, uindex_t count); // This method removes 'count' chars from string starting at 'at'. static void __MCStringShrinkAt(MCStringRef string, uindex_t at, uindex_t count); // This method clamps the given range to the valid limits for the string. static void __MCStringClampRange(MCStringRef string, MCRange& x_range); // This method forces a nativization of a string even if there is already a native char ptr. static bool __MCStringNativize(MCStringRef string, uindex_t &r_char_count); static bool __MCStringNativize(MCStringRef string); // This method ensures there is a unichar ptr. static bool __MCStringUnnativize(MCStringRef self); // This method marks the string as changed. static void __MCStringChanged(MCStringRef string, uindex_t simple = kMCStringFlagNoChange, uindex_t combined = kMCStringFlagNoChange, uindex_t native = kMCStringFlagNoChange); // Creates an indirect mutable string with contents. static bool __MCStringCreateIndirect(__MCString *contents, __MCString*& r_string); // Returns true if the string is indirect. static bool __MCStringIsIndirect(__MCString *self); static bool __MCStringMakeImmutable(__MCString *self); // Creates an immutable string from this one, changing 'self' to indirect. static bool __MCStringMakeIndirect(__MCString *self); // Ensures the given mutable but indirect string is direct. static bool __MCStringResolveIndirect(__MCString *self); // Makes direct mutable string indirect, referencing r_new_string. static bool __MCStringCopyMutable(__MCString *self, __MCString*& r_new_string); // Copy the given unicode chars into the target unicode buffer and return true // if all the chars being copied in could be native. static bool __MCStringCopyChars(unichar_t *target, const unichar_t *source, uindex_t count, bool target_can_be_native); static bool MCStringSplitByDelimiterNative(MCStringRef self, MCStringRef p_elem_del, MCStringOptions p_options, MCProperListRef& r_list); // Check the string and set CanBeNative, Basic and Trivial flags accordingly static void __MCStringCheck(MCStringRef self); //////////////////////////////////////////////////////////////////////////////// // AL-2015-02-06: [[ Bug 14504 ]] Add wrappers for string flag and length checking, // for internal use when a string is known to be direct. static bool __MCStringIsNative(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); return (self -> flags & kMCStringFlagIsNotNative) == 0; } static bool __MCStringIsChecked(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); return (self -> flags & kMCStringFlagIsChecked) != 0; } static bool __MCStringIsTrivial(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); __MCStringCheck(self); return (self -> flags & kMCStringFlagIsNotNative) == 0 || (self -> flags & kMCStringFlagIsTrivial) != 0; } static bool __MCStringIsBasic(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); __MCStringCheck(self); return (self -> flags & kMCStringFlagIsNotNative) == 0 || (self -> flags & kMCStringFlagIsBasic) != 0; } static bool __MCStringCanBeNative(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); return (self -> flags & kMCStringFlagIsNotNative) == 0 || (self -> flags & kMCStringFlagCanBeNative) != 0; } static bool __MCStringCantBeEqualToNative(MCStringRef self, MCStringOptions p_options) { // If self can't be native, then we check the comparison options to see if // it could still be native after normalization. if (!__MCStringCanBeNative(self)) { // At this point self must contain unicode characters which don't directly // map to native. Thus the only way we could be equal to a native string is // if we contain combining sequences which compose to a native char. switch(p_options) { case kMCStringOptionCompareExact: case kMCStringOptionCompareFolded: // If no normalization is taking place, then no composition can occur // so we can't be equal to native. return true; case kMCStringOptionCompareNonliteral: case kMCStringOptionCompareCaseless: // If the string has been checked then we have more information. if (__MCStringIsChecked(self)) { // If there are no combining chars, then normalization is not // going to make a difference - there's no way this string // can be native. if (__MCStringIsTrivial(self)) return true; // If the string is not simple, then even though it contains // combining chars it can't be native. if (!__MCStringIsBasic(self)) return true; } break; default: MCUnreachableReturn(false); } } return false; } static bool __MCStringCopyChars(unichar_t *p_dst, const unichar_t *p_src, uindex_t p_count, bool p_dst_can_be_native) { // If the dst cannot be native, then there's no point checking if the src can be. if (!p_dst_can_be_native) { MCMemoryCopy(p_dst, p_src, p_count * sizeof(unichar_t)); return false; } // Copy the unicode chars to our dst checking if we can nativize as we go. for(uindex_t i = 0; i < p_count; i++) { // If we fail to convert the char to native, then we can't be native so // finish up with a direct copy. char_t t_nchar; if (!MCUnicodeCharMapToNative(p_src[i], t_nchar)) { MCMemoryCopy(p_dst + i, p_src + i, (p_count - i) * sizeof(unichar_t)); return false; } // We still copy across unicode char. p_dst[i] = p_src[i]; } // If we get here, then both src and dst can be native. return true; } static uindex_t __MCStringGetLength(MCStringRef self) { MCAssert(!__MCStringIsIndirect(self)); return self -> char_count; } static bool __MCStringIsEmpty(MCStringRef string) { return string == nil || __MCStringGetLength(string) == 0; } //////////////////////////////////////////////////////////////////////////////// #include "foundation-string-native.cpp.h" //////////////////////////////////////////////////////////////////////////////// // This method creates a 'constant' MCStringRef from the given c-string. At some // point we'll make it work 'magically' at compile/build time. For now, uniquing // and returning that has a similar effect (if slightly slower). MC_DLLEXPORT_DEF MCStringRef MCSTR(const char *p_cstring) { MCAssert(nil != p_cstring); MCStringRef t_string = nullptr; /* UNCHECKED */ MCStringCreateWithNativeChars((const char_t *)p_cstring, strlen(p_cstring), t_string); MCValueRef t_unique_string = nullptr; /* UNCHECKED */ MCValueInter(t_string, t_unique_string); MCValueRelease(t_string); return (MCStringRef)t_unique_string; } //////////////////////////////////////////////////////////////////////////////// MC_DLLEXPORT_DEF bool MCStringCreateWithCString(const char* p_cstring, MCStringRef& r_string) { return MCStringCreateWithNativeChars((const char_t*)p_cstring, p_cstring == nil ? 0 : strlen(p_cstring), r_string); } MC_DLLEXPORT_DEF bool MCStringCreateWithCStringAndRelease(char* p_cstring, MCStringRef& r_string) { MCAssert(nil != p_cstring); if (MCStringCreateWithNativeChars((const char_t *)p_cstring, p_cstring == nil ? 0 : strlen((const char*)p_cstring), r_string)) { delete[] p_cstring; return true; } return false; } MC_DLLEXPORT_DEF const char *MCStringGetCString(MCStringRef p_string) { if (p_string == nil) return nil; __MCAssertIsString(p_string); MCStringNativize(p_string); const char *t_cstring; t_cstring = (const char *)MCStringGetNativeCharPtr(p_string); MCAssert(t_cstring != nil); return t_cstring; } MC_DLLEXPORT_DEF bool MCStringIsEqualToCString(MCStringRef p_string, const char *p_cstring, MCStringOptions p_options) { __MCAssertIsString(p_string); return MCStringIsEqualToNativeChars(p_string, (const char_t *)p_cstring, strlen(p_cstring), p_options); } MC_DLLEXPORT_DEF bool MCStringSubstringIsEqualToCString(MCStringRef p_string, MCRange p_range, const char *p_cstring, MCStringOptions p_options) { __MCAssertIsString(p_string); return MCStringSubstringIsEqualToNativeChars(p_string, p_range, (const char_t *)p_cstring, strlen(p_cstring), p_options); } // Create an immutable string from the given bytes, interpreting them using // the specified encoding. MC_DLLEXPORT_DEF bool MCStringCreateWithBytes(const byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string) { if (0 == p_byte_count) { if (nil != kMCEmptyString) { r_string = MCValueRetain(kMCEmptyString); return true; } } else { MCAssert(nil != p_bytes); } MCAssert(!p_is_external_rep); switch (p_encoding) { case kMCStringEncodingASCII: case kMCStringEncodingNative: return MCStringCreateWithNativeChars(p_bytes, p_byte_count, r_string); case kMCStringEncodingUTF16: return MCStringCreateWithChars((unichar_t *)p_bytes, p_byte_count / 2, r_string); // AL-2014-31-03: [[ Bug 12067 ]] Fix conversion from little endian bytes. case kMCStringEncodingUTF16LE: case kMCStringEncodingUTF16BE: { unichar_t *t_buffer; uindex_t t_length = p_byte_count / 2; if (!MCMemoryNewArray(t_length, t_buffer)) return false; for (uindex_t i = 0; i < t_length; i++) { if (p_encoding == kMCStringEncodingUTF16BE) t_buffer[i] = (unichar_t)MCSwapInt16BigToHost(((unichar_t *)p_bytes)[i]); else t_buffer[i] = (unichar_t)MCSwapInt16LittleToHost(((unichar_t *)p_bytes)[i]); } return MCStringCreateWithCharsAndRelease(t_buffer, t_length, r_string); } case kMCStringEncodingUTF8: { unichar_t *t_chars; uindex_t t_char_count; t_char_count = MCUnicodeCharsMapFromUTF8(p_bytes, p_byte_count, nil, 0); if (!MCMemoryNewArray(t_char_count, t_chars)) return false; MCUnicodeCharsMapFromUTF8(p_bytes, p_byte_count, t_chars, t_char_count); if (!MCStringCreateWithCharsAndRelease(t_chars, t_char_count, r_string)) { MCMemoryDeleteArray(t_chars); return false; } return true; } case kMCStringEncodingUTF32: case kMCStringEncodingUTF32LE: case kMCStringEncodingUTF32BE: { // Round the byte count to a multiple of UTF-32 units p_byte_count = ((p_byte_count + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1)); // Convert the string to UTF-16 first. MCAutoArray<unichar_t> t_buffer; if (!t_buffer.Extend(p_byte_count / sizeof(uint32_t))) return false; uindex_t t_in_offset; uindex_t t_out_offset = 0; for (t_in_offset = 0; t_in_offset < p_byte_count; t_in_offset += sizeof(uint32_t)) { // BMP characters are output unchanged, non-BMP requires surrogate pairs codepoint_t t_codepoint; t_codepoint = *(uint32_t*)&p_bytes[t_in_offset]; if (p_encoding == kMCStringEncodingUTF32BE) t_codepoint = MCSwapInt32BigToHost(t_codepoint); else if (p_encoding == kMCStringEncodingUTF32LE) t_codepoint = MCSwapInt32LittleToHost(t_codepoint); if (t_codepoint < 0x10000) { t_buffer[t_out_offset] = unichar_t(t_codepoint); t_out_offset += 1; } else { // Split to surrogate pairs // SN-2015-07-03: [[ Bug 15571 ]] Creating a surrogate pair // makes the UTF-16 string longer. if (!t_buffer . Extend(t_buffer . Size() + 1)) return false; unichar_t t_lead, t_trail; t_lead = unichar_t((t_codepoint - 0x10000) >> 10) + 0xD800; t_trail = unichar_t((t_codepoint - 0x10000) & 0x3FF) + 0xDC00; t_buffer[t_out_offset] = t_lead; t_buffer[t_out_offset + 1] = t_trail; t_out_offset += 2; } } return MCStringCreateWithChars(t_buffer.Ptr(), t_out_offset, r_string); } #if !defined(__ISO_8859_1__) case kMCStringEncodingISO8859_1: break; #endif #ifndef __WINDOWS__ case kMCStringEncodingWindows1252: break; #endif #if !defined(__MAC__) && !defined(__IOS__) case kMCStringEncodingMacRoman: break; #endif } return false; } MC_DLLEXPORT_DEF bool MCStringCreateWithBytesAndRelease(byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string) { if (p_byte_count == 0) { if (kMCEmptyString != nil) { r_string = MCValueRetain(kMCEmptyString); free(p_bytes); return true; } } else { MCAssert(nil != p_bytes); } MCStringRef t_string; t_string = nil; switch (p_encoding) { case kMCStringEncodingASCII: case kMCStringEncodingNative: break; default: if (!MCStringCreateWithBytes(p_bytes, p_byte_count, p_encoding, p_is_external_rep, t_string)) return false; r_string = t_string; free(p_bytes); return true; } bool t_success; t_success = true; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, t_string); if (t_success) t_success = MCMemoryReallocate(p_bytes, p_byte_count + 1, p_bytes); if (t_success) { p_bytes[p_byte_count] = '\0'; t_string -> native_chars = p_bytes; t_string -> char_count = p_byte_count; r_string = t_string; } else MCMemoryDelete(t_string); return t_success; } //////////////////////////////////////////////////////////////////////////////// MC_DLLEXPORT_DEF bool MCStringCreateWithChars(const unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string) { if (p_char_count == 0) { if (kMCEmptyString != nil) { r_string = MCValueRetain(kMCEmptyString); return true; } } else { MCAssert(nil != p_chars); } bool t_success; t_success = true; __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); bool t_not_native; t_not_native = false; if (t_success) t_success = MCMemoryNewArray(p_char_count + 1, self -> native_chars); if (t_success) { uindex_t i; for(i = 0; i < p_char_count; i++) if (!MCUnicodeCharMapToNative(p_chars[i], self -> native_chars[i])) { t_not_native = true; break; } if (t_not_native) { MCMemoryDeleteArray(self -> native_chars); t_success = MCMemoryNewArray(p_char_count + 1, self -> chars); } } if (t_success) { if (t_not_native) { MCStrCharsMapFromUnicode(p_chars, p_char_count, self -> chars, self -> char_count); self -> flags |= kMCStringFlagIsNotNative; } else self -> char_count = p_char_count; r_string = self; } else { if (self != nil) MCMemoryDeleteArray(self -> chars); MCMemoryDelete(self); } return t_success; } MC_DLLEXPORT_DEF bool MCStringCreateWithCharsAndRelease(unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string) { MCAssert(nil != p_chars); if (MCStringCreateWithChars(p_chars, p_char_count, r_string)) { free(p_chars); return true; } return false; } MC_DLLEXPORT_DEF bool MCStringCreateUnicodeWithChars(const unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string) { if (p_char_count == 0) { if (kMCEmptyString != nil) { r_string = MCValueRetain(kMCEmptyString); return true; } } else { MCAssert(nil != p_chars); } bool t_success; t_success = true; __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); if (t_success) t_success = MCMemoryNewArray(p_char_count + 1, self -> chars); if (t_success) { MCStrCharsMapFromUnicode(p_chars, p_char_count, self -> chars, self -> char_count); self -> flags |= kMCStringFlagIsNotNative; r_string = self; } else { if (self != nil) MCMemoryDeleteArray(self -> chars); MCMemoryDelete(self); } return t_success; } MC_DLLEXPORT_DEF bool MCStringCreateWithWString(const unichar_t *p_wstring, MCStringRef& r_string) { MCAssert(nil != p_wstring); uindex_t t_length; for(t_length = 0; p_wstring[t_length] != 0; t_length++) ; return MCStringCreateWithChars(p_wstring, t_length, r_string); } MC_DLLEXPORT_DEF bool MCStringCreateWithWStringAndRelease(unichar_t* p_wstring, MCStringRef& r_string) { MCAssert(nil != p_wstring); if (MCStringCreateWithWString(p_wstring, r_string)) { free(p_wstring); return true; } return false; } MC_DLLEXPORT_DEF bool MCStringCreateWithNativeChars(const char_t *p_chars, uindex_t p_char_count, MCStringRef& r_string) { bool t_success; t_success = true; if ((p_chars == nil || p_char_count == 0) && kMCEmptyString != nil) { r_string = MCValueRetain(kMCEmptyString); return true; } MCAssert(nil != p_chars); __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); if (t_success) t_success = MCMemoryNewArray(p_char_count + 1, self -> native_chars); if (t_success) MCMemoryCopy(self -> native_chars, p_chars, p_char_count); else { if (self != nil) MCMemoryDeleteArray(self -> native_chars); MCMemoryDelete(self); } if (t_success) { self -> char_count = p_char_count; r_string = self; } return t_success; } MC_DLLEXPORT_DEF bool MCStringCreateWithNativeCharsAndRelease(char_t *p_chars, uindex_t p_char_count, MCStringRef& r_string) { return MCStringCreateWithNativeCharBufferAndRelease(p_chars, p_char_count, p_char_count, r_string); } MC_DLLEXPORT_DEF bool MCStringCreateWithNativeCharBufferAndRelease(char_t* p_chars, uindex_t p_char_count, uindex_t p_buffer_length, MCStringRef& r_string) { MCAssert(nil != p_chars); bool t_success; t_success = true; if (p_char_count == 0 && kMCEmptyString != nil) { r_string = MCValueRetain(kMCEmptyString); MCMemoryDeallocate(p_chars); return true; } __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); uindex_t t_capacity = p_buffer_length; if (t_success && t_capacity < p_char_count + 1) { t_capacity = p_char_count + 1; t_success = MCMemoryReallocate(p_chars, t_capacity, p_chars); } if (t_success) { p_chars[p_char_count] = '\0'; self -> native_chars = p_chars; self -> char_count = p_char_count; self -> capacity = t_capacity; r_string = self; } else MCMemoryDelete(self); return t_success; } static bool MCStringCreateMutableUnicode(uindex_t p_initial_capacity, MCStringRef& r_string) { bool t_success; t_success = true; __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); if (t_success) { self -> flags |= kMCStringFlagIsNotNative; t_success = __MCStringExpandAt(self, 0, p_initial_capacity); } if (t_success) { self -> flags |= kMCStringFlagIsMutable; self->char_count = 0; r_string = self; } else { MCValueRelease (self); } return t_success; } MC_DLLEXPORT_DEF bool MCStringCreateMutable(uindex_t p_initial_capacity, MCStringRef& r_string) { bool t_success; t_success = true; __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); if (t_success) t_success = __MCStringExpandAt(self, 0, p_initial_capacity); if (t_success) { self -> flags |= kMCStringFlagIsMutable; self->char_count = 0; r_string = self; } else { MCValueRelease (self); } return t_success; } //////////////////////////////////////////////////////////////////////////////// MC_DLLEXPORT_DEF bool MCStringEncode(MCStringRef p_string, MCStringEncoding p_encoding, bool p_is_external_rep, MCDataRef& r_data) { __MCAssertIsString(p_string); byte_t *t_bytes; uindex_t t_byte_count; if (!MCStringConvertToBytes(p_string, p_encoding, p_is_external_rep, t_bytes, t_byte_count)) return false; if (!MCDataCreateWithBytesAndRelease(t_bytes, t_byte_count, r_data)) { free(t_bytes); return false; } return true; } MC_DLLEXPORT_DEF bool MCStringEncodeAndRelease(MCStringRef p_string, MCStringEncoding p_encoding, bool p_is_external_rep, MCDataRef& r_data) { __MCAssertIsString(p_string); MCDataRef t_data; if (!MCStringEncode(p_string, p_encoding, p_is_external_rep, t_data)) return false; MCValueRelease(p_string); r_data = t_data; return true; } MC_DLLEXPORT_DEF bool MCStringDecode(MCDataRef p_data, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string) { return MCStringCreateWithBytes(MCDataGetBytePtr(p_data), MCDataGetLength(p_data), p_encoding, p_is_external_rep, r_string); } MC_DLLEXPORT_DEF bool MCStringDecodeAndRelease(MCDataRef p_data, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string) { __MCAssertIsData(p_data); MCStringRef t_string; if (!MCStringDecode(p_data, p_encoding, p_is_external_rep, t_string)) return false; MCValueRelease(p_data); r_string = t_string; return true; } // SN-2015-07-27: [[ Bug 15379 ]] This function is only used for internal // purposes, when a LiveCode function parameter can be data - in which case // no char translation must occur between the bytes in the DataRef and the // Unicode string that the engine function takes (in MCR_exec for instance) bool MCStringCreateUnicodeStringFromData(MCDataRef p_data, bool p_is_external_rep, MCStringRef& r_string) { __MCAssertIsData(p_data); MCAssert(!p_is_external_rep); if (MCDataIsEmpty(p_data)) { r_string = MCValueRetain(kMCEmptyString); return true; } bool t_success; t_success = true; __MCString *self; self = nil; if (t_success) t_success = __MCValueCreate(kMCValueTypeCodeString, self); uint32_t t_byte_count; t_byte_count = MCDataGetLength(p_data); const byte_t* t_bytes = MCDataGetBytePtr(p_data); if (t_success) t_success = MCMemoryNewArray(t_byte_count + 1, self -> chars); if (t_success) { uindex_t i; for(i = 0; i < t_byte_count; i++) self -> chars[i] = t_bytes[i]; } if (t_success) { self -> flags |= kMCStringFlagIsNotNative; self -> char_count = t_byte_count; r_string = self; } else { if (self != nil) MCMemoryDeleteArray(self -> chars); MCMemoryDelete(self); } return t_success; } //////////////////////////////////////////////////////////////////////////////// MC_DLLEXPORT_DEF bool MCStringFormatV(MCStringRef& r_string, const char *p_format, va_list p_args) { MCAssert(nil != p_format); MCStringRef t_buffer; if (!MCStringCreateMutable(0, t_buffer)) return false; bool t_success; t_success = true; const char *t_format_ptr; t_format_ptr = p_format; while (t_success && *t_format_ptr != '\0') { const char *t_format_start_ptr, *t_format_end_ptr; t_format_start_ptr = t_format_end_ptr = t_format_ptr; bool t_has_range; t_has_range = false; // We need to track integer, floating-point and 'long double' argument // counts separately. The last group needs to be tracked individually as // it is passed in different registers on x86_64 to other floating-point // values. // // The integer types are themselves broken down by type, to ensure we // always use the correct type in the call to va_arg. We don't need to // track anything smaller than an int as the rules for '...' params say // that parameters are promoted to int/double. size_t t_int_arg_count = 0; size_t t_long_arg_count = 0; size_t t_llong_arg_count = 0; size_t t_ptr_arg_count = 0; size_t t_float_arg_count = 0; size_t t_ldouble_arg_count = 0; // Whether we are dealing with an MCValueRef or not bool t_is_valueref = false; // Loop until we encounter something that can't be handled by the C // library (i.e a ValueRef formatting command) or reach the end of the // formatting string. while (!t_is_valueref && *t_format_ptr != '\0') { // Is this a formatting command? if (*t_format_ptr != '%') { // Not a formatting command; continue t_format_ptr++; t_format_end_ptr = t_format_ptr; } else { // Move past the '%' and begin processing the formatting command t_format_ptr++; // Skip any flag characters at the beginning of the format while (*t_format_ptr == '-' || *t_format_ptr == '+' || *t_format_ptr == ' ' || *t_format_ptr == '#' || *t_format_ptr == '0') { t_format_ptr++; } // Skip any width specifier that is present bool t_indirect_width = false; if (*t_format_ptr == '*') { // Width will be specified via an argument of type int t_int_arg_count++; t_format_ptr++; t_indirect_width = true; } else { // Skip any digits specifying the width while (isdigit(*t_format_ptr)) t_format_ptr++; } // Skip any precision specifier that is present bool t_indirect_precision = false; if (*t_format_ptr == '.') { t_format_ptr++; if (*t_format_ptr == '*') { // Precision will be specified via an argument of type int t_int_arg_count++; t_format_ptr++; t_indirect_precision = true; } else { // Skip any digits specifying the precision while (isdigit(*t_format_ptr)) t_format_ptr++; } } // Process any size specifiers that are present size_t* t_type_count = NULL; switch (*t_format_ptr) { case 'h': |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |