ObjFW  Check-in [cfb401ed3a]

Overview
Comment:Implement -[substringFromIndex:toIndex:] for OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cfb401ed3ad7b7b5bc44543420496ce367c4044d8eaa7748ee945c3a0557336a
User & Date: js on 2009-06-30 20:12:20
Other Links: manifest | tags
Context
2009-06-30
22:21
Implement -[indexOf{First,Last}OccurrenceOfString:] for OFString. check-in: 9758677802 user: js tags: trunk
20:12
Implement -[substringFromIndex:toIndex:] for OFString. check-in: cfb401ed3a user: js tags: trunk
14:22
Add +[instanceMethodForSelector:] to OFObject. check-in: 0707c56762 user: js tags: trunk
Changes

Modified src/OFString.h from [bbf5506e5d] to [1821b8be7d].

122
123
124
125
126
127
128









129
130
131
132
133
134
135
 *
 * \param obj An object to compare with
 * \return An integer which is the result of the comparison, see for example
 *	   strcmp
 */
- (int)compare: (id)obj;










/**
 * Splits an OFString into an OFArray of OFStrings.
 *
 * \param delimiter The delimiter for splitting
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;







>
>
>
>
>
>
>
>
>







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 *
 * \param obj An object to compare with
 * \return An integer which is the result of the comparison, see for example
 *	   strcmp
 */
- (int)compare: (id)obj;

/**
 * \param start The index where the substring starts
 * \param end The index where the substring ends.
 *	      This points BEHIND the last character!
 * \return The substring as a new autoreleased OFString
 */
- (OFString*)substringFromIndex: (size_t)start
			toIndex: (size_t)end;

/**
 * Splits an OFString into an OFArray of OFStrings.
 *
 * \param delimiter The delimiter for splitting
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;

Modified src/OFString.m from [248b3d5447] to [26f5edf3f9].

306
307
308
309
310
311
312

































313
314
315
316
317
318
319
	OF_HASH_INIT(hash);
	for (i = 0; i < length; i++)
		OF_HASH_ADD(hash, string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}


































- (OFArray*)splitWithDelimiter: (OFString*)delimiter
{
	OFAutoreleasePool *pool;
	OFArray *array = nil;
	OFString *str;
	const char *delim = [delimiter cString];







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







306
307
308
309
310
311
312
313
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
347
348
349
350
351
352
	OF_HASH_INIT(hash);
	for (i = 0; i < length; i++)
		OF_HASH_ADD(hash, string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (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];

Modified tests/OFString/OFString.m from [fa55c2ad9d] to [132079fa19].

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

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

#define NUM_TESTS 34
#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);						\







|







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

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

#define NUM_TESTS 39
#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);						\
97
98
99
100
101
102
103












104
105
106
107
108
109
110
	/* Format tests */
	s1 = [OFMutableString stringWithFormat: @"%s: %d", "test", 123];
	CHECK(!strcmp([s1 cString], "test: 123"))

	[s1 appendWithFormat: @"%02X", 15];
	CHECK(!strcmp([s1 cString], "test: 1230F"))













	/* Split tests */
	a = [@"fooXXbarXXXXbazXXXX" splitWithDelimiter: @"XX"];
	CHECK([[a objectAtIndex: j++] isEqual: @"foo"])
	CHECK([[a objectAtIndex: j++] isEqual: @"bar"])
	CHECK([[a objectAtIndex: j++] isEqual: @""])
	CHECK([[a objectAtIndex: j++] isEqual: @"baz"])
	CHECK([[a objectAtIndex: j++] isEqual: @""])







>
>
>
>
>
>
>
>
>
>
>
>







97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	/* Format tests */
	s1 = [OFMutableString stringWithFormat: @"%s: %d", "test", 123];
	CHECK(!strcmp([s1 cString], "test: 123"))

	[s1 appendWithFormat: @"%02X", 15];
	CHECK(!strcmp([s1 cString], "test: 1230F"))

	/* Substring tests */
	CHECK([[@"foo" substringFromIndex: 1
				  toIndex: 2] isEqual: @"o"]);
	CHECK([[@"foo" substringFromIndex: 3
				  toIndex: 3] isEqual: @""]);
	CHECK_EXCEPT([@"foo" substringFromIndex: 2
					toIndex: 4], OFOutOfRangeException)
	CHECK_EXCEPT([@"foo" substringFromIndex: 4
					toIndex: 4], OFOutOfRangeException)
	CHECK_EXCEPT([@"foo" substringFromIndex: 2
					toIndex: 0], OFInvalidArgumentException)

	/* Split tests */
	a = [@"fooXXbarXXXXbazXXXX" splitWithDelimiter: @"XX"];
	CHECK([[a objectAtIndex: j++] isEqual: @"foo"])
	CHECK([[a objectAtIndex: j++] isEqual: @"bar"])
	CHECK([[a objectAtIndex: j++] isEqual: @""])
	CHECK([[a objectAtIndex: j++] isEqual: @"baz"])
	CHECK([[a objectAtIndex: j++] isEqual: @""])