ObjFW  Check-in [1bbc11d7a5]

Overview
Comment:A few new string methods.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1bbc11d7a5e9d11685a13cba93e073f55bd87bee777c2ec705a4ee854549f5e6
User & Date: js on 2009-07-19 13:04:02
Other Links: manifest | tags
Context
2009-07-19
13:11
Emphasize UTF-8 encoding of C strings in documentation. check-in: df75a3df04 user: js tags: trunk
13:04
A few new string methods. check-in: 1bbc11d7a5 user: js tags: trunk
2009-07-17
20:11
Forgot to hg add OFXMLParser test. Fixed. check-in: cb145d6634 user: js tags: trunk
Changes

Modified src/OFString.h from [f69ee67b87] to [a4f1a280cf].

167
168
169
170
171
172
173
























174
175
176
177
178
179
180
 * \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;







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







167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 * \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;

/**
 * Creates a new string by appending another string.
 *
 * \param str The string to append
 * \return A new autoreleased OFString with the specified string appended
 */
- (OFString*)stringByAppendingString: (OFString*)str;

/**
 * Checks whether the string has the specified prefix.
 *
 * \param prefix The prefix to check for
 * \return A boolean whether the string has the specified prefix
 */
- (BOOL)hasPrefix: (OFString*)prefix;

/**
 * Checks whether the string has the specified suffix.
 *
 * \param suffix The suffix to check for
 * \return A boolean whether the string has the specified suffix
 */
- (BOOL)hasSuffix: (OFString*)suffix;

/**
 * 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 [a8bcd353b2] to [b274f55ce4].

440
441
442
443
444
445
446


























447
448
449
450
451
452
453

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







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







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

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

	return [OFString stringWithCString: string + start
				 andLength: end - start];
}

- (OFString*)stringByAppendingString: (OFString*)str
{
	return [[OFMutableString stringWithString: self] appendString: str];
}

- (BOOL)hasPrefix: (OFString*)prefix
{
	size_t len = [prefix length];

	if (len > length)
		return NO;

	return (memcmp(string, [prefix cString], len) ? NO : YES);
}

- (BOOL)hasSuffix: (OFString*)suffix
{
	size_t len = [suffix length];

	if (len > length)
		return NO;

	return (memcmp(string + (length - len), [suffix cString], len)
	    ? NO : YES);
}

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

Modified tests/OFString/OFString.m from [a21e69eb00] to [94044c3abd].

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 62
#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 68
#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);						\
140
141
142
143
144
145
146







147
148
149
150
151
152
153
	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: @""])







>
>
>
>
>
>
>







140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	CHECK_EXCEPT([@"foo" substringFromIndex: 2
					toIndex: 4], OFOutOfRangeException)
	CHECK_EXCEPT([@"foo" substringFromIndex: 4
					toIndex: 4], OFOutOfRangeException)
	CHECK_EXCEPT([@"foo" substringFromIndex: 2
					toIndex: 0], OFInvalidArgumentException)

	/* Misc tests */
	CHECK([[@"foo" stringByAppendingString: @"bar"] isEqual: @"foobar"])
	CHECK([@"foobar" hasPrefix: @"foo"])
	CHECK([@"foobar" hasSuffix: @"bar"])
	CHECK(![@"foobar" hasPrefix: @"foobar0"])
	CHECK(![@"foobar" hasSuffix: @"foobar0"])

	/* 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: @""])
213
214
215
216
217
218
219


220
221
222
223
224
225
226
227
	CHECK([[@"y" stringByXMLUnescaping] isEqual: @"y"]);
	CHECK([[@"ä" stringByXMLUnescaping] isEqual: @"ä"]);
	CHECK([[@"€" stringByXMLUnescaping] isEqual: @"€"]);
	CHECK([[@"𝄞" stringByXMLUnescaping] isEqual: @"𝄞"]);

	CHECK_EXCEPT([@"&#;" stringByXMLUnescaping], OFInvalidEncodingException)
	CHECK_EXCEPT([@"&#x;" stringByXMLUnescaping],


	    OFInvalidEncodingException)
	CHECK_EXCEPT([@"&#xg;" stringByXMLUnescaping],
	    OFInvalidEncodingException)

	puts("");

	return 0;
}







>
>








220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	CHECK([[@"y" stringByXMLUnescaping] isEqual: @"y"]);
	CHECK([[@"ä" stringByXMLUnescaping] isEqual: @"ä"]);
	CHECK([[@"€" stringByXMLUnescaping] isEqual: @"€"]);
	CHECK([[@"𝄞" stringByXMLUnescaping] isEqual: @"𝄞"]);

	CHECK_EXCEPT([@"&#;" stringByXMLUnescaping], OFInvalidEncodingException)
	CHECK_EXCEPT([@"&#x;" stringByXMLUnescaping],
	    OFInvalidEncodingException)
	CHECK_EXCEPT([@"&#g;" stringByXMLUnescaping],
	    OFInvalidEncodingException)
	CHECK_EXCEPT([@"&#xg;" stringByXMLUnescaping],
	    OFInvalidEncodingException)

	puts("");

	return 0;
}