ObjFW  Diff

Differences From Artifact [b060def371]:

To Artifact [c68cb5d129]:


35
36
37
38
39
40
41
















42
43
44
45
46
47
48
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#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,
    of_string_encoding_t encoding, size_t length)
{
	if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8))
		[cache appendCStringWithoutUTF8Checking: string
						 length: length];
	else {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		[cache appendString: [OFString stringWithCString: string
							encoding: encoding
							  length: length]];
		[pool release];
	}
}

static OFString*
transform_string(OFMutableString *cache, size_t cut, BOOL unescape,
    OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
	[cache replaceOccurrencesOfString: @"\r\n"
			       withString: @"\n"];
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
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







-
+







-
+





+
-
+


-
+




-
-
-
+







}

- (void)setDelegate: (id <OFXMLParserDelegate>)delegate_
{
	OF_SETTER(delegate, delegate_, YES, NO)
}

- (void)parseBuffer: (const char*)buf
- (void)parseBuffer: (const char*)buffer
	 withLength: (size_t)length
{
	size_t i, last = 0;

	for (i = 0; i < length; i++) {
		size_t j = i;

		lookupTable[state](self, selectors[state], buf, &i, &last);
		lookupTable[state](self, selectors[state], buffer, &i, &last);

		/* Ensure we don't count this character twice */
		if (i != j)
			continue;

		if (buffer[i] == '\r' || (buffer[i] == '\n' &&
		if (buf[i] == '\r' || (buf[i] == '\n' && !lastCarriageReturn))
		    !lastCarriageReturn))
			lineNumber++;

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

	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (length - last > 0 && state != OF_XMLPARSER_IN_TAG)
		[cache appendCStringWithoutUTF8Checking: buf + last
					       encoding: encoding
						 length: length - last];
		cache_append(cache, buffer + last, encoding, length - last);
}

- (void)parseString: (OFString*)string
{
	[self parseBuffer: [string cString]
	       withLength: [string cStringLength]];
}
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
312
313
314
315
316
317
318

319


320
321
322
323
324
325
326







-
+
-
-







		@throw [OFMalformedXMLException newWithClass: isa
						      parser: self];

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

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

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

		pool = [[OFAutoreleasePool alloc] init];
		characters = transform_string(cache, 0, YES, self);
465
466
467
468
469
470
471
472

473
474
475
476
477
478
479
480
481
478
479
480
481
482
483
484

485


486
487
488
489
490
491
492







-
+
-
-







{
	if (buffer[*i] == '?')
		level = 1;
	else if (level == 1 && buffer[*i] == '>') {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFString *pi;

		[cache appendCStringWithoutUTF8Checking: buffer + *last
		cache_append(cache, buffer + *last, encoding, *i - *last);
					       encoding: encoding
						 length: *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 _parseXMLProcessingInstructions: pi])
				@throw [OFMalformedXMLException
504
505
506
507
508
509
510
511

512
513
514
515
516
517
518
519
520
515
516
517
518
519
520
521

522


523
524
525
526
527
528
529







-
+
-
-







	size_t length, cacheLength;

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

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

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
599
600
601
602
603
604
605
606

607
608
609
610
611
612
613
614
615
608
609
610
611
612
613
614

615


616
617
618
619
620
621
622







-
+
-
-







	OFString *ns;

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

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

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
		name = [[OFString alloc] initWithCString: tmp + 1
						  length: cacheLength -
751
752
753
754
755
756
757
758

759
760
761
762
763
764
765
766
767
758
759
760
761
762
763
764

765


766
767
768
769
770
771
772







-
+
-
-







	const char *cacheCString, *tmp;
	size_t length, cacheLength;

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

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

	[cache deleteLeadingAndTrailingWhitespaces];

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

	if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
810
811
812
813
814
815
816
817

818
819
820
821
822
823
824
825
826
815
816
817
818
819
820
821

822


823
824
825
826
827
828
829







-
+
-
-







	OFString *attributeValue;
	size_t length;

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

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

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

	if (attributePrefix == nil && [attributeName isEqual: @"xmlns"])
		[[namespaces lastObject] setObject: attributeValue
					    forKey: @""];
940
941
942
943
944
945
946
947

948
949
950
951
952
953
954
955
956
943
944
945
946
947
948
949

950


951
952
953
954
955
956
957







-
+
-
-







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

		return;
	}

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buffer + *last
	cache_append(cache, buffer + *last, encoding, *i - *last);
				       encoding: encoding
					 length: *i - *last];
	CDATA = transform_string(cache, 2, NO, nil);

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (CDATAHandler != NULL)
		CDATAHandler(self, CDATA);
	else
#endif
1001
1002
1003
1004
1005
1006
1007
1008

1009
1010
1011
1012
1013
1014
1015
1016
1017
1002
1003
1004
1005
1006
1007
1008

1009


1010
1011
1012
1013
1014
1015
1016







-
+
-
-








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

	pool = [[OFAutoreleasePool alloc] init];

	[cache appendCStringWithoutUTF8Checking: buffer + *last
	cache_append(cache, buffer + *last, encoding, *i - *last);
				       encoding: encoding
					 length: *i - *last];
	comment = transform_string(cache, 2, NO, nil);

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
	if (commentHandler != NULL)
		commentHandler(self, comment);
	else
#endif