ObjFW  Check-in [a855618045]

Overview
Comment:Make OFArray an abstract class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a85561804542d9d40f5c995f5316bfa2b86cedd8a376d7b58ac9e097972b2efb
User & Date: js on 2011-08-03 00:30:04
Other Links: manifest | tags
Context
2011-08-03
00:37
Fix wrong case in tests output. check-in: 0bb31fb062 user: js tags: trunk
00:30
Make OFArray an abstract class. check-in: a855618045 user: js tags: trunk
2011-08-01
19:01
Don't use any other classes for +[inheritMethodsFromClass:]. check-in: 7b5b0c8174 user: js tags: trunk
Changes

Modified src/Makefile from [8482526187] to [af792bbc48].

1
2
3
4
5
6
7
8
9
10
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
37
38
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40













+


















+







include ../extra.mk

SUBDIRS = exceptions

LIB = ${OBJFW_SHARED_LIB}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}
STATIC_LIB = ${OBJFW_STATIC_LIB}

SRCS = OFApplication.m			\
       OFArray.m			\
       OFAutoreleasePool.m		\
       OFBlock.m			\
       OFCArray.m			\
       OFConstantString.m		\
       OFCountedSet.m			\
       OFDataArray.m			\
       OFDataArray+Hashing.m		\
       OFDate.m				\
       OFDictionary.m			\
       OFDoubleMatrix.m			\
       OFDoubleVector.m			\
       OFEnumerator.m			\
       OFFile.m				\
       OFFloatMatrix.m			\
       OFFloatVector.m			\
       OFHash.m				\
       OFHTTPRequest.m			\
       OFIntrospection.m		\
       OFList.m				\
       OFMD5Hash.m			\
       OFMutableArray.m			\
       OFMutableCArray.m		\
       OFMutableDictionary.m		\
       OFMutableSet.m			\
       OFMutableString.m		\
       OFNull.m				\
       OFNumber.m			\
       OFObject.m			\
       OFObject+Serialization.m		\

Modified src/OFApplication.m from [2e89fcfb3b] to [849335f8ed].

196
197
198
199
200
201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
196
197
198
199
200
201
202






203
204
205
206
207
208
209
210







-
-
-
-
-
-
+








	programName = [[OFString alloc] initWithCString: (*argv)[0]];
	arguments = [[OFMutableArray alloc] init];

	for (i = 1; i < *argc; i++)
		[arguments addObject: [OFString stringWithCString: (*argv)[i]]];

	/*
	 * Class swizzle the arguments to be immutable, as we don't need to
	 * change them anymore and expose them only as OFArray*. But not
	 * swizzling it would create a real copy each time -[copy] is called.
	 */
	arguments->isa = [OFArray class];
	[arguments makeImmutable];

	[pool release];
}

- (void)getArgumentCount: (int**)argc_
       andArgumentValues: (char****)argv_
{

Modified src/OFArray.h from [ba9897850c] to [23dded9802].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38




39
40
41
42
43
44
45







-















-
-
-
-







#include <stdarg.h>

#import "OFObject.h"
#import "OFCollection.h"
#import "OFEnumerator.h"
#import "OFSerialization.h"

@class OFDataArray;
@class OFString;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_array_enumeration_block_t)(id object, size_t index,
    BOOL *stop);
typedef BOOL (^of_array_filter_block_t)(id odject, size_t index);
typedef id (^of_array_map_block_t)(id object, size_t index);
typedef id (^of_array_fold_block_t)(id left, id right);
#endif

/**
 * \brief A class for storing objects in an array.
 */
@interface OFArray: OFObject <OFCopying, OFMutableCopying, OFCollection,
    OFSerialization>
{
	OFDataArray *array;
}

/**
 * \brief Creates a new OFArray.
 *
 * \return A new autoreleased OFArray
 */
+ array;

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
















165
166
167
168
169
170
171
135
136
137
138
139
140
141







142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175







-
-
-
-
-
-
-











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







 * \param objects A C array of objects
 * \param length The length of the C array
 * \return An initialized OFArray
 */
- initWithCArray: (id*)objects
	  length: (size_t)length;

/**
 * \brief Returns the objects of the array as a C array.
 *
 * \return The objects of the array as a C array
 */
- (id*)cArray;

/**
 * \brief Returns a specified object of the array.
 *
 * The returned object is <i>not</i> retained and autoreleased for performance
 * reasons!
 *
 * \param index The number of the object to return
 * \return The specified object of the OFArray
 */
- (id)objectAtIndex: (size_t)index;

/**
 * \brief Copies the objects at the specified range to the specified buffer.
 *
 * \param buffer The buffer to copy the objects to
 * \param range The range to copy
 */
- (void)getObjects: (id*)buffer
	   inRange: (of_range_t)range;

/**
 * \brief Returns the objects of the array as a C array.
 *
 * \return The objects of the array as a C array
 */
- (id*)cArray;

/**
 * \brief Returns the index of the first object that is equivalent to the
 *	  specified object or OF_INVALID_INDEX if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object equivalent to the specified object
 *	   or OF_INVALID_INDEX if it was not found
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
212
213
214
215
216
217
218












219
220
221
222
223
224
225







-
-
-
-
-
-
-
-
-
-
-
-







 * The returned object is <i>not</i> retained and autoreleased for performance
 * reasons!
 *
 * \return The last object of the array or nil
 */
- (id)lastObject;

/**
 * \brief Returns the objects from the specified index to the specified index as
 *	  a new OFArray.
 *
 * \param start The index where the subarray starts
 * \param end The index where the subarray ends.
 *	      This points BEHIND the last object!
 * \return The subarray as a new autoreleased OFArray
 */
- (OFArray*)objectsFromIndex: (size_t)start
		     toIndex: (size_t)end;

/**
 * \brief Returns the objects in the specified range as a new OFArray.
 *
 * \param range The range for the subarray
 * \return The subarray as a new autoreleased OFArray
 */
- (OFArray*)objectsInRange: (of_range_t)range;
299
300
301
302
303
304
305



306
307
308
309
310
311
312
313
314

315
316
317

318
319

320
321
322
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







+
+
+




-



-
+


-
+
-
-
+



 * \param block A block which folds two objects into one, which is called for
 *		all objects except the first
 * \return The array folded to a single object
 */
- (id)foldUsingBlock: (of_array_fold_block_t)block;
#endif
@end

@interface OFArrayPlaceholder: OFArray
@end

@interface OFArrayEnumerator: OFEnumerator
{
	OFArray	      *array;
	OFDataArray   *dataArray;
	size_t	      count;
	unsigned long mutations;
	unsigned long *mutationsPtr;
	size_t	      pos;
	size_t	      position;
}

-    initWithArray: (OFArray*)data
- initWithArray: (OFArray*)data
	 dataArray: (OFDataArray*)dataArray
  mutationsPointer: (unsigned long*)mutationsPtr;
   mutationsPtr: (unsigned long*)mutationsPtr;
@end

#import "OFMutableArray.h"

Modified src/OFArray.m from [1f143f64a7] to [409e6b7e60].

15
16
17
18
19
20
21
22

23
24
25
26
27
28

29
30
31
32















































































33














34
35
36
37
38
39
40
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134







-
+






+




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

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







 */

#include "config.h"

#include <stdarg.h>

#import "OFArray.h"
#import "OFDataArray.h"
#import "OFCArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

static struct {
	Class isa;
} placeholder;

@implementation OFArrayPlaceholder
- init
{
	return (id)[[OFCArray alloc] init];
}

- initWithObject: (id)object
{
	return (id)[[OFCArray alloc] initWithObject: object];
}

- initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [[OFCArray alloc] initWithObject: firstObject
				     arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	return (id)[[OFCArray alloc] initWithObject: firstObject
					  arguments: arguments];
}

- initWithArray: (OFArray*)array
{
	return (id)[[OFCArray alloc] initWithArray: array];
}

- initWithCArray: (id*)objects
{
	return (id)[[OFCArray alloc] initWithCArray: objects];
}

- initWithCArray: (id*)objects
	  length: (size_t)length
{
	return (id)[[OFCArray alloc] initWithCArray: objects
					     length: length];
}

- initWithSerialization: (OFXMLElement*)element
{
	return (id)[[OFCArray alloc] initWithSerialization: element];
}

- retain
{
	return self;
}

- autorelease
{
	return self;
}

- (void)release
{
}

- (void)dealloc
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
	[super dealloc];	/* Get rid of a stupid warning */
}
@end

@implementation OFArray
+ (void)initialize
{
	if (self == [OFArray class])
		placeholder.isa = [OFArrayPlaceholder class];
}

+ alloc
{
	if (self == [OFArray class])
		return (id)&placeholder;

	return [super alloc];
}

+ array
{
	return [[[self alloc] init] autorelease];
}

+ arrayWithObject: (id)object
{
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
151


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
215
216
217
218
219
220
221
222
223


224
225

226
227
228
229
230
231
232

233
234
235
236
237

238
239
240
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
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
387
388
389
390
391
392




393
394
395
396
397
398

399
400
401
402
403



404
405
406
407
408
409

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

426
427
428
429
430
431
432
433
434
435

436
437
438
439
440
441
442
443
444


445
446
447
448
449
450
451
452
453


454
455
456
457
458
459
460
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
















215
216


217

218
219
220
221


222





223







224
225
226


227



228



229






230
231
232
233


234




235





236
237
238

239
240
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
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
387
388

389
390
391
392
393
394
395

396
397
398
399
400



401

402
403
404
405
406
407
408
409
410


411
412
413
414
415
416
417
418
419







-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
-
-
-
-
-
-
-
-
-
-
+


















-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-


-
+

-
-
-
+
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-




-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-





-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-




-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
+
+
+
-
-

-
-
-
+
-
-
-
+
-
-
-
-
-
-
+
+
+

-
-
+
-
-
-
-
+
-
-
-
-
-
+
+
+
-
+
+




+
+
+
+
+
+
+
+
+
+
+
+
-
+














-
+
+




-
-
+

-
-
-

-
+







-
-
+

-
-
-

-
+







-
-
-
+
-
-
-
-
-
-
-
-




-
-
-
+
-
-
-
-
-
-
-
-




+
-
+

-
+




+
-
+
+
+

-
+


-
+
-

-
+
+
+

-
-
+
+
+

-
-
-
-
+
+
+
+
+
-
-
-
+
+
-




-
-
-
-
+
+
+
+





-
+





+
+
+





-
+
















+

-







-
+




-
-
-

-
+
+







-
-
+
+







+ arrayWithCArray: (id*)objects
	   length: (size_t)length
{
	return [[[self alloc] initWithCArray: objects
				      length: length] autorelease];
}

- init
{
	self = [super init];

	@try {
		array = [[OFDataArray alloc] initWithItemSize: sizeof(id)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObject: (id)object
{
	self = [self init];

	@try {
		[array addItem: &object];
		[object retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
	return [self initWithObjects: object, nil];
}

- initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [self initWithObject: firstObject
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	self = [self init];

	Class c = isa;
	@try {
		id object;

		[array addItem: &firstObject];
		[firstObject retain];

		while ((object = va_arg(arguments, id)) != nil) {
			[array addItem: &object];
			[object retain];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	return self;
}

- initWithArray: (OFArray*)array_
- initWithArray: (OFArray*)array
{
	id *cArray;
	size_t i, count;

	Class c = isa;
	self = [self init];

	@try {
		cArray = [array_ cArray];
		count = [array_ count];
	} @catch (id e) {
		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	@try {
		for (i = 0; i < count; i++)
			[cArray[i] retain];

		[array addNItems: count
		      fromCArray: cArray];
	} @catch (id e) {
		for (i = 0; i < count; i++)
			[cArray[i] release];

		/* Prevent double-release of objects */
		[array release];
		array = nil;

		[self release];
		@throw e;
	}

	return self;
}

- initWithCArray: (id*)objects
{
	self = [self init];

	Class c = isa;
	@try {
		id *object;
		size_t count = 0;

		for (object = objects; *object != nil; object++) {
			[*object retain];
			count++;
		}

		[array addNItems: count
		      fromCArray: objects];
	} @catch (id e) {
		id *object;

		for (object = objects; *object != nil; object++)
			[*object release];

		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	return self;
}

- initWithCArray: (id*)objects
	  length: (size_t)length
{
	self = [self init];

	Class c = isa;
	@try {
		size_t i;

		for (i = 0; i < length; i++)
			[objects[i] retain];

		[array addNItems: length
		      fromCArray: objects];
	} @catch (id e) {
		size_t i;

		for (i = 0; i < length; i++)
			[objects[i] release];

		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [self init];

	Class c = isa;
	@try {
		OFAutoreleasePool *pool, *pool2;
		OFEnumerator *enumerator;
		OFXMLElement *child;

	[self release];
		pool = [[OFAutoreleasePool alloc] init];

		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}
		enumerator = [[element children] objectEnumerator];
		pool2 = [[OFAutoreleasePool alloc] init];

		while ((child = [enumerator nextObject]) != nil) {
			id object;

- (size_t)count
			if (![[child namespace] isEqual: OF_SERIALIZATION_NS])
				continue;

{
			object = [child objectByDeserializing];
			[array addItem: &object];
			[object retain];

			[pool2 releaseObjects];
		}
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

		[pool release];
	} @catch (id e) {
- (void)getObjects: (id*)buffer
		[self release];
		@throw e;
	}

	   inRange: (of_range_t)range
	return self;
}

- (size_t)count
{
{
	size_t i;

	return [array count];
	for (i = 0; i < range.length; i++)
		buffer[i] = [self objectAtIndex: range.start + i];
}

- (id*)cArray
{
	OFObject *container;
	size_t count;
	id *buffer;

	container = [[[OFObject alloc] init] autorelease];
	count = [self count];
	buffer = [container allocMemoryForNItems: [self count]
					withSize: sizeof(*buffer)];

	[self getObjects: buffer
		 inRange: of_range(0, count)];

	return [array cArray];
	return buffer;
}

- copy
{
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableArray alloc] initWithArray: self];
}

- (id)objectAtIndex: (size_t)index
{
	return *((id*)[array itemAtIndex: index]);
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)indexOfObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	size_t i, count = [self count];

	if (cArray == NULL)
		return OF_INVALID_INDEX;

	for (i = 0; i < count; i++)
		if ([cArray[i] isEqual: object])
		if ([[self objectAtIndex: i] isEqual: object])
			return i;

	return OF_INVALID_INDEX;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	size_t i, count = [self count];

	if (cArray == NULL)
		return OF_INVALID_INDEX;

	for (i = 0; i < count; i++)
		if (cArray[i] == object)
		if ([self objectAtIndex: i] == object)
			return i;

	return OF_INVALID_INDEX;
}

- (BOOL)containsObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	return ([self indexOfObject: object] != OF_INVALID_INDEX);
	if (cArray == NULL)
		return NO;

	for (i = 0; i < count; i++)
		if ([cArray[i] isEqual: object])
			return YES;

	return NO;
}

- (BOOL)containsObjectIdenticalTo: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	return ([self indexOfObjectIdenticalTo: object] != OF_INVALID_INDEX);
	if (cArray == NULL)
		return NO;

	for (i = 0; i < count; i++)
		if (cArray[i] == object)
			return YES;

	return NO;
}

- (id)firstObject
{
	if ([self count] > 0)
	id *firstObject = [array firstItem];
		return [self objectAtIndex: 0];

	return (firstObject != NULL ? *firstObject : nil);
	return nil;
}

- (id)lastObject
{
	size_t count = [self count];
	id *lastObject = [array lastItem];

	if (count > 0)
		return [self objectAtIndex: count - 1];

	return (lastObject != NULL ? *lastObject : nil);
	return nil;
}

- (OFArray*)objectsFromIndex: (size_t)start
- (OFArray*)objectsInRange: (of_range_t)range
		     toIndex: (size_t)end
{
	size_t count = [array count];
	OFArray *ret;
	id *buffer = [self allocMemoryForNItems: range.length
				       withSize: sizeof(*buffer)];

	if (end > count || start > end)
		@throw [OFOutOfRangeException newWithClass: isa];
	@try {
		[self getObjects: buffer
			 inRange: range];

	return [OFArray arrayWithCArray: (id*)[array cArray] + start
				 length: end - start];
}

		ret = [OFArray arrayWithCArray: buffer
					length: range.length];
	} @finally {
		[self freeMemory: buffer];
	}
- (OFArray*)objectsInRange: (of_range_t)range
{
	return [self objectsFromIndex: range.start

	return ret;
			      toIndex: range.start + range.length];
}

- (OFString*)componentsJoinedByString: (OFString*)separator
{
	OFAutoreleasePool *pool;
	OFString *ret;
	OFObject **cArray = [array cArray];
	size_t i, count = [array count];
	OFAutoreleasePool *pool, *pool2;
	OFMutableString *ret;
	id *cArray;
	size_t i, count = [self count];
	IMP append;

	if (count == 0)
		return @"";
	if (count == 1)
		return [cArray[0] description];
		return [[self firstObject] description];

	ret = [OFMutableString string];
	append = [ret methodForSelector: @selector(appendString:)];

	pool = [[OFAutoreleasePool alloc] init];
	cArray = [self cArray];

	pool2 = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count - 1; i++) {
		append(ret, @selector(appendString:), [cArray[i] description]);
		append(ret, @selector(appendString:), separator);

		[pool releaseObjects];
		[pool2 releaseObjects];
	}
	append(ret, @selector(appendString:), [cArray[i] description]);

	[pool release];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFString class];
	return ret;
}

- (BOOL)isEqual: (id)object
{
	/* FIXME: Optimize (for example, buffer of 16 for each) */
	OFArray *otherArray;
	id *cArray, *otherCArray;
	size_t i, count;

	if (![object isKindOfClass: [OFArray class]])
		return NO;

	otherArray = object;

	count = [array count];
	count = [self count];

	if (count != [otherArray count])
		return NO;

	cArray = [array cArray];
	otherCArray = [otherArray cArray];

	for (i = 0; i < count; i++)
		if (![cArray[i] isEqual: otherCArray[i]])
		if (![[self objectAtIndex: i] isEqual:
		    [otherArray objectAtIndex: i]])
			return NO;

	return YES;
}

- (uint32_t)hash
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	id *cArray = [self cArray];
	size_t i, count = [self count];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < count; i++) {
		uint32_t h = [cArray[i] hash];

470
471
472
473
474
475
476
477

478
479
480
481
482
483
484
429
430
431
432
433
434
435

436
437
438
439
440
441
442
443







-
+







}

- (OFString*)description
{
	OFAutoreleasePool *pool;
	OFMutableString *ret;

	if ([array count] == 0)
	if ([self count] == 0)
		return @"()";

	pool = [[OFAutoreleasePool alloc] init];
	ret = [[self componentsJoinedByString: @",\n"] mutableCopy];

	@try {
		[ret prependString: @"(\n"];
504
505
506
507
508
509
510
511
512


513

514
515





516
517
518
519
520
521
522
463
464
465
466
467
468
469


470
471
472
473


474
475
476
477
478
479
480
481
482
483
484
485







-
-
+
+

+
-
-
+
+
+
+
+







}

- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFAutoreleasePool *pool2;
	OFXMLElement *element;
	id <OFSerialization> *cArray = [array cArray];
	size_t i, count = [array count];
	id <OFSerialization> *cArray = [self cArray];
	size_t i, count = [self count];

	if ([self isKindOfClass: [OFMutableArray class]])
	element = [OFXMLElement elementWithName: [self className]
				      namespace: OF_SERIALIZATION_NS];
		element = [OFXMLElement elementWithName: @"OFMutableArray"
					      namespace: OF_SERIALIZATION_NS];
	else
		element = [OFXMLElement elementWithName: @"OFArray"
					      namespace: OF_SERIALIZATION_NS];

	pool2 = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count; i++) {
		[element addChild: [cArray[i] XMLElementBySerializing]];

		[pool2 releaseObjects];
530
531
532
533
534
535
536
537
538


539
540
541
542
543
544
545
546
547
548
549


550
551
552
553
554
555
556
557
558
559

560

561
562
563
564
565
566
567
568
569

570
571
572
573
574
575
576
577
578
579

580
581
582
583
584
585
586

587
588
589
590

591
592
593
594
595
596

597
598
599
600
601
602
603
493
494
495
496
497
498
499


500
501
502
503
504
505
506
507
508
509
510


511
512
513
514
515
516
517
518
519
520
521
522
523

524
525
526
527
528
529
530
531
532

533
534
535
536
537
538
539
540
541


542
543
544
545
546
547


548
549
550
551

552
553
554
555
556
557

558
559
560
561
562
563
564
565







-
-
+
+









-
-
+
+










+
-
+








-
+








-
-
+





-
-
+



-
+





-
+







	}

	return element;
}

- (void)makeObjectsPerformSelector: (SEL)selector
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	id *cArray = [self cArray];
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		((void(*)(id, SEL))[cArray[i]
		    methodForSelector: selector])(cArray[i], selector);
}

- (void)makeObjectsPerformSelector: (SEL)selector
			withObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	id *cArray = [self cArray];
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		((void(*)(id, SEL, id))[cArray[i]
		    methodForSelector: selector])(cArray[i], selector, object);
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	/* FIXME: Use -[getObjects:inRange:] on the passed objects */
	size_t count = [array count];
	size_t count = [self count];

	if (count > INT_MAX)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (state->state >= count)
		return 0;

	state->state = count;
	state->itemsPtr = [array cArray];
	state->itemsPtr = [self cArray];
	state->mutationsPtr = (unsigned long*)self;

	return (int)count;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFArrayEnumerator alloc] initWithArray: self
					       dataArray: array
					mutationsPointer: NULL] autorelease];
					    mutationsPtr: NULL] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	size_t i, count = [self count];
	BOOL stop = NO;

	for (i = 0; i < count && !stop; i++)
		block(cArray[i], i, &stop);
		block([self objectAtIndex: i], i, &stop);
}

- (OFArray*)mappedArrayUsingBlock: (of_array_map_block_t)block
{
	OFArray *ret;
	size_t count = [array count];
	size_t count = [self count];
	id *tmp = [self allocMemoryForNItems: count
				    withSize: sizeof(id)];

	@try {
		[self enumerateObjectsUsingBlock: ^ (id object, size_t index,
		    BOOL *stop) {
			tmp[index] = block(object, index);
611
612
613
614
615
616
617
618

619
620
621
622
623
624
625
573
574
575
576
577
578
579

580
581
582
583
584
585
586
587







-
+








	return ret;
}

- (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block
{
	OFArray *ret;
	size_t count = [array count];
	size_t count = [self count];
	id *tmp = [self allocMemoryForNItems: count
				    withSize: sizeof(id)];

	@try {
		__block size_t i = 0;

		[self enumerateObjectsUsingBlock: ^ (id object, size_t index,
635
636
637
638
639
640
641
642

643
644
645
646
647
648
649
597
598
599
600
601
602
603

604
605
606
607
608
609
610
611







-
+







	}

	return ret;
}

- (id)foldUsingBlock: (of_array_fold_block_t)block
{
	size_t count = [array count];
	size_t count = [self count];
	__block id current;

	if (count == 0)
		return nil;
	if (count == 1)
		return [[[self firstObject] retain] autorelease];

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

687
688

689
690
691
692
693
694

695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716


717
718
719
720
721
722
723
724
725
726
727

728
729
625
626
627
628
629
630
631













632
633
634

635


636
637
638
639
640


641
642
643
644
645
646
647
648
649
650

651
652
653
654
655
656
657
658
659
660


661
662
663
664
665
666
667
668
669
670
671
672

673
674
675







-
-
-
-
-
-
-
-
-
-
-
-
-



-
+
-
-
+




-
-
+









-










-
-
+
+










-
+


		}
		current = new;
	}];

	return [current autorelease];
}
#endif

- (void)dealloc
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		[cArray[i] release];

	[array release];

	[super dealloc];
}
@end

@implementation OFArrayEnumerator
-    initWithArray: (OFArray*)array_
- initWithArray: (OFArray*)array_
	 dataArray: (OFDataArray*)dataArray_
  mutationsPointer: (unsigned long*)mutationsPtr_
   mutationsPtr: (unsigned long*)mutationsPtr_
{
	self = [super init];

	array = [array_ retain];
	dataArray = [dataArray_ retain];
	count = [dataArray count];
	count = [array count];
	mutations = (mutationsPtr_ != NULL ? *mutationsPtr_ : 0);
	mutationsPtr = mutationsPtr_;

	return self;
}

- (void)dealloc
{
	[array release];
	[dataArray release];

	[super dealloc];
}

- (id)nextObject
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException newWithClass: isa
							     object: array];

	if (pos < count)
		return *(id*)[dataArray itemAtIndex: pos++];
	if (position < count)
		return [array objectAtIndex: position++];

	return nil;
}

- (void)reset
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException newWithClass: isa
							     object: array];

	pos = 0;
	position = 0;
}
@end

Added src/OFCArray.h version [a0f3673861].





























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFArray.h"

@class OFDataArray;

/**
 * \brief A class for storing objects in an array.
 */
@interface OFCArray: OFArray
{
	OFDataArray *array;
}
@end

Added src/OFCArray.m version [2c7cd9bb93].





































































































































































































































































































































































1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
151
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdarg.h>

#import "OFCArray.h"
#import "OFMutableCArray.h"
#import "OFDataArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

@implementation OFCArray
- init
{
	self = [super init];

	@try {
		array = [[OFDataArray alloc] initWithItemSize: sizeof(id)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObject: (id)object
{
	self = [self init];

	@try {
		[array addItem: &object];
		[object retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	self = [self init];

	@try {
		id object;

		[array addItem: &firstObject];
		[firstObject retain];

		while ((object = va_arg(arguments, id)) != nil) {
			[array addItem: &object];
			[object retain];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithArray: (OFArray*)array_
{
	id *cArray;
	size_t i, count;

	self = [self init];

	@try {
		cArray = [array_ cArray];
		count = [array_ count];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	@try {
		for (i = 0; i < count; i++)
			[cArray[i] retain];

		[array addNItems: count
		      fromCArray: cArray];
	} @catch (id e) {
		for (i = 0; i < count; i++)
			[cArray[i] release];

		/* Prevent double-release of objects */
		[array release];
		array = nil;

		[self release];
		@throw e;
	}

	return self;
}

- initWithCArray: (id*)objects
{
	self = [self init];

	@try {
		id *object;
		size_t count = 0;

		for (object = objects; *object != nil; object++) {
			[*object retain];
			count++;
		}

		[array addNItems: count
		      fromCArray: objects];
	} @catch (id e) {
		id *object;

		for (object = objects; *object != nil; object++)
			[*object release];

		[self release];
		@throw e;
	}

	return self;
}

- initWithCArray: (id*)objects
	  length: (size_t)length
{
	self = [self init];

	@try {
		size_t i;

		for (i = 0; i < length; i++)
			[objects[i] retain];

		[array addNItems: length
		      fromCArray: objects];
	} @catch (id e) {
		size_t i;

		for (i = 0; i < length; i++)
			[objects[i] release];

		[self release];
		@throw e;
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [self init];

	@try {
		OFAutoreleasePool *pool, *pool2;
		OFEnumerator *enumerator;
		OFXMLElement *child;

		pool = [[OFAutoreleasePool alloc] init];

		if ((![[element name] isEqual: @"OFArray"] &&
		    ![[element name] isEqual: @"OFMutableArray"]) ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		enumerator = [[element children] objectEnumerator];
		pool2 = [[OFAutoreleasePool alloc] init];

		while ((child = [enumerator nextObject]) != nil) {
			id object;

			if (![[child namespace] isEqual: OF_SERIALIZATION_NS])
				continue;

			object = [child objectByDeserializing];
			[array addItem: &object];
			[object retain];

			[pool2 releaseObjects];
		}

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (size_t)count
{
	return [array count];
}

- (id*)cArray
{
	return [array cArray];
}

- (id)objectAtIndex: (size_t)index
{
	return *((id*)[array itemAtIndex: index]);
}

- (void)getObjects: (id*)buffer
	   inRange: (of_range_t)range
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	if (range.start + range.length > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = 0; i < range.length; i++)
		buffer[i] = cArray[range.start + i];
}

- (size_t)indexOfObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if ([cArray[i] isEqual: object])
			return i;

	return OF_INVALID_INDEX;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if (cArray[i] == object)
			return i;

	return OF_INVALID_INDEX;
}


- (OFArray*)objectsInRange: (of_range_t)range
{
	size_t count = [array count];

	if (range.start + range.length > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	return [OFArray arrayWithCArray: (id*)[array cArray] + range.start
				 length: range.length];
}

- (BOOL)isEqual: (id)object
{
	OFArray *otherArray;
	id *cArray, *otherCArray;
	size_t i, count;

	if (![object isKindOfClass: [OFCArray class]] &&
	    ![object isKindOfClass: [OFMutableCArray class]])
		return [super isEqual: object];

	otherArray = object;

	count = [array count];

	if (count != [otherArray count])
		return NO;

	cArray = [array cArray];
	otherCArray = [otherArray cArray];

	for (i = 0; i < count; i++)
		if (![cArray[i] isEqual: otherCArray[i]])
			return NO;

	return YES;
}

- (uint32_t)hash
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < count; i++) {
		uint32_t h = [cArray[i] hash];

		OF_HASH_ADD(hash, h >> 24);
		OF_HASH_ADD(hash, (h >> 16) & 0xFF);
		OF_HASH_ADD(hash, (h >> 8) & 0xFF);
		OF_HASH_ADD(hash, h & 0xFF);
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;

	for (i = 0; i < count && !stop; i++)
		block(cArray[i], i, &stop);
}
#endif

- (void)dealloc
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		[cArray[i] release];

	[array release];

	[super dealloc];
}
@end

Modified src/OFFile.m from [5aa0f12b35] to [24d4055b98].

299
300
301
302
303
304
305
306
307
308
309
310
311
312

313
314
315
316
317
318
319
299
300
301
302
303
304
305







306
307
308
309
310
311
312
313







-
-
-
-
-
-
-
+







	} @finally {
		FindClose(handle);
	}

	[pool release];
#endif

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	files->isa = [OFArray class];
	return files;
	return [files makeImmutable];
}

+ (void)changeToDirectory: (OFString*)path
{
	if (chdir([path cString]))
		@throw [OFChangeDirectoryFailedException newWithClass: self
								 path: path];

Modified src/OFIntrospection.m from [5d382d3723] to [20e651ef45].

163
164
165
166
167
168
169
170
171


172
173
174
175
176
177
178
163
164
165
166
167
168
169


170
171
172
173
174
175
176
177
178







-
-
+
+







			for (i = 0; i < methodList->method_count; i++)
				[instanceMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}
#endif

		classMethods->isa = [OFArray class];
		instanceMethods->isa = [OFArray class];
		[classMethods makeImmutable];
		[instanceMethods makeImmutable];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

Modified src/OFMutableArray.h from [246ee00df8] to [1389a09656].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
20
21
22
23
24
25
26




27
28
29
30
31
32
33







-
-
-
-







typedef id (^of_array_replace_block_t)(id obj, size_t idx, BOOL *stop);
#endif

/**
 * \brief A class for storing, adding and removing objects in an array.
 */
@interface OFMutableArray: OFArray
{
	unsigned long mutations;
}

/**
 * \brief Adds an object to the OFArray.
 *
 * \param object An object to add
 */
- (void)addObject: (id)object;

99
100
101
102
103
104
105
106

107
108

109
110
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126





127



95
96
97
98
99
100
101

102
103

104

105

106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129







-
+

-
+
-

-
+
-














+
+
+
+
+

+
+
+
 * \brief Removes the specified amount of objects from the end of the OFArray.
 *
 * \param nObjects The number of objects to remove
 */
- (void)removeNObjects: (size_t)nObjects;

/**
 * \brief Removes the specified amount of objects at the specified index.
 * \brief Removes the object in the specified range.
 *
 * \param nobjects The number of objects to remove
 * \param range The range of the objects to remove
 * \param index The index at which the objects are removed
 */
- (void)removeNObjects: (size_t)nObjects
- (void)removeObjectsInRange: (of_range_t)range;
	       atIndex: (size_t)index;

/**
 * \brief Removes the last object.
 */
- (void)removeLastObject;

#ifdef OF_HAVE_BLOCKS
/**
 * \brief Replaces each object with the object returned by the block.
 *
 * \param block The block which returns a new object for each object
 */
- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block;
#endif

/**
 * \brief Converts the mutable array to an immutable array.
 */
- (OFArray*)makeImmutable;
@end

@interface OFMutableArrayPlaceholder: OFMutableArray
@end

Modified src/OFMutableArray.m from [637e903291] to [7ce334d92a].

15
16
17
18
19
20
21
22

23
24
25
26

27
28

















































































29














30
31
32
33
34
35
36
37
38
39


40
41
42
43
44
45



46
47
48
49
50






51
52
53
54
55
56
57

58
59
60
61


62
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

89
90
91
92


93
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146

147
148
149

150


151
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
151
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
215

216



217
218



219
220



221

222

223



224

225
226
227
228













229


230

231











232

















233
234





235



236





237


238








239
240
241
242
243
244
245
246
247







-
+




+


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

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







-
-
-
+
+
-





+
+
+
-
-
-
-
-
+
+
+
+
+
+





-
-
+


-
-
+
+
-
-
+
-





-
-
-
-
-
-
-
-
-
-
-
-
-
-



-
-
+


-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








-
+
-
-
-
+
-


-
+

-
-
+

+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+

-

-
-
+

-
+
-
-
-
+
+
-
-
-
+
+
-
-
-
+
-

-
+
-
-
-
+
-




-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-

-
-
-
-
-
-
-
-
-
-
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+


+
+
+
+
+

 */

#include "config.h"

#include <string.h>

#import "OFMutableArray.h"
#import "OFDataArray.h"
#import "OFMutableCArray.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

static struct {
	Class isa;
} placeholder;

@implementation OFMutableArrayPlaceholder
- init
{
	return (id)[[OFMutableCArray alloc] init];
}

- initWithObject: (id)object
{
	return (id)[[OFMutableCArray alloc] initWithObject: object];
}

- initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [[OFMutableCArray alloc] initWithObject: firstObject
					    arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	return (id)[[OFMutableCArray alloc] initWithObject: firstObject
						 arguments: arguments];
}

- initWithArray: (OFArray*)array
{
	return (id)[[OFMutableCArray alloc] initWithArray: array];
}

- initWithCArray: (id*)objects
{
	return (id)[[OFMutableCArray alloc] initWithCArray: objects];
}

- initWithCArray: (id*)objects
	  length: (size_t)length
{
	return (id)[[OFMutableCArray alloc] initWithCArray: objects
						    length: length];
}

- initWithSerialization: (OFXMLElement*)element
{
	return (id)[[OFMutableCArray alloc] initWithSerialization: element];
}

- retain
{
	return self;
}

- autorelease
{
	return self;
}

- (void)release
{
}

- (void)dealloc
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
	[super dealloc];	/* Get rid of a stupid warning */
}
@end

@implementation OFMutableArray
+ (void)initialize
{
	if (self == [OFMutableArray class])
		placeholder.isa = [OFMutableArrayPlaceholder class];
}

+ alloc
{
	if (self == [OFMutableArray class])
		return (id)&placeholder;

	return [super alloc];
}

- copy
{
	return [[OFArray alloc] initWithArray: self];
}

- (void)addObject: (id)object
{
	[array addItem: &object];
	[object retain];

	[self addObject: object
		atIndex: [self count]];
	mutations++;
}

- (void)addObject: (id)object
	  atIndex: (size_t)index
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
	[array addItem: &object
	       atIndex: index];
	[object retain];

	mutations++;

- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)object
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)replaceObject: (id)oldObject
	   withObject: (id)newObject
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	size_t i, count = [self count];

	for (i = 0; i < count; i++) {
		if ([cArray[i] isEqual: oldObject]) {
			[newObject retain];
		if ([[self objectAtIndex: i] isEqual: oldObject]) {
			[self replaceObjectAtIndex: i
			[cArray[i] release];
			cArray[i] = newObject;
					withObject: newObject];

			return;
		}
	}
}

- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)object
{
	id *cArray = [array cArray];
	id oldObject;

	if (index >= [array count])
		@throw [OFOutOfRangeException newWithClass: isa];

	oldObject = cArray[index];
	cArray[index] = [object retain];
	[oldObject release];
}

- (void)replaceObjectIdenticalTo: (id)oldObject
		      withObject: (id)newObject
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	size_t i, count = [self count];

	for (i = 0; i < count; i++) {
		if (cArray[i] == oldObject) {
			[newObject retain];
		if ([self objectAtIndex: i] == oldObject) {
			[self replaceObjectAtIndex: i
			[cArray[i] release];
			cArray[i] = newObject;
					withObject: newObject];

			return;
		}
	}
}

- (void)removeObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if ([cArray[i] isEqual: object]) {
			object = cArray[i];

			[array removeItemAtIndex: i];
			mutations++;

			[object release];

			return;
		}
	}
}

- (void)removeObjectIdenticalTo: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if (cArray[i] == object) {
			[array removeItemAtIndex: i];
			mutations++;

			[object release];

			return;
		}
	}
}

- (void)removeObjectAtIndex: (size_t)index
{
	id object = [self objectAtIndex: index];
	@throw [OFNotImplementedException newWithClass: isa
	[array removeItemAtIndex: index];
	[object release];

					      selector: _cmd];
	mutations++;
}

- (void)removeNObjects: (size_t)nObjects
- (void)removeObject: (id)object
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];
	size_t i, count = [self count];

	for (i = 0; i < count; i++) {
		if ([[self objectAtIndex: i] isEqual: object]) {
	if (nObjects > count)
		@throw [OFOutOfRangeException newWithClass: isa];

			[self removeObjectAtIndex: i];

			return;
		}
	copy = [self allocMemoryForNItems: nObjects
				 withSize: sizeof(id)];
	memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id));

	@try {
		[array removeNItems: nObjects];
	}
}

- (void)removeObjectIdenticalTo: (id)object
		mutations++;

		for (i = 0; i < nObjects; i++)
{
	size_t i, count = [self count];
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}
}


	for (i = 0; i < count; i++) {
		if ([self objectAtIndex: i] == object) {
			[self removeObjectAtIndex: i];

			return;
		}
	}
}

- (void)removeNObjects: (size_t)nObjects
	       atIndex: (size_t)index
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];
	size_t count = [self count];

	if (nObjects > count - index)
	[self removeObjectsInRange: of_range(count - nObjects, nObjects)];
		@throw [OFOutOfRangeException newWithClass: isa];

	copy = [self allocMemoryForNItems: nObjects
}

				 withSize: sizeof(id)];
	memcpy(copy, cArray + index, nObjects * sizeof(id));

- (void)removeObjectsInRange: (of_range_t)range
{
	@try {
		[array removeNItems: nObjects
			    atIndex: index];
	size_t i;
		mutations++;

		for (i = 0; i < nObjects; i++)
	for (i = 0; i < range.length; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
		[self removeObjectAtIndex: range.start];
	}
}

- (void)removeLastObject
{
	id object = [self objectAtIndex: [array count] - 1];
	[array removeLastItem];
	[object release];

	mutations++;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	int ret = [super countByEnumeratingWithState: state
					     objects: objects
	[self removeNObjects: 1];
					       count: count_];

}
	state->mutationsPtr = &mutations;

	return ret;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFArrayEnumerator alloc]
	       initWithArray: self
		   dataArray: array
	    mutationsPointer: &mutations] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < count && !stop; i++) {
		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa
				  object: self];

		block(cArray[i], i, &stop);
	}
}

- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	[self enumerateObjectsUsingBlock: ^ (id object, size_t index,
	for (i = 0; i < count && !stop; i++) {
		id newObject;

	    BOOL *stop) {
		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa
				  object: self];

		[self replaceObjectAtIndex: index
		newObject = block(cArray[i], i, &stop);

				withObject: block(object, index, stop)];
		if (newObject == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		[newObject retain];
		[cArray[i] release];
		cArray[i] = newObject;
	}
	}];
}
#endif

- (OFArray*)makeImmutable
{
	return self;
}
@end

Added src/OFMutableCArray.h version [cc4737ccfc].






























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFArray.h"

@class OFDataArray;

/**
 * \brief A class for storing, adding and removing objects in an array.
 */
@interface OFMutableCArray: OFMutableArray
{
	OFDataArray   *array;
	unsigned long mutations;
}
@end

Added src/OFMutableCArray.m version [3855ae84ee].




















































































































































































































































































1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
151
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>

#import "OFMutableCArray.h"
#import "OFCArray.h"
#import "OFDataArray.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"

@implementation OFMutableCArray
+ (void)initialize
{
	if (self == [OFMutableCArray class])
		[self inheritMethodsFromClass: [OFCArray class]];
}

- (void)addObject: (id)object
{
	[array addItem: &object];
	[object retain];

	mutations++;
}

- (void)addObject: (id)object
	  atIndex: (size_t)index
{
	[array addItem: &object
	       atIndex: index];
	[object retain];

	mutations++;
}

- (void)replaceObject: (id)oldObject
	   withObject: (id)newObject
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if ([cArray[i] isEqual: oldObject]) {
			[newObject retain];
			[cArray[i] release];
			cArray[i] = newObject;

			return;
		}
	}
}

- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)object
{
	id *cArray = [array cArray];
	id oldObject;

	if (index >= [array count])
		@throw [OFOutOfRangeException newWithClass: isa];

	oldObject = cArray[index];
	cArray[index] = [object retain];
	[oldObject release];
}

- (void)replaceObjectIdenticalTo: (id)oldObject
		      withObject: (id)newObject
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if (cArray[i] == oldObject) {
			[newObject retain];
			[cArray[i] release];
			cArray[i] = newObject;

			return;
		}
	}
}

- (void)removeObject: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if ([cArray[i] isEqual: object]) {
			object = cArray[i];

			[array removeItemAtIndex: i];
			mutations++;

			[object release];

			return;
		}
	}
}

- (void)removeObjectIdenticalTo: (id)object
{
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if (cArray[i] == object) {
			[array removeItemAtIndex: i];
			mutations++;

			[object release];

			return;
		}
	}
}

- (void)removeObjectAtIndex: (size_t)index
{
	id object = [self objectAtIndex: index];
	[array removeItemAtIndex: index];
	[object release];

	mutations++;
}

- (void)removeNObjects: (size_t)nObjects
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];

	if (nObjects > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	copy = [self allocMemoryForNItems: nObjects
				 withSize: sizeof(id)];
	memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id));

	@try {
		[array removeNItems: nObjects];
		mutations++;

		for (i = 0; i < nObjects; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}
}

- (void)removeObjectsInRange: (of_range_t)range
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];

	if (range.length > count - range.start)
		@throw [OFOutOfRangeException newWithClass: isa];

	copy = [self allocMemoryForNItems: range.length
				 withSize: sizeof(id)];
	memcpy(copy, cArray + range.start, range.length * sizeof(id));

	@try {
		[array removeNItems: range.length
			    atIndex: range.start];
		mutations++;

		for (i = 0; i < range.length; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}
}

- (void)removeLastObject
{
	id object = [self objectAtIndex: [array count] - 1];
	[array removeLastItem];
	[object release];

	mutations++;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	int ret = [super countByEnumeratingWithState: state
					     objects: objects
					       count: count_];

	state->mutationsPtr = &mutations;

	return ret;
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFArrayEnumerator alloc]
	    initWithArray: self
	     mutationsPtr: &mutations] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < count && !stop; i++) {
		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa
				  object: self];

		block(cArray[i], i, &stop);
	}
}

- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < count && !stop; i++) {
		id newObject;

		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa
				  object: self];

		newObject = block(cArray[i], i, &stop);

		if (newObject == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		[newObject retain];
		[cArray[i] release];
		cArray[i] = newObject;
	}
}
#endif

- (OFArray*)makeImmutable
{
	isa = [OFCArray class];
	return self;
}
@end

Modified src/OFString.m from [fee37530e3] to [9e62f802e4].

1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592

1593
1594
1595
1596
1597
1598
1599
1579
1580
1581
1582
1583
1584
1585







1586
1587
1588
1589
1590
1591
1592
1593







-
-
-
-
-
-
-
+







		i += cStringLength - 1;
		last = i + 1;
	}
	[array addObject: [OFString stringWithCString: s->cString + last]];

	[pool release];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	array->isa = [OFArray class];
	return array;
	return [array makeImmutable];
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	OFAutoreleasePool *pool;
	size_t i, last = 0, pathCStringLength = s->cStringLength;
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640

1641
1642
1643
1644
1645
1646
1647
1621
1622
1623
1624
1625
1626
1627







1628
1629
1630
1631
1632
1633
1634
1635







-
-
-
-
-
-
-
+







	}

	[ret addObject: [OFString stringWithCString: s->cString + last
					     length: i - last]];

	[pool release];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFArray class];
	return ret;
	return [ret makeImmutable];
}

- (OFString*)lastPathComponent
{
	size_t pathCStringLength = s->cStringLength;
	ssize_t i;

Modified src/OFXMLElement.m from [c0f22f99a1] to [c5f82626bf].

1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065

1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

1086
1087
1088
1089
1090
1091
1092
1032
1033
1034
1035
1036
1037
1038







1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052







1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066







1067
1068
1069
1070
1071
1072
1073
1074







-
-
-
-
-
-
-
+













-
-
-
-
-
-
-
+













-
-
-
-
-
-
-
+







	OFXMLElement **cArray = [children cArray];
	size_t i, count = [children count];

	for (i = 0; i < count; i++)
		if (cArray[i]->name != nil)
			[ret addObject: cArray[i]];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFArray class];
	return ret;
	return [ret makeImmutable];
}

- (OFArray*)elementsForName: (OFString*)elementName
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **cArray = [children cArray];
	size_t i, count = [children count];

	for (i = 0; i < count; i++)
		if (cArray[i]->ns == nil &&
		    [cArray[i]->name isEqual: elementName])
			[ret addObject: cArray[i]];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFArray class];
	return ret;
	return [ret makeImmutable];
}

- (OFArray*)elementsForNamespace: (OFString*)elementNS
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **cArray = [children cArray];
	size_t i, count = [children count];

	for (i = 0; i < count; i++)
		if (cArray[i]->name != nil &&
		    [cArray[i]->ns isEqual: elementNS])
			[ret addObject: cArray[i]];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFArray class];
	return ret;
	return [ret makeImmutable];
}

- (OFArray*)elementsForName: (OFString*)elementName
		  namespace: (OFString*)elementNS
{
	OFMutableArray *ret;
	OFXMLElement **cArray;
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113

1114
1115
1116
1117
1118
1119
1120
1082
1083
1084
1085
1086
1087
1088







1089
1090
1091
1092
1093
1094
1095
1096







-
-
-
-
-
-
-
+







	count = [children count];

	for (i = 0; i < count; i++)
		if ([cArray[i]->ns isEqual: elementNS] &&
		    [cArray[i]->name isEqual: elementName])
			[ret addObject: cArray[i]];

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFArray class];
	return ret;
	return [ret makeImmutable];
}

- (BOOL)isEqual: (id)object
{
	OFXMLElement *otherElement;

	if (![object isKindOfClass: [OFXMLElement class]])

Modified tests/OFArrayTests.m from [964609bde3] to [8332694435].

81
82
83
84
85
86
87
88

89
90
91
92
93
94
95
96
97
98
99


100
101
102
103
104
105
106
107
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96
97


98
99

100
101
102
103
104
105
106







-
+









-
-
+
+
-







	    [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[a[1] objectAtIndex: 2] isEqual: c_ary[2]])

	TEST(@"-[containsObject:]",
	    [a[0] containsObject: c_ary[1]] == YES &&
	    [a[0] containsObject: @"nonexistant"] == NO)

	TEST(@"-[containsObject:]",
	TEST(@"-[containsObjectIdenticalTo:]",
	    [a[0] containsObjectIdenticalTo: c_ary[1]] == YES &&
	    [a[0] containsObjectIdenticalTo:
	    [OFString stringWithString: c_ary[1]]] == NO)

	TEST(@"-[indexOfObject:]", [a[0] indexOfObject: c_ary[1]] == 1)

	TEST(@"-[indexOfObjectIdenticalTo:]",
	    [a[1] indexOfObjectIdenticalTo: c_ary[1]] == 1)

	TEST(@"-[objectsFromIndex:toIndex",
	    [[a[0] objectsFromIndex: 1
	TEST(@"-[objectsInRange:]",
	    [[a[0] objectsInRange: of_range(1, 2)] isEqual:
			    toIndex: 3] isEqual:
	    ([OFArray arrayWithObjects: c_ary[1], c_ary[2], nil])])

	TEST(@"-[replaceObject:withObject:]",
	    R([m[0] replaceObject: c_ary[1]
		       withObject: c_ary[0]]) &&
	    [[m[0] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[m[0] objectAtIndex: 1] isEqual: c_ary[0]] &&
133
134
135
136
137
138
139
140
141


142
143
144
145
146
147

148
149
150
151
152
153
154
132
133
134
135
136
137
138


139
140
141
142
143
144
145

146
147
148
149
150
151
152
153







-
-
+
+





-
+







	    [m[0] count] == 1 && [[m[0] objectAtIndex: 0] isEqual: c_ary[0]])

	m[1] = [[a[0] mutableCopy] autorelease];
	TEST(@"-[removeObjectAtIndex:]", R([m[1] removeObjectAtIndex: 1]) &&
	    [m[1] count] == 2 && [[m[1] objectAtIndex: 1] isEqual: c_ary[2]])

	m[1] = [[a[0] mutableCopy] autorelease];
	TEST(@"-[removeNObjects:atIndex:]", R([m[1] removeNObjects: 2
							   atIndex: 0]) &&
	TEST(@"-[removeObjectsInRange:]",
	    R([m[1] removeObjectsInRange: of_range(0, 2)]) &&
	    [m[1] count] == 1 && [[m[1] objectAtIndex: 0] isEqual: c_ary[2]])

	EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]",
	    OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]])

	EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]",
	EXPECT_EXCEPTION(@"Detect out of range in -[removeNObjects:]",
	    OFOutOfRangeException, [m[0] removeNObjects: [m[0] count] + 1])

	TEST(@"-[componentsJoinedByString:]",
	    (a[1] = [OFArray arrayWithObjects: @"foo", @"bar", @"baz", nil]) &&
	    [[a[1] componentsJoinedByString: @" "] isEqual: @"foo bar baz"] &&
	    (a[1] = [OFArray arrayWithObject: @"foo"]) &&
	    [[a[1] componentsJoinedByString: @" "] isEqual: @"foo"])
200
201
202
203
204
205
206

207
208
209





210
211
212

213
214
215
216
217
218
219
199
200
201
202
203
204
205
206
207


208
209
210
211
212
213
214

215
216
217
218
219
220
221
222







+

-
-
+
+
+
+
+


-
+







			withObject: c_ary[0]];
	[m[0] replaceObjectAtIndex: 1
			withObject: c_ary[1]];
	[m[0] replaceObjectAtIndex: 2
			withObject: c_ary[2]];

	ok = NO;
	i = 0;
	@try {
		for (OFString *s in m[0])
			[m[0] addObject: @""];
		for (OFString *s in m[0]) {
			if (i == 0)
				[m[0] addObject: @""];
			i++;
		}
	} @catch (OFEnumerationMutationException *e) {
		ok = YES;
		[e dealloc];
		[e release];
	}

	TEST(@"Detection of mutation during Fast Enumeration", ok)

	[m[0] removeNObjects: 1];
#endif

242
243
244
245
246
247
248




249



250
251
252
253
254
255
256
245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
264
265







+
+
+
+
-
+
+
+







		@try {
			[a2 enumerateObjectsUsingBlock:
			    ^ (id obj, size_t idx, BOOL *stop) {
				[a2 removeObjectAtIndex: idx];
			}];
		} @catch (OFEnumerationMutationException *e) {
			ok = YES;
			[e release];
		} @catch (OFOutOfRangeException *e) {
			/*
			 * Out of bounds access due to enumeration not being
			[e dealloc];
			 * detected.
			 */
			[e release];
		}

		TEST(@"Detection of mutation during enumeration using blocks",
		    ok)
	}

	TEST(@"-[replaceObjectsUsingBlock:]",