Differences From Artifact [5b68538ff6]:
- File
src/OFXMLParser.m
— part of check-in
[1255f3a11a]
at
2012-08-10 20:08:24
on branch trunk
— Directly use the runtime's autorelease pools.
This greatly improves performance, as it gets rid of the overhead of
OFAutoreleasePool. (user: js, size: 25000) [annotate] [blame] [check-ins using]
To Artifact [f9b1934552]:
- File src/OFXMLParser.m — part of check-in [c137da5e5b] at 2012-09-16 15:27:48 on branch trunk — Prefix all private methods with OF_. (user: js, size: 25072) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
138 139 140 141 142 143 144 | @implementation OFXMLParser + (void)initialize { size_t i; const SEL selectors_[] = { | | | | | | | | | | | | | | | | | | | | | 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 | @implementation OFXMLParser + (void)initialize { size_t i; const SEL selectors_[] = { @selector(OF_parseOutsideTagWithBuffer:i:last:), @selector(OF_parseTagOpenedWithBuffer:i:last:), @selector(OF_parseInProcessingInstructionsWithBuffer:i:last:), @selector(OF_parseInTagNameWithBuffer:i:last:), @selector(OF_parseInCloseTagNameWithBuffer:i:last:), @selector(OF_parseInTagWithBuffer:i:last:), @selector(OF_parseInAttributeNameWithBuffer:i:last:), @selector(OF_parseExpectDelimiterWithBuffer:i:last:), @selector(OF_parseInAttributeValueWithBuffer:i:last:), @selector(OF_parseExpectCloseWithBuffer:i:last:), @selector(OF_parseExpectSpaceOrCloseWithBuffer:i:last:), @selector(OF_parseInExclamationMarkWithBuffer:i:last:), @selector(OF_parseInCDATAOpeningWithBuffer:i:last:), @selector(OF_parseInCDATA1WithBuffer:i:last:), @selector(OF_parseInCDATA2WithBuffer:i:last:), @selector(OF_parseInCommentOpeningWithBuffer:i:last:), @selector(OF_parseInComment1WithBuffer:i:last:), @selector(OF_parseInComment2WithBuffer:i:last:), @selector(OF_parseInDoctypeWithBuffer:i:last:), }; memcpy(selectors, selectors_, sizeof(selectors_)); for (i = 0; i < OF_XMLPARSER_NUM_STATES; i++) { if (![self instancesRespondToSelector: selectors[i]]) @throw [OFInitializationFailedException exceptionWithClass: self]; |
︙ | ︙ | |||
299 300 301 302 303 304 305 | /* * The following methods handle the different states of the parser. They are * looked up in +[initialize] and put in a lookup table to speed things up. * One dispatch for every character would be way too slow! */ /* Not in a tag */ | | | | | 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | /* * The following methods handle the different states of the parser. They are * looked up in +[initialize] and put in a lookup table to speed things up. * One dispatch for every character would be way too slow! */ /* Not in a tag */ - (void)OF_parseOutsideTagWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { size_t length; if ((finishedParsing || [previous count] < 1) && buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' && buffer[*i] != '<') @throw [OFMalformedXMLException exceptionWithClass: [self class] |
︙ | ︙ | |||
334 335 336 337 338 339 340 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_TAG_OPENED; } /* Tag was just opened */ | | | | | 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_TAG_OPENED; } /* Tag was just opened */ - (void)OF_parseTagOpenedWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (finishedParsing && buffer[*i] != '!' && buffer[*i] != '?') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; switch (buffer[*i]) { case '?': |
︙ | ︙ | |||
367 368 369 370 371 372 373 | acceptProlog = NO; (*i)--; break; } } /* <?xml […]?> */ | | | 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | acceptProlog = NO; (*i)--; break; } } /* <?xml […]?> */ - (BOOL)OF_parseXMLProcessingInstructions: (OFString*)pi { const char *cString; size_t i, last, length; int piState = 0; OFString *attribute = nil; OFMutableString *value = nil; char piDelimiter = 0; |
︙ | ︙ | |||
463 464 465 466 467 468 469 | if (piState != 0) return NO; return YES; } /* Inside processing instructions */ | | | | | | | | | 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 | if (piState != 0) return NO; return YES; } /* Inside processing instructions */ - (void)OF_parseInProcessingInstructionsWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '?') level = 1; else if (level == 1 && buffer[*i] == '>') { void *pool = objc_autoreleasePoolPush(); OFString *pi; cache_append(cache, buffer + *last, encoding, *i - *last); pi = transform_string(cache, 1, NO, nil); if ([pi isEqual: @"xml"] || [pi hasPrefix: @"xml "] || [pi hasPrefix: @"xml\t"] || [pi hasPrefix: @"xml\r"] || [pi hasPrefix: @"xml\n"]) if (![self OF_parseXMLProcessingInstructions: pi]) @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; [delegate parser: self foundProcessingInstructions: pi]; objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } else level = 0; } /* Inside a tag, no name yet */ - (void)OF_parseInTagNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; const char *cacheCString, *tmp; size_t length, cacheLength; OFString *cacheString; if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && |
︙ | ︙ | |||
580 581 582 583 584 585 586 | objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; } /* Inside a close tag, no name yet */ | | | | | 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | objc_autoreleasePoolPop(pool); [cache removeAllItems]; *last = *i + 1; } /* Inside a close tag, no name yet */ - (void)OF_parseInCloseTagNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; const char *cacheCString, *tmp; size_t length, cacheLength; OFString *cacheString; OFString *ns; |
︙ | ︙ | |||
652 653 654 655 656 657 658 | : OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE); if ([previous count] == 0) finishedParsing = YES; } /* Inside a tag, name found */ | | | | | 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | : OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE); if ([previous count] == 0) finishedParsing = YES; } /* Inside a tag, name found */ - (void)OF_parseInTagWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *ns; OFXMLAttribute **attributesObjects; size_t j, attributesCount; if (buffer[*i] != '>' && buffer[*i] != '/') { |
︙ | ︙ | |||
725 726 727 728 729 730 731 | *last = *i + 1; state = (buffer[*i] == '/' ? OF_XMLPARSER_EXPECT_CLOSE : OF_XMLPARSER_OUTSIDE_TAG); } /* Looking for attribute name */ | | | | | 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | *last = *i + 1; state = (buffer[*i] == '/' ? OF_XMLPARSER_EXPECT_CLOSE : OF_XMLPARSER_OUTSIDE_TAG); } /* Looking for attribute name */ - (void)OF_parseInAttributeNameWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFMutableString *cacheString; const char *cacheCString, *tmp; size_t length, cacheLength; if (buffer[*i] != '=') |
︙ | ︙ | |||
772 773 774 775 776 777 778 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_EXPECT_DELIM; } /* Expecting delimiter */ | | | | | | | | 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 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_EXPECT_DELIM; } /* Expecting delimiter */ - (void)OF_parseExpectDelimiterWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { *last = *i + 1; if (buffer[*i] == ' ' || buffer[*i] == '\t' || buffer[*i] == '\n' || buffer[*i] == '\r') return; if (buffer[*i] != '\'' && buffer[*i] != '"') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; delimiter = buffer[*i]; state = OF_XMLPARSER_IN_ATTR_VALUE; } /* Looking for attribute value */ - (void)OF_parseInAttributeValueWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *attributeValue; size_t length; if (buffer[*i] != delimiter) return; |
︙ | ︙ | |||
832 833 834 835 836 837 838 | attributeName = attributePrefix = nil; *last = *i + 1; state = OF_XMLPARSER_IN_TAG; } /* Expecting closing '>' */ | | | | | | | | | | | 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 | attributeName = attributePrefix = nil; *last = *i + 1; state = OF_XMLPARSER_IN_TAG; } /* Expecting closing '>' */ - (void)OF_parseExpectCloseWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '>') { *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } else @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; } /* Expecting closing '>' or space */ - (void)OF_parseExpectSpaceOrCloseWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '>') { *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } else if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; } /* In <! */ - (void)OF_parseInExclamationMarkWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (finishedParsing && buffer[*i] != '-') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; if (buffer[*i] == '-') state = OF_XMLPARSER_IN_COMMENT_OPENING; |
︙ | ︙ | |||
883 884 885 886 887 888 889 | @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; *last = *i + 1; } /* CDATA */ | | | | | | | | | | | 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 | @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; *last = *i + 1; } /* CDATA */ - (void)OF_parseInCDATAOpeningWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] != "CDATA["[level]) @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; if (++level == 6) { state = OF_XMLPARSER_IN_CDATA_1; level = 0; } *last = *i + 1; } - (void)OF_parseInCDATA1WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == ']') level++; else level = 0; if (level == 2) state = OF_XMLPARSER_IN_CDATA_2; } - (void)OF_parseInCDATA2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *CDATA; if (buffer[*i] != '>') { state = OF_XMLPARSER_IN_CDATA_1; level = (buffer[*i] == ']' ? 1 : 0); |
︙ | ︙ | |||
943 944 945 946 947 948 949 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } /* Comment */ | | | | | | | | | | | 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 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } /* Comment */ - (void)OF_parseInCommentOpeningWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] != '-') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; *last = *i + 1; state = OF_XMLPARSER_IN_COMMENT_1; level = 0; } - (void)OF_parseInComment1WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if (buffer[*i] == '-') level++; else level = 0; if (level == 2) state = OF_XMLPARSER_IN_COMMENT_2; } - (void)OF_parseInComment2WithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { void *pool; OFString *comment; if (buffer[*i] != '>') @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; |
︙ | ︙ | |||
997 998 999 1000 1001 1002 1003 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } /* In <!DOCTYPE ...> */ | | | | | 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 | [cache removeAllItems]; *last = *i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } /* In <!DOCTYPE ...> */ - (void)OF_parseInDoctypeWithBuffer: (const char*)buffer i: (size_t*)i last: (size_t*)last { if ((level < 6 && buffer[*i] != "OCTYPE"[level]) || (level == 6 && buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r')) @throw [OFMalformedXMLException exceptionWithClass: [self class] parser: self]; |
︙ | ︙ |