ObjFW  Check-in [9758677802]

Overview
Comment:Implement -[indexOf{First,Last}OccurrenceOfString:] for OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 975867780222f3b942d53acc10f9dbb2fc5b8235243f99e37772939f34a19a9f
User & Date: js on 2009-06-30 22:21:30
Other Links: manifest | tags
Context
2009-07-14
17:14
Get rid of strcmp and strlen calls in OFString tests. check-in: 6a2551a704 user: js tags: trunk
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
Changes

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

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;















/**
 * \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







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







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 *
 * \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 str The string to search
 * \return The index of the first occurrence of the string or SIZE_MAX if it
 *	   wasn't found
 */
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)str;

/**
 * \param str The string to search
 * \return The index of the last occurrence of the string or SIZE_MAX if it
 *	   wasn't found
 */
- (size_t)indexOfLastOccurrenceOfString: (OFString*)str;

/**
 * \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

Modified src/OFString.m from [26f5edf3f9] to [6fce01c941].

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










































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







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







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
353
354
355
356
357
358
359
360
	OF_HASH_INIT(hash);
	for (i = 0; i < length; i++)
		OF_HASH_ADD(hash, string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)str
{
	const char *str_c = [str cString];
	size_t str_len = [str length];
	size_t i;

	if (str_len == 0)
		return 0;

	if (str_len > length)
		return SIZE_MAX;

	for (i = 0; i <= length - str_len; i++)
		if (!memcmp(string + i, str_c, str_len))
			return i;

	return SIZE_MAX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)str
{
	const char *str_c = [str cString];
	size_t str_len = [str length];
	size_t i;

	if (str_len == 0)
		return length;

	if (str_len > length)
		return SIZE_MAX;

	for (i = length - str_len;; i--) {
		if (!memcmp(string + i, str_c, str_len))
			return i;

		/* Did not match and we're at the last char */
		if (i == 0)
			return SIZE_MAX;
	}
}

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

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

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);						\







|







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 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);						\
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"))











	/* 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)







>
>
>
>
>
>
>
>
>
>







97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	/* 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"))

	/* Find index tests */
	CHECK([@"foo" indexOfFirstOccurrenceOfString: @"oo"] == 1)
	CHECK([@"foo" indexOfLastOccurrenceOfString: @"oo"] == 1)
	CHECK([@"foo" indexOfFirstOccurrenceOfString: @"o"] == 1)
	CHECK([@"foo" indexOfLastOccurrenceOfString: @"o"] == 2)
	CHECK([@"foo" indexOfFirstOccurrenceOfString: @"f"] == 0)
	CHECK([@"foo" indexOfLastOccurrenceOfString: @"f"] == 0)
	CHECK([@"foo" indexOfFirstOccurrenceOfString: @"x"] == SIZE_MAX)
	CHECK([@"foo" indexOfLastOccurrenceOfString: @"x"] == SIZE_MAX)

	/* 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)