ObjFW  Check-in [265a0928ca]

Overview
Comment:Rename OFXMLParser's _cache to _buffer.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 265a0928cab0789c77a50548a7deac0d92e0d581e45f515c10df27c8acf9c653
User & Date: js on 2013-02-18 22:00:20
Other Links: manifest | tags
Context
2013-02-18
23:05
Make OFHash a protocol. check-in: 9b2d255a3d user: js tags: trunk
22:00
Rename OFXMLParser's _cache to _buffer. check-in: 265a0928ca user: js tags: trunk
21:53
Rename -[OFStream pendingBytes]. check-in: efe7be259d user: js tags: trunk
Changes

Modified src/OFXMLParser.h from [3c72bea853] to [366806b936].

144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
144
145
146
147
148
149
150

151
152
153
154
155
156
157
158







-
+







		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;
	OFDataArray *_cache;
	OFDataArray *_buffer;
	OFString *_name, *_prefix;
	OFMutableArray *_namespaces, *_attributes;
	OFString *_attributeName, *_attributePrefix;
	char _delimiter;
	OFMutableArray *_previous;
	size_t _level;
	BOOL _acceptProlog;

Modified src/OFXMLParser.m from [ca4fab60df] to [a35aa2dad5].

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







-
+



-
-
+
+





-
-
+
+





-
+







-
-
+
+




-
-
+
+







#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 OF_INLINE void
cache_append(OFDataArray *cache, const char *string,
buffer_append(OFDataArray *buffer, const char *string,
    of_string_encoding_t encoding, size_t length)
{
	if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8))
		[cache addItems: string
			  count: length];
		[buffer addItems: string
			   count: length];
	else {
		void *pool = objc_autoreleasePoolPush();
		OFString *tmp = [OFString stringWithCString: string
						   encoding: encoding
						     length: length];
		[cache addItems: [tmp UTF8String]
			  count: [tmp UTF8StringLength]];
		[buffer addItems: [tmp UTF8String]
			   count: [tmp UTF8StringLength]];
		objc_autoreleasePoolPop(pool);
	}
}

static OFString*
transform_string(OFDataArray *cache, size_t cut, BOOL unescape,
transform_string(OFDataArray *buffer, size_t cut, BOOL unescape,
    id <OFStringXMLUnescapingDelegate> delegate)
{
	char *items;
	size_t i, length;
	BOOL hasEntities = NO;
	OFString *ret;

	items = [cache items];
	length = [cache count] - cut;
	items = [buffer items];
	length = [buffer count] - cut;

	for (i = 0; i < length; i++) {
		if (items[i] == '\r') {
			if (i + 1 < length && items[i + 1] == '\n') {
				[cache removeItemAtIndex: i];
				items = [cache items];
				[buffer removeItemAtIndex: i];
				items = [buffer items];

				i--;
				length--;
			} else
				items[i] = '\n';
		} else if (items[i] == '&')
			hasEntities = YES;
181
182
183
184
185
186
187
188

189
190
191
192
193
194
195
181
182
183
184
185
186
187

188
189
190
191
192
193
194
195







-
+







{
	self = [super init];

	@try {
		void *pool;
		OFMutableDictionary *dict;

		_cache = [[OFBigDataArray alloc] init];
		_buffer = [[OFBigDataArray alloc] init];
		_previous = [[OFMutableArray alloc] init];
		_namespaces = [[OFMutableArray alloc] init];
		_attributes = [[OFMutableArray alloc] init];

		pool = objc_autoreleasePoolPush();
		dict = [OFMutableDictionary dictionaryWithKeysAndObjects:
		    @"xml", @"http://www.w3.org/XML/1998/namespace",
208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222







-
+







	}

	return self;
}

- (void)dealloc
{
	[_cache release];
	[_buffer release];
	[_name release];
	[_prefix release];
	[_namespaces release];
	[_attributes release];
	[_attributeName release];
	[_attributePrefix release];
	[_previous release];
263
264
265
266
267
268
269
270

271
272
273
274
275
276
277
263
264
265
266
267
268
269

270
271
272
273
274
275
276
277







-
+







			_lineNumber++;

		_lastCarriageReturn = (buffer[i] == '\r');
	}

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (length - last > 0 && _state != OF_XMLPARSER_IN_TAG)
		cache_append(_cache, buffer + last, _encoding, length - last);
		buffer_append(_buffer, buffer + last, _encoding, length - last);
}

- (void)parseString: (OFString*)string
{
	[self parseBuffer: [string UTF8String]
		   length: [string UTF8StringLength]];
}
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
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







-
+

-
+

-
+









-
+







		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

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

	if ((length = *i - *last) > 0)
		cache_append(_cache, buffer + *last, _encoding, length);
		buffer_append(_buffer, buffer + *last, _encoding, length);

	if ([_cache count] > 0) {
	if ([_buffer count] > 0) {
		void *pool = objc_autoreleasePoolPush();
		OFString *characters = transform_string(_cache, 0, YES, self);
		OFString *characters = transform_string(_buffer, 0, YES, self);

		if ([_delegate respondsToSelector:
		    @selector(parser:foundCharacters:)])
			[_delegate parser: self
			  foundCharacters: characters];

		objc_autoreleasePoolPop(pool);
	}

	[_cache removeAllItems];
	[_buffer removeAllItems];

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

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







-
-
+
+
















-
+













-
-
-
+
+
+






-
+



-
-
-
-
+
+
+
+

-
+


+
-
+

-
-
+
+

-
+







{
	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);
		buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
		PI = transform_string(_buffer, 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];

		if ([_delegate respondsToSelector:
		    @selector(parser:foundProcessingInstructions:)])
			[_delegate		 parser: self
			    foundProcessingInstructions: PI];

		objc_autoreleasePoolPop(pool);

		[_cache removeAllItems];
		[_buffer 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;
	const char *bufferCString, *tmp;
	size_t length, bufferLength;
	OFString *bufferString;

	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);
		buffer_append(_buffer, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheCString = [_cache items];
	cacheLength = [_cache count];
	cacheString = [OFString stringWithUTF8String: cacheCString
					      length: cacheLength];
	bufferCString = [_buffer items];
	bufferLength = [_buffer count];
	bufferString = [OFString stringWithUTF8String: bufferCString
					       length: bufferLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
	if ((tmp = memchr(bufferCString, ':', bufferLength)) != NULL) {
		_name = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: bufferLength -
				length: cacheLength - (tmp - cacheCString) - 1];
					(tmp - bufferCString) - 1];
		_prefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
		    initWithUTF8String: bufferCString
				length: tmp - bufferCString];
	} else {
		_name = [cacheString copy];
		_name = [bufferString copy];
		_prefix = nil;
	}

	if (buffer[*i] == '>' || buffer[*i] == '/') {
		OFString *namespace;

		namespace = namespace_for_prefix(_prefix, _namespaces);
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
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







-
+
















-
+









-
-
-
+
+
+






-
+



-
-
-
-
+
+
+
+

-
+


+
-
+

-
-
+
+

-
+



-
+





-
+







				    didEndElement: _name
					   prefix: _prefix
					namespace: namespace];

			if ([_previous count] == 0)
				_finishedParsing = YES;
		} else
			[_previous addObject: cacheString];
			[_previous addObject: bufferString];

		[_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] != '/')
		[_namespaces addObject: [OFMutableDictionary dictionary]];

	objc_autoreleasePoolPop(pool);

	[_cache removeAllItems];
	[_buffer 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, *namespace;
	const char *bufferCString, *tmp;
	size_t length, bufferLength;
	OFString *bufferString, *namespace;

	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);
		buffer_append(_buffer, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheCString = [_cache items];
	cacheLength = [_cache count];
	cacheString = [OFString stringWithUTF8String: cacheCString
					      length: cacheLength];
	bufferCString = [_buffer items];
	bufferLength = [_buffer count];
	bufferString = [OFString stringWithUTF8String: bufferCString
					       length: bufferLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
	if ((tmp = memchr(bufferCString, ':', bufferLength)) != NULL) {
		_name = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: bufferLength -
				length: cacheLength - (tmp - cacheCString) - 1];
					(tmp - bufferCString) - 1];
		_prefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
		    initWithUTF8String: bufferCString
				length: tmp - bufferCString];
	} else {
		_name = [cacheString copy];
		_name = [bufferString copy];
		_prefix = nil;
	}

	if (![[_previous lastObject] isEqual: cacheString])
	if (![[_previous lastObject] isEqual: bufferString])
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

	[_previous removeLastObject];

	[_cache removeAllItems];
	[_buffer removeAllItems];

	namespace = namespace_for_prefix(_prefix, _namespaces);
	if (_prefix != nil && namespace == nil)
		@throw [OFUnboundNamespaceException
		    exceptionWithClass: [self class]
				prefix: _prefix];

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







-
-
-
+
+
+





-
+



-
-
-
+
+
+

-
+

-
-
+
+

-
+


+
-
+

-
-
+
+

-
+





-
+








/* 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;
	OFMutableString *bufferString;
	const char *bufferCString, *tmp;
	size_t length, bufferLength;

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

	if ((length = *i - *last) > 0)
		cache_append(_cache, buffer + *last, _encoding, length);
		buffer_append(_buffer, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();

	cacheString = [OFMutableString stringWithUTF8String: [_cache items]
						     length: [_cache count]];
	[cacheString deleteEnclosingWhitespaces];
	bufferString = [OFMutableString stringWithUTF8String: [_buffer items]
						      length: [_buffer count]];
	[bufferString deleteEnclosingWhitespaces];
	/* Prevent a useless copy later */
	[cacheString makeImmutable];
	[bufferString makeImmutable];

	cacheCString = [cacheString UTF8String];
	cacheLength = [cacheString UTF8StringLength];
	bufferCString = [bufferString UTF8String];
	bufferLength = [bufferString UTF8StringLength];

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
	if ((tmp = memchr(bufferCString, ':', bufferLength)) != NULL) {
		_attributeName = [[OFString alloc]
		    initWithUTF8String: tmp + 1
				length: bufferLength -
				length: cacheLength - (tmp - cacheCString) - 1];
					(tmp - bufferCString) - 1];
		_attributePrefix = [[OFString alloc]
		    initWithUTF8String: cacheCString
				length: tmp - cacheCString];
		    initWithUTF8String: bufferCString
				length: tmp - bufferCString];
	} else {
		_attributeName = [cacheString copy];
		_attributeName = [bufferString copy];
		_attributePrefix = nil;
	}

	objc_autoreleasePoolPop(pool);

	[_cache removeAllItems];
	[_buffer removeAllItems];

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

/* Expecting delimiter */
- (void)OF_parseExpectDelimiterWithBuffer: (const char*)buffer
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
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







-
+


-
+















-
+







	OFString *attributeValue;
	size_t length;

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

	if ((length = *i - *last) > 0)
		cache_append(_cache, buffer + *last, _encoding, length);
		buffer_append(_buffer, buffer + *last, _encoding, length);

	pool = objc_autoreleasePoolPush();
	attributeValue = transform_string(_cache, 0, YES, self);
	attributeValue = transform_string(_buffer, 0, YES, self);

	if (_attributePrefix == nil && [_attributeName isEqual: @"xmlns"])
		[[_namespaces lastObject] setObject: attributeValue
					     forKey: @""];
	if ([_attributePrefix isEqual: @"xmlns"])
		[[_namespaces lastObject] setObject: attributeValue
					     forKey: _attributeName];

	[_attributes addObject:
	    [OFXMLAttribute attributeWithName: _attributeName
				    namespace: _attributePrefix
				  stringValue: attributeValue]];

	objc_autoreleasePoolPop(pool);

	[_cache removeAllItems];
	[_buffer removeAllItems];
	[_attributeName release];
	[_attributePrefix release];
	_attributeName = _attributePrefix = nil;

	*last = *i + 1;
	_state = OF_XMLPARSER_IN_TAG;
}
956
957
958
959
960
961
962
963
964


965
966
967
968
969
970
971
972

973
974
975
976
977
978
979
959
960
961
962
963
964
965


966
967
968
969
970
971
972
973
974

975
976
977
978
979
980
981
982







-
-
+
+







-
+







		_level = (buffer[*i] == ']' ? 1 : 0);

		return;
	}

	pool = objc_autoreleasePoolPush();

	cache_append(_cache, buffer + *last, _encoding, *i - *last);
	CDATA = transform_string(_cache, 2, NO, nil);
	buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
	CDATA = transform_string(_buffer, 2, NO, nil);

	if ([_delegate respondsToSelector: @selector(parser:foundCDATA:)])
		[_delegate parser: self
		       foundCDATA: CDATA];

	objc_autoreleasePoolPop(pool);

	[_cache removeAllItems];
	[_buffer removeAllItems];

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

/* Comment */
- (void)OF_parseInCommentOpeningWithBuffer: (const char*)buffer
1011
1012
1013
1014
1015
1016
1017
1018
1019


1020
1021
1022
1023
1024
1025
1026
1027

1028
1029
1030
1031
1032
1033
1034
1014
1015
1016
1017
1018
1019
1020


1021
1022
1023
1024
1025
1026
1027
1028
1029

1030
1031
1032
1033
1034
1035
1036
1037







-
-
+
+







-
+








	if (buffer[*i] != '>')
		@throw [OFMalformedXMLException exceptionWithClass: [self class]
							    parser: self];

	pool = objc_autoreleasePoolPush();

	cache_append(_cache, buffer + *last, _encoding, *i - *last);
	comment = transform_string(_cache, 2, NO, nil);
	buffer_append(_buffer, buffer + *last, _encoding, *i - *last);
	comment = transform_string(_buffer, 2, NO, nil);

	if ([_delegate respondsToSelector: @selector(parser:foundComment:)])
		[_delegate parser: self
		     foundComment: comment];

	objc_autoreleasePoolPop(pool);

	[_cache removeAllItems];
	[_buffer removeAllItems];

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

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