ObjFW  Diff

Differences From Artifact [c52dab0d6d]:

To Artifact [01aa579278]:


1
2
3
4

5
6
7
8
9
10
11
1



2
3
4
5
6
7
8
9

-
-
-
+







/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018, 2019, 2020
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
 *
 * 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.
 *
40
41
42
43
44
45
46
47

48
49
50
51
52
53
54
55
38
39
40
41
42
43
44

45

46
47
48
49
50
51
52







-
+
-







}

+ (instancetype)dataWithCapacity: (size_t)capacity
{
	return [[[self alloc] initWithCapacity: capacity] autorelease];
}

+ (instancetype)dataWithItemSize: (size_t)itemSize
+ (instancetype)dataWithItemSize: (size_t)itemSize capacity: (size_t)capacity
			capacity: (size_t)capacity
{
	return [[[self alloc] initWithItemSize: itemSize
				      capacity: capacity] autorelease];
}

- (instancetype)init
{
81
82
83
84
85
86
87
88

89
90
91
92
93
94
95
96
78
79
80
81
82
83
84

85

86
87
88
89
90
91
92







-
+
-








- (instancetype)initWithCapacity: (size_t)capacity
{
	return [self initWithItemSize: 1
			     capacity: capacity];
}

- (instancetype)initWithItemSize: (size_t)itemSize
- (instancetype)initWithItemSize: (size_t)itemSize capacity: (size_t)capacity
			capacity: (size_t)capacity
{
	self = [super init];

	@try {
		if (itemSize == 0)
			@throw [OFInvalidArgumentException exception];

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







-
+
-
-











-
+
-
-







	return self;
}

- (instancetype)initWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super initWithItems: items
	self = [super initWithItems: items count: count itemSize: itemSize];
			      count: count
			   itemSize: itemSize];

	_capacity = _count;

	return self;
}

- (instancetype)initWithItemsNoCopy: (void *)items
			      count: (size_t)count
			   itemSize: (size_t)itemSize
		       freeWhenDone: (bool)freeWhenDone
{
	self = [self initWithItems: items
	self = [self initWithItems: items count: count itemSize: itemSize];
			     count: count
			  itemSize: itemSize];

	if (freeWhenDone)
		free(items);

	return self;
}

194
195
196
197
198
199
200
201

202
203
204

205
206
207
208
209

210
211
212
213
214
215
216
217
186
187
188
189
190
191
192

193

194

195


196
197

198

199
200
201
202
203
204
205







-
+
-

-
+
-
-


-
+
-







	}

	memcpy(_items + _count * _itemSize, item, _itemSize);

	_count++;
}

- (void)insertItem: (const void *)item
- (void)insertItem: (const void *)item atIndex: (size_t)idx
	   atIndex: (size_t)idx
{
	[self insertItems: item
	[self insertItems: item atIndex: idx count: 1];
		  atIndex: idx
		    count: 1];
}

- (void)addItems: (const void *)items
- (void)addItems: (const void *)items count: (size_t)count
	   count: (size_t)count
{
	if (count > SIZE_MAX - _count)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		_items = of_realloc(_items, _count + count, _itemSize);
		_capacity = _count + count;