ObjFW  Check-in [bc71bde0dd]

Overview
Comment:Add replaceOccurrencesOfString:withString: to OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bc71bde0dd6964df93a30eaf12bb43ca878426e34dd2e251113672dc28de9e5b
User & Date: js on 2009-05-23 21:02:09
Other Links: manifest | tags
Context
2009-05-23
21:53
A few renames. check-in: 526d04018d user: js tags: trunk
21:02
Add replaceOccurrencesOfString:withString: to OFString. check-in: bc71bde0dd user: js tags: trunk
20:32
Fix splitWithDelimiter:. check-in: cabf6ee9e7 user: js tags: trunk
Changes

Modified src/OFMutableString.m from [fa80b4b8f3] to [da1cf273ef].

241
242
243
244
245
246
247
248
249





















































250

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: isa];

	while (--p >= string)
		*p = tolower((int)*p);

	return self;
}





















































@end









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

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

	if (is_utf8)
		@throw [OFInvalidEncodingException newWithClass: isa];

	while (--p >= string)
		*p = tolower((int)*p);

	return self;
}

- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl
{
	const char *str_c = [str cString];
	const char *repl_c = [repl cString];
	size_t str_len = [str length];
	size_t repl_len = [repl length];
	size_t i, last, tmp_len;
	char *tmp;

	if (str_len > length)
		return self;

	tmp = NULL;
	tmp_len = 0;

	for (i = 0, last = 0; i <= length - str_len; i++) {
		if (memcmp(string + i, str_c, str_len))
			continue;

		@try {
			tmp = [self resizeMem: tmp
				       toSize: tmp_len + i - last +
					       repl_len + 1];
		} @catch (OFException *e) {
			[self freeMem: tmp];
			@throw e;
		}
		memcpy(tmp + tmp_len, string + last, i - last);
		memcpy(tmp + tmp_len + i - last, repl_c, repl_len);
		tmp_len += i - last + repl_len;
		i += str_len - 1;
		last = i + 1;
	}

	@try {
		tmp = [self resizeMem: tmp
			       toSize: tmp_len + length - last + 1];
	} @catch (OFException *e) {
		[self freeMem: tmp];
		@throw e;
	}
	memcpy(tmp + tmp_len, string + last, length - last);
	tmp_len += length - last;
	tmp[tmp_len] = 0;

	[self freeMem: string];
	string = tmp;
	length = tmp_len;

	return self;
}
@end

Modified src/OFString.h from [ffb53e2845] to [54ab126f71].

160
161
162
163
164
165
166









167
168
169
170
171
172
173
174
175
176
177
178
- upper;

/**
 * Lower the OFString.
 */
- lower;










/**
 * 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;
@end

#import "OFConstString.h"
#import "OFMutableString.h"
#import "OFURLEncoding.h"







>
>
>
>
>
>
>
>
>












160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
- upper;

/**
 * Lower the OFString.
 */
- lower;

/**
 * Replaces all occurrences of a string with another string.
 *
 * \param str The string to replace
 * \param repl The string with which it should be replaced
 */
- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl;

/**
 * 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;
@end

#import "OFConstString.h"
#import "OFMutableString.h"
#import "OFURLEncoding.h"

Modified src/OFString.m from [4a9dfef866] to [f339ad2a58].

323
324
325
326
327
328
329







330
331
332
333
334
335
336
- upper
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- lower







{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- (OFArray*)splitWithDelimiter: (OFString*)delimiter
{







>
>
>
>
>
>
>







323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
- upper
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- lower
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- (OFArray*)splitWithDelimiter: (OFString*)delimiter
{

Modified tests/OFString/OFString.m from [c6f6b5b983] to [41d0f6db68].

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 25
#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 27
#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);						\
109
110
111
112
113
114
115









116
117
118
119
120
	CHECK([[a objectAtIndex: j++] isEqual: @""])
	CHECK([[a objectAtIndex: j++] isEqual: @""])

	CHECK([[@"foo\"ba'_$" urlencode] isEqual: @"foo%22ba%27_%24"])
	CHECK([[@"foo%20bar%22%24" urldecode] isEqual: @"foo bar\"$"])
	CHECK_EXCEPT([@"foo%bar" urldecode], OFInvalidEncodingException)
	CHECK_EXCEPT([@"foo%FFbar" urldecode], OFInvalidEncodingException)










	puts("");

	return 0;
}







>
>
>
>
>
>
>
>
>





109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	CHECK([[a objectAtIndex: j++] isEqual: @""])
	CHECK([[a objectAtIndex: j++] isEqual: @""])

	CHECK([[@"foo\"ba'_$" urlencode] isEqual: @"foo%22ba%27_%24"])
	CHECK([[@"foo%20bar%22%24" urldecode] isEqual: @"foo bar\"$"])
	CHECK_EXCEPT([@"foo%bar" urldecode], OFInvalidEncodingException)
	CHECK_EXCEPT([@"foo%FFbar" urldecode], OFInvalidEncodingException)

	s1 = [@"asd fo asd fofo asd" mutableCopy];
	[s1 replaceOccurrencesOfString: @"fo"
			    withString: @"foo"];
	CHECK([s1 isEqual: @"asd foo asd foofoo asd"])
	s1 = [@"XX" mutableCopy];
	[s1 replaceOccurrencesOfString: @"X"
			    withString: @"XX"];
	CHECK([s1 isEqual: @"XXXX"])

	puts("");

	return 0;
}