ObjFW  Check-in [029d4af91d]

Overview
Comment:Add - splitWithDelimiter: to OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 029d4af91da1ecef3e176bcc2e67617bf3a144693dd7341f8771f64aa08d2d12
User & Date: js on 2009-05-05 15:04:40
Other Links: manifest | tags
Context
2009-05-05
17:59
Rename - objects / - items in OFArray / OFDataArray to - count. check-in: 64bc94cdb3 user: js tags: trunk
15:04
Add - splitWithDelimiter: to OFString. check-in: 029d4af91d user: js tags: trunk
14:00
There is a good reason to return id and not OFObject*. check-in: 9338313079 user: js tags: trunk
Changes

Modified src/OFString.h from [71a103f35e] to [a1e4b20180].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23







+







 * the packaging of this file.
 */

#include <stdio.h>
#include <stdarg.h>

#import "OFObject.h"
#import "OFArray.h"

/**
 * A class for managing strings.
 */
@interface OFString: OFObject
{
	char	     *string;
145
146
147
148
149
150
151








152
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161







+
+
+
+
+
+
+
+

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

Modified src/OFString.m from [c5b8e74c51] to [c3388d8ecc].

8
9
10
11
12
13
14

15
16
17
18

19
20
21
22
23
24
25
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27







+




+







 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#import "OFString.h"
#import "OFMutableString.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFString
+ string
{
	return [[[OFMutableString alloc] init] autorelease];
150
151
152
153
154
155
156























































157
152
153
154
155
156
157
158
159
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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

}

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

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

	@try {
		pool = [[OFAutoreleasePool alloc] init];
	} @catch (OFException *e) {
		[array release];
		@throw e;
	}

	@try {
		for (i = 0, last = 0; i <= length; i++) {
			if (OF_UNLIKELY(i == length ||
			    !memcmp(string + i, delim, delim_len))) {
				OFString *str;
				char *tmp;

				/*
				 * We can't use [self allocWithSize:] here as
				 * self might be a @""-literal.
				 */
				if ((tmp = malloc(i - last + 1)) == NULL)
					@throw [OFNoMemException
					    newWithClass: isa
						 andSize: i - last + 1];
				memcpy(tmp, string + last, i - last);
				tmp[i - last] = '\0';
				@try {
					str = [OFString stringWithCString: tmp];
				} @finally {
					free(tmp);
				}

				[array add: str];
				[pool releaseObjects];

				i += delim_len - 1;
				last = i + 1;
			}
		}
	} @catch (OFException *e) {
		[array release];
		@throw e;
	} @finally {
		[pool release];
	}

	return [array autorelease];
}
@end

Modified tests/OFString/OFString.m from [a863061368] to [4f6e666db1].

11
12
13
14
15
16
17

18

19

20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36







+
-
+

+







-
+








#import "config.h"

#include <stdio.h>
#include <string.h>

#import "OFString.h"
#import "OFConstString.h"
#import "OFExceptions.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

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

#define NUM_TESTS 15
#define NUM_TESTS 21
#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);						\
49
50
51
52
53
54
55

56
57
58
59
60
61

62
63
64
65
66
67
68
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72







+






+







	}								\
	i++;

int
main()
{
	size_t i = 0;
	size_t j = 0;

	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *s1 = [OFString stringWithCString: "test"];
	OFString *s2 = [OFString stringWithCString: ""];
	OFString *s3;
	OFString *s4 = [OFString string];
	OFArray *a;

	s3 = [s1 copy];

	CHECK([s1 isEqual: s3])
	CHECK(![s1 isEqual: [[OFObject alloc] init]])
	CHECK([s1 hash] == [s3 hash])

92
93
94
95
96
97
98








99
100
101
102
103
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115







+
+
+
+
+
+
+
+






	/* Format tests */
	s1 = [OFString stringWithFormat: "%s: %d", "test", 123];
	CHECK(!strcmp([s1 cString], "test: 123"))

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

	a = [@"fooXXbarXXXXbazXXXX" splitWithDelimiter: @"XX"];
	CHECK([[a object: j++] isEqual: @"foo"])
	CHECK([[a object: j++] isEqual: @"bar"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @"baz"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @""])

	puts("");

	return 0;
}