ObjFW  Check-in [2496d46c35]

Overview
Comment:Add methods to remove leading or trailing whitespaces or both.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2496d46c35c2a2eee2b4b16e4e7fbfbcbf51d54360b87c92856ef4cb07a416cb
User & Date: js on 2009-06-03 03:54:20
Other Links: manifest | tags
Context
2009-06-03
18:21
No need to add LIB_LDFLAGS to LDFLAGS, the buildsys handles this. check-in: ab934c0805 user: js tags: trunk
03:54
Add methods to remove leading or trailing whitespaces or both. check-in: 2496d46c35 user: js tags: trunk
2009-06-02
20:15
No need to override -[release] in OFAutoreleasePool. check-in: e6f6113b5c user: js tags: trunk
Changes

Modified src/OFMutableString.m from [6f55d4cb4b] to [3cf489fd9b].

291
292
293
294
295
296
297
298
299
300





















































































301
	memcpy(tmp + tmp_len, string + last, length - last);
	tmp_len += length - last;
	tmp[tmp_len] = 0;

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

	return self;
}





















































































@end










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

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
	memcpy(tmp + tmp_len, string + last, length - last);
	tmp_len += length - last;
	tmp[tmp_len] = 0;

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

	return self;
}

- removeLeadingWhitespaces
{
	size_t i;

	for (i = 0; i < length; i++)
		if (string[i] != ' ' && string[i] != '\t')
			break;

	length -= i;
	memmove(string, string + i, length);
	string[length] = '\0';

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e dealloc];
	}

	return self;
}

- removeTrailingWhitespaces
{
	size_t d;
	char *p;

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t')
			break;

		*p = '\0';
		d++;
	}

	length -= d;

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e dealloc];
	}

	return self;
}

- removeLeadingAndTrailingWhitespaces
{
	size_t d, i;
	char *p;

	d = 0;
	for (p = string + length - 1; p >= string; p--) {
		if (*p != ' ' && *p != '\t')
			break;

		*p = '\0';
		d++;
	}

	length -= d;

	for (i = 0; i < length; i++)
		if (string[i] != ' ' && string[i] != '\t')
			break;

	length -= i;
	memmove(string, string + i, length);
	string[length] = '\0';

	@try {
		string = [self resizeMemory: string
				     toSize: length + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e dealloc];
	}

	return self;
}
@end

Modified src/OFString.h from [0f37fc8589] to [444a655483].

185
186
187
188
189
190
191















192
193
194
195
196
197
198
199
200
201
202
203
 *
 * \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"







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












185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 *
 * \param str The string to replace
 * \param repl The string with which it should be replaced
 */
- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl;

/**
 * Removes all whitespaces at the beginning of a string.
 */
- removeLeadingWhitespaces;

/**
 * Removes all whitespaces at the end of a string.
 */
- removeTrailingWhitespaces;

/**
 * Removes all whitespaces at the beginning and the end of a string.
 */
- removeLeadingAndTrailingWhitespaces;

/**
 * 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 [346940a245] to [24b458f3b5].

356
357
358
359
360
361
362


















363
364
365
366
367
368
369
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- replaceOccurrencesOfString: (OFString*)str
		  withString: (OFString*)repl


















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

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







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







356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

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

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

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

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

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

Modified tests/OFString/OFString.m from [894fde2106] to [8579a7043b].

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







|







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 33
#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);						\
120
121
122
123
124
125
126














127
128
129
130
			    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;
}







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




120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
			    withString: @"foo"];
	CHECK([s1 isEqual: @"asd foo asd foofoo asd"])
	s1 = [@"XX" mutableCopy];
	[s1 replaceOccurrencesOfString: @"X"
			    withString: @"XX"];
	CHECK([s1 isEqual: @"XXXX"])

	s1 = [@"  \t\t \tasd  \t \t\t" mutableCopy];
	s2 = [s1 mutableCopy];
	s3 = [s1 mutableCopy];
	CHECK([[s1 removeLeadingWhitespaces] isEqual: @"asd  \t \t\t"])
	CHECK([[s2 removeTrailingWhitespaces] isEqual: @"  \t\t \tasd"])
	CHECK([[s3 removeLeadingAndTrailingWhitespaces] isEqual: @"asd"])

	s1 = [@" \t\t  \t\t  \t \t" mutableCopy];
	s2 = [s1 mutableCopy];
	s3 = [s1 mutableCopy];
	CHECK([[s1 removeLeadingWhitespaces] isEqual: @""])
	CHECK([[s2 removeTrailingWhitespaces] isEqual: @""])
	CHECK([[s3 removeLeadingAndTrailingWhitespaces] isEqual: @""])

	puts("");

	return 0;
}