ObjFW  Check-in [bbe98ea6c2]

Overview
Comment:Use OFDataArray instead of OFMutableString for cache in OFXMLParser.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bbe98ea6c2b3f0e88b0010a261bd3e326b211dd1184ab0867c3f8bff8fbcc981
User & Date: js on 2011-07-09 14:34:14
Other Links: manifest | tags
Context
2011-07-09
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
13:33
Get rid of -[appendCStringWithoutUTF8Checking:] in base64.m. check-in: 56c98bf847 user: js tags: trunk
Changes

Modified src/OFXMLParser.h from [39864cd6ea] to [dcec4d0e65].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31







+







#import "OFObject.h"
#import "OFString.h"
#import "OFXMLAttribute.h"

@class OFXMLParser;
@class OFArray;
@class OFMutableArray;
@class OFDataArray;
@class OFStream;

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
typedef void (^of_xml_parser_processing_instructions_block_t)(
    OFXMLParser *parser, OFString *pi);
typedef void (^of_xml_parser_element_start_block_t)(OFXMLParser *parser,
    OFString *name, OFString *prefix, OFString *ns, OFArray *attributes);
156
157
158
159
160
161
162
163

164
165
166
167
168
169
170
157
158
159
160
161
162
163

164
165
166
167
168
169
170
171







-
+







		OF_XMLPARSER_IN_CDATA_2,
		OF_XMLPARSER_IN_COMMENT_OPENING,
		OF_XMLPARSER_IN_COMMENT_1,
		OF_XMLPARSER_IN_COMMENT_2,
		OF_XMLPARSER_IN_DOCTYPE,
		OF_XMLPARSER_NUM_STATES
	} state;
	OFMutableString *cache;
	OFDataArray *cache;
	OFString *name;
	OFString *prefix;
	OFMutableArray *namespaces;
	OFMutableArray *attributes;
	OFString *attributeName;
	OFString *attributePrefix;
	char delimiter;

Modified src/OFXMLParser.m from [d41280c3bf] to [b3a828e74d].

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
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







+















-
-
+
+



-
-
+
+


-
-
-
+
+
+
+
+





-
+


+
+
+
+
-
-
-
-
+
+
+
+


-
+

-
-
+
+



-
-
-
+
+
+
+







#include <string.h>
#include <unistd.h>

#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
#import "OFXMLAttribute.h"
#import "OFStream.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"

#import "OFInitializationFailedException.h"
#import "OFMalformedXMLException.h"
#import "OFUnboundNamespaceException.h"

#import "macros.h"

typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*);
static SEL selectors[OF_XMLPARSER_NUM_STATES];
static state_function lookupTable[OF_XMLPARSER_NUM_STATES];

static void
cache_append(OFMutableString *cache, const char *string,
static OF_INLINE void
cache_append(OFDataArray *cache, const char *string,
    of_string_encoding_t encoding, size_t length)
{
	if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8))
		[cache appendCStringWithoutUTF8Checking: string
						 length: length];
		[cache addNItems: length
		      fromCArray: string];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[cache appendString: [OFString stringWithCString: string
							encoding: encoding
							  length: length]];
		OFString *tmp = [OFString stringWithCString: string
						   encoding: encoding
						     length: length];
		[cache addNItems: [tmp cStringLength]
		      fromCArray: [tmp cString]];
		[pool release];
	}
}

static OFString*
transform_string(OFMutableString *cache, size_t cut, BOOL unescape,
transform_string(OFDataArray *cache, size_t cut, BOOL unescape,
    OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
	OFMutableString *ret = [OFMutableString
	    stringWithCString: [cache cArray]
		       length: [cache count]];

	[cache replaceOccurrencesOfString: @"\r\n"
			       withString: @"\n"];
	[cache replaceOccurrencesOfString: @"\r"
			       withString: @"\n"];
	[ret replaceOccurrencesOfString: @"\r\n"
			     withString: @"\n"];
	[ret replaceOccurrencesOfString: @"\r"
			     withString: @"\n"];

	if (cut > 0) {
		size_t length = [cache length];
		size_t length = [ret length];

		[cache deleteCharactersFromIndex: length - cut
					 toIndex: length];
		[ret deleteCharactersFromIndex: length - cut
				       toIndex: length];
	}

	if (unescape)
		return [cache stringByXMLUnescapingWithDelegate: delegate];
	else
		return [[cache copy] autorelease];
		return [ret stringByXMLUnescapingWithDelegate: delegate];

	ret->isa = [OFString class];
	return ret;
}

static OFString*
namespace_for_prefix(OFString *prefix, OFArray *namespaces)
{
	OFDictionary **cArray = [namespaces cArray];
	ssize_t i;
171
172
173
174
175
176
177
178

179
180
181
182
183
184
185
179
180
181
182
183
184
185

186
187
188
189
190
191
192
193







-
+







{
	self = [super init];

	@try {
		OFAutoreleasePool *pool;
		OFMutableDictionary *dict;

		cache = [[OFMutableString alloc] init];
		cache = [[OFBigDataArray alloc] init];
		previous = [[OFMutableArray alloc] init];
		namespaces = [[OFMutableArray alloc] init];

		pool = [[OFAutoreleasePool alloc] init];
		dict = [OFMutableDictionary dictionaryWithKeysAndObjects:
		    @"xml", @"http://www.w3.org/XML/1998/namespace",
		    @"xmlns", @"http://www.w3.org/2000/xmlns/", nil];
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
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







-
+

















-
+








	if (buffer[*i] != '<')
		return;

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);

	if ([cache cStringLength] > 0) {
	if ([cache count] > 0) {
		OFString *characters;
		OFAutoreleasePool *pool;

		pool = [[OFAutoreleasePool alloc] init];
		characters = transform_string(cache, 0, YES, self);

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
		if (charactersHandler != NULL)
			charactersHandler(self, characters);
		else
#endif
			[delegate parser: self
			 foundCharacters: characters];

		[pool release];
	}

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];

	*last = *i + 1;
	state = OF_XMLPARSER_TAG_OPENED;
}

/* Tag was just opened */
- (void)_parseTagOpenedWithBuffer: (const char*)buffer
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
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







-
+












+


+








+
+
-
-
+
+
+
+









-
+




-
+









-
+







					parser: self];

		[delegate parser: self
		    foundProcessingInstructions: pi];

		[pool release];

		[cache setToCString: ""];
		[cache removeNItems: [cache count]];

		*last = *i + 1;
		state = OF_XMLPARSER_OUTSIDE_TAG;
	} else
		level = 0;
}

/* Inside a tag, no name yet */
- (void)_parseInTagNameWithBuffer: (const char*)buffer
				i: (size_t*)i
			     last: (size_t*)last
{
	OFAutoreleasePool *pool;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;
	OFString *cacheString;

	if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' &&
	    buffer[*i] != '\r' && buffer[*i] != '>' && buffer[*i] != '/')
		return;

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);

	pool = [[OFAutoreleasePool alloc] init];

	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];
	cacheCString = [cache cArray];
	cacheLength = [cache count];
	cacheString = [OFString stringWithCString: cacheCString
					   length: cacheLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
							  (tmp - cacheCString) -
							  1];
		prefix = [[OFString alloc] initWithCString: cacheCString
						    length: tmp - cacheCString];
	} else {
		name = [cache copy];
		name = [cacheString copy];
		prefix = nil;
	}

	if (buffer[*i] == '>' || buffer[*i] == '/') {
		OFAutoreleasePool *pool;
		OFAutoreleasePool *pool2;
		OFString *ns;

		ns = namespace_for_prefix(prefix, namespaces);

		if (prefix != nil && ns == nil)
			@throw
			    [OFUnboundNamespaceException newWithClass: isa
							       prefix: prefix];

		pool = [[OFAutoreleasePool alloc] init];
		pool2 = [[OFAutoreleasePool alloc] init];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
		if (elementStartHandler != NULL)
			elementStartHandler(self, name, prefix, ns, nil);
		else
#endif
			[delegate parser: self
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
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







-
+

-
+











-
-
+

+





-
+











+









+
+
-
-
+
+
+
+









-
+



-
+





-
+






-
-







				   didEndElement: name
				      withPrefix: prefix
				       namespace: ns];

			if ([previous count] == 0)
				finishedParsing = YES;
		} else
			[previous addObject: [[cache copy] autorelease]];
			[previous addObject: cacheString];

		[pool release];
		[pool2 release];

		[name release];
		[prefix release];
		name = prefix = nil;

		state = (buffer[*i] == '/'
		    ? OF_XMLPARSER_EXPECT_CLOSE
		    : OF_XMLPARSER_OUTSIDE_TAG);
	} else
		state = OF_XMLPARSER_IN_TAG;

	if (buffer[*i] != '/') {
		OFAutoreleasePool *pool;
	[pool release];

	if (buffer[*i] != '/') {
		pool = [[OFAutoreleasePool alloc] init];
		[namespaces addObject: [OFMutableDictionary dictionary]];
		[pool release];
	}

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];
	*last = *i + 1;
}

/* Inside a close tag, no name yet */
- (void)_parseInCloseTagNameWithBuffer: (const char*)buffer
				     i: (size_t*)i
				  last: (size_t*)last
{
	OFAutoreleasePool *pool;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;
	OFString *cacheString;
	OFString *ns;

	if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' &&
	    buffer[*i] != '\r' && buffer[*i] != '>')
		return;

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);

	pool = [[OFAutoreleasePool alloc] init];

	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];
	cacheCString = [cache cArray];
	cacheLength = [cache count];
	cacheString = [OFString stringWithCString: cacheCString
					   length: cacheLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
							  (tmp - cacheCString) -
							  1];
		prefix = [[OFString alloc] initWithCString: cacheCString
						    length: tmp - cacheCString];
	} else {
		name = [cache copy];
		name = [cacheString copy];
		prefix = nil;
	}

	if (![[previous lastObject] isEqual: cache])
	if (![[previous lastObject] isEqual: cacheString])
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

	[previous removeLastObject];

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];

	ns = namespace_for_prefix(prefix, namespaces);
	if (prefix != nil && ns == nil)
		@throw [OFUnboundNamespaceException newWithClass: isa
							  prefix: prefix];

	pool = [[OFAutoreleasePool alloc] init];

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (elementEndHandler != NULL)
		elementEndHandler(self, name, prefix, ns);
	else
#endif
		[delegate parser: self
		   didEndElement: name
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
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







+
+









+
+
+
+
-
+
+
+

-
-
+
+









-
+



+
-
+
+







}

/* Looking for attribute name */
- (void)_parseInAttributeNameWithBuffer: (const char*)buffer
				      i: (size_t*)i
				   last: (size_t*)last
{
	OFAutoreleasePool *pool;
	OFMutableString *cacheString;
	const char *cacheCString, *tmp;
	size_t length, cacheLength;

	if (buffer[*i] != '=')
		return;

	if ((length = *i - *last) > 0)
		cache_append(cache, buffer + *last, encoding, length);

	pool = [[OFAutoreleasePool alloc] init];

	cacheString = [OFMutableString stringWithCString: [cache cArray]
						  length: [cache count]];
	[cache deleteEnclosingWhitespaces];
	[cacheString deleteEnclosingWhitespaces];
	/* Prevent a useless copy later */
	cacheString->isa = [OFString class];

	cacheCString = [cache cString];
	cacheLength = [cache cStringLength];
	cacheCString = [cacheString cString];
	cacheLength = [cacheString cStringLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		attributeName = [[OFString alloc]
		    initWithCString: tmp + 1
			     length: cacheLength - (tmp - cacheCString) - 1];
		attributePrefix = [[OFString alloc]
		    initWithCString: cacheCString
			     length: tmp - cacheCString];
	} else {
		attributeName = [cache copy];
		attributeName = [cacheString copy];
		attributePrefix = nil;
	}

	[pool release];
	[cache setToCString: ""];

	[cache removeNItems: [cache count]];

	*last = *i + 1;
	state = OF_XMLPARSER_EXPECT_DELIM;
}

/* Expecting delimiter */
- (void)_parseExpectDelimiterWithBuffer: (const char*)buffer
843
844
845
846
847
848
849
850

851
852
853
854
855
856
857
870
871
872
873
874
875
876

877
878
879
880
881
882
883
884







-
+







	[attributes addObject:
	    [OFXMLAttribute attributeWithName: attributeName
				    namespace: attributePrefix
				  stringValue: attributeValue]];

	[pool release];

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];
	[attributeName release];
	[attributePrefix release];
	attributeName = attributePrefix = nil;

	*last = *i + 1;
	state = OF_XMLPARSER_IN_TAG;
}
962
963
964
965
966
967
968
969

970
971
972
973
974
975
976
989
990
991
992
993
994
995

996
997
998
999
1000
1001
1002
1003







-
+







	else
#endif
		[delegate parser: self
		      foundCDATA: CDATA];

	[pool release];

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];

	*last = *i + 1;
	state = OF_XMLPARSER_OUTSIDE_TAG;
}

/* Comment */
- (void)_parseInCommentOpeningWithBuffer: (const char*)buffer
1021
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032
1033
1034
1035
1048
1049
1050
1051
1052
1053
1054

1055
1056
1057
1058
1059
1060
1061
1062







-
+







	else
#endif
		[delegate parser: self
		    foundComment: comment];

	[pool release];

	[cache setToCString: ""];
	[cache removeNItems: [cache count]];

	*last = *i + 1;
	state = OF_XMLPARSER_OUTSIDE_TAG;
}

/* In <!DOCTYPE ...> */
- (void)_parseInDoctypeWithBuffer: (const char*)buffer