ObjFW  Check-in [7bb3494ef9]

Overview
Comment:Optimize some code by using the new stringWithCString:andLength: method.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7bb3494ef9f039631ef9e85c6de2105012a0ce0ec0306bf915b2fd9b755271f9
User & Date: js on 2009-07-14 17:51:02
Other Links: manifest | tags
Context
2009-07-14
18:57
Add OFString (OFXMLUnescaping) category and API for OFXMLParser. check-in: d8684fc232 user: js tags: trunk
17:51
Optimize some code by using the new stringWithCString:andLength: method. check-in: 7bb3494ef9 user: js tags: trunk
17:32
Add methods to handle C strings with length to OF(Mutable)String. check-in: dc7bb2d594 user: js tags: trunk
Changes

Modified src/OFString.m from [36e450e8e9] to [b0bbce5a9e].

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
			return SIZE_MAX;
	}
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
{
	char *tmp;
	size_t len;
	OFString *ret;

	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	if (end > length)
		@throw [OFOutOfRangeException newWithClass: isa];

	len = end - start;

	if ((tmp = malloc(len + 1)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
						    andSize: len + 1];

	if (len)
		memcpy(tmp, string + start, len);
	tmp[len] = 0;

	@try {
		ret = [OFString stringWithCString: tmp];
	} @finally {
		free(tmp);
	}

	return ret;
}

- (OFArray*)splitWithDelimiter: (OFString*)delimiter
{
	OFAutoreleasePool *pool;
	OFArray *array = nil;
	OFString *str;
	const char *delim = [delimiter cString];
	size_t delim_len = [delimiter length];
	size_t i, last;

	array = [OFMutableArray array];
	pool = [[OFAutoreleasePool alloc] init];

	if (delim_len > length) {
		str = [[self copy] autorelease];
		[array addObject: str];
		[pool release];

		return array;
	}

	for (i = 0, last = 0; i <= length - delim_len; i++) {
		char *tmp;

		if (memcmp(string + i, delim, delim_len))
			continue;

		/*
		 * We can't use [self allocMemoryWithSize:] here as self might
		 * be a @""-literal.
		 */
		if ((tmp = malloc(i - last + 1)) == NULL)
			@throw [OFOutOfMemoryException
			    newWithClass: isa
				 andSize: i - last + 1];
		memcpy(tmp, string + last, i - last);
		tmp[i - last] = '\0';
		@try {
			str = [OFString stringWithCString: tmp];
		} @finally {
			free(tmp);
		}

		[array addObject: str];
		[pool releaseObjects];

		i += delim_len - 1;
		last = i + 1;
	}
	[array addObject: [OFString stringWithCString: string + last]];

	[pool release];








<
<
<
<







<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
|
<





|
<








<
|






<
<



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|







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
			return SIZE_MAX;
	}
}

- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end
{




	if (start > end)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	if (end > length)
		@throw [OFOutOfRangeException newWithClass: isa];












	return [OFString stringWithCString: string + start



				 andLength: end - start];

}

- (OFArray*)splitWithDelimiter: (OFString*)delimiter
{
	OFAutoreleasePool *pool;
	OFArray *array;

	const char *delim = [delimiter cString];
	size_t delim_len = [delimiter length];
	size_t i, last;

	array = [OFMutableArray array];
	pool = [[OFAutoreleasePool alloc] init];

	if (delim_len > length) {

		[array addObject: [[self copy] autorelease]];
		[pool release];

		return array;
	}

	for (i = 0, last = 0; i <= length - delim_len; i++) {


		if (memcmp(string + i, delim, delim_len))
			continue;

















		[array addObject: [OFString stringWithCString: string + last

						    andLength: i - last]];
		i += delim_len - 1;
		last = i + 1;
	}
	[array addObject: [OFString stringWithCString: string + last]];

	[pool release];

Modified tests/OFString/OFString.m from [2fcb062a9a] to [b1315ec8f9].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 47
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 49
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\