ObjFW  Check-in [1d44132d96]

Overview
Comment:There is no point in splitWithDelimiter: requiring an OFString.
Plus some code clean up in splitWithDelimiter:.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1d44132d96bd76dad8d9996c8d225714999c3f2a4f8cf586e602fbaac1319688
User & Date: js on 2009-05-07 11:55:53
Other Links: manifest | tags
Context
2009-05-07
12:55
Add - data for OFArray. check-in: eb6aafad32 user: js tags: trunk
11:55
There is no point in splitWithDelimiter: requiring an OFString.
Plus some code clean up in splitWithDelimiter:.
check-in: 1d44132d96 user: js tags: trunk
2009-05-05
17:59
Rename - objects / - items in OFArray / OFDataArray to - count. check-in: 64bc94cdb3 user: js tags: trunk
Changes

Modified src/OFString.h from [a1e4b20180] to [7948d30ca4].

153
154
155
156
157
158
159
160
161

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







|

153
154
155
156
157
158
159
160
161

/**
 * Splits an OFString into an OFArray of OFStrings.
 *
 * \param delimiter The delimiter for splitting
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (const char*)delimiter;
@end

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

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

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







|

|
|
<
|



|
<
<
<
|
<
<


|







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

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

- (OFArray*)splitWithDelimiter: (const char*)delimiter
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFArray *array = nil;

	size_t delim_len = strlen(delimiter);
	size_t i, last;

	@try {
		array = [[OFArray alloc] init];






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

				/*
				 * We can't use [self allocWithSize:] here as
				 * self might be a @""-literal.
				 */
199
200
201
202
203
204
205

206
207
208
209
210
211
212
213
214
				[pool releaseObjects];

				i += delim_len - 1;
				last = i + 1;
			}
		}
	} @catch (OFException *e) {

		[array release];
		@throw e;
	} @finally {
		[pool release];
	}

	return [array autorelease];
}
@end







>
|








193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
				[pool releaseObjects];

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

	return [array autorelease];
}
@end

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

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







|











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