ObjFW  Check-in [9746cff094]

Overview
Comment:OFNumber: Don't always use the smallest type

This is annoying as it changes the type encoding, and hence makes
-[getValue:size:] a pain.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9746cff0942fc2156e923817d03daf13e4f5a90450761f628e683e00441c27f7
User & Date: js on 2020-08-22 18:39:48
Other Links: manifest | tags
Context
2020-08-25
00:11
OFNumber: Remove (u)int{8,16,32,64} methods check-in: 25c985fec1 user: js tags: trunk
2020-08-22
18:39
OFNumber: Don't always use the smallest type check-in: 9746cff094 user: js tags: trunk
2020-08-16
18:28
OFNumber: Remove ssize_t check-in: be8b442546 user: js tags: trunk
Changes

Modified src/OFNumber.m from [c26097a3d6] to [f7c843c996].

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







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





-
-
+
+


-
-
+
+






-
-
+
+
+
+
+






-
-
+
+
+
-
-
+
+






-
-
-
-
+
+
+
+
+






-
-
-
-
+
+
+
+
+






-
-
+
+
+
-
-
+
+






-
+
-

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







-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
-
-
+
+
+
+
+






-
-
-
-
+
+
+
+
+






-
-
-
-
+
+
+
+
+






+
+
+
+
+
+





-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
+
+
+
+
+






-
-
-



-
-
-
-
-
-
-
-
-


-
-
-





-
-
-
-
-





-
-
-
-



-
-
-
-


-
-
-
-
-
+
+
+
+
+






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







@interface OFNumberSingleton: OFNumber
@end

static struct {
	Class isa;
} placeholder;

static OFNumberSingleton *zeroNumber, *oneNumber, *twoNumber;
static OFNumberSingleton *trueNumber, *falseNumber;

static void
initZeroNumber(void)
{
	zeroNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 0];
}

#define SINGLETON(var, sel, val)				\
	static OFNumberSingleton *var;				\
								\
	static void						\
	var##Init(void)						\
	{							\
		var = [[OFNumberSingleton alloc] sel val];	\
	}
SINGLETON(falseNumber, initWithBool:, false)
static void
initOneNumber(void)
SINGLETON(trueNumber, initWithBool:, true)
{
	oneNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 1];
SINGLETON(charZeroNumber, initWithChar:, 0)
SINGLETON(shortZeroNumber, initWithShort:, 0)
SINGLETON(intZeroNumber, initWithInt:, 0)
SINGLETON(longZeroNumber, initWithLong:, 0)
SINGLETON(longLongZeroNumber, initWithLongLong:, 0)
SINGLETON(unsignedCharZeroNumber, initWithUnsignedChar:, 0)
}

SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0)
static void
initTwoNumber(void)
SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0)
{
	twoNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 2];
SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0)
}

static void
initTrueNumber(void)
SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0)
SINGLETON(int8ZeroNumber, initWithInt8:, 0)
SINGLETON(int16ZeroNumber, initWithInt16:, 0)
SINGLETON(int32ZeroNumber, initWithInt32:, 0)
{
	trueNumber = [[OFNumberSingleton alloc] initWithBool: true];
SINGLETON(int64ZeroNumber, initWithInt64:, 0)
}

static void
initFalseNumber(void)
SINGLETON(uInt8ZeroNumber, initWithUInt8:, 0)
SINGLETON(uInt16ZeroNumber, initWithUInt16:, 0)
SINGLETON(uInt32ZeroNumber, initWithUInt32:, 0)
SINGLETON(uInt64ZeroNumber, initWithUInt64:, 0)
SINGLETON(floatZeroNumber, initWithFloat:, 0)
{
	falseNumber = [[OFNumberSingleton alloc] initWithBool: false];
SINGLETON(doubleZeroNumber, initWithDouble:, 0)
}
#undef SINGLETON

@implementation OFNumberPlaceholder
- (instancetype)initWithBool: (bool)value
{
	if (value) {
		static of_once_t once;
		of_once(&once, initTrueNumber);
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, trueNumberInit);
		return (id)trueNumber;
	} else {
		static of_once_t once;
		of_once(&once, initFalseNumber);
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, falseNumberInit);
		return (id)falseNumber;
	}
}

- (instancetype)initWithChar: (signed char)value
{
	if (value >= 0)
		return [self initWithUnsignedChar: value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, charZeroNumberInit);
		return (id)charZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithChar: value];
}

- (instancetype)initWithShort: (short)value
{
	if (value >= 0)
		return [self initWithUnsignedShort: value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, shortZeroNumberInit);
	if (value >= SCHAR_MIN)
		return [self initWithChar: (signed char)value];
		return (id)shortZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithShort: value];
}

- (instancetype)initWithInt: (int)value
{
	if (value >= 0)
		return [self initWithUnsignedInt: value];
	if (value >= SHRT_MIN)
		return [self initWithShort: (short)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, intZeroNumberInit);
		return (id)intZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithInt: value];
}

- (instancetype)initWithLong: (long)value
{
	if (value >= 0)
		return [self initWithUnsignedLong: value];
	if (value >= INT_MIN)
		return [self initWithShort: (int)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, longZeroNumberInit);
		return (id)longZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithLong: value];
}

- (instancetype)initWithLongLong: (long long)value
{
	if (value >= 0)
		return [self initWithUnsignedLongLong: value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, longLongZeroNumberInit);
	if (value >= LONG_MIN)
		return [self initWithLong: (long)value];
		return (id)longLongZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithLongLong: value];
}

- (instancetype)initWithUnsignedChar: (unsigned char)value
{
	switch (value) {
	if (value == 0) {
	case 0: {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, initZeroNumber);
		return (id)zeroNumber;
	}
	case 1: {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, initOneNumber);
		return (id)oneNumber;
		of_once(&once, unsignedCharZeroNumberInit);
		return (id)unsignedCharZeroNumber;
	}
	case 2: {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, initTwoNumber);
		return (id)twoNumber;
	}
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedChar: value];
}

- (instancetype)initWithUnsignedShort: (unsigned short)value
{
	if (value <= UCHAR_MAX)
		return [self initWithUnsignedChar: (unsigned char)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedShortZeroNumberInit);
		return (id)unsignedShortZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedShort: value];
}

- (instancetype)initWithUnsignedInt: (unsigned int)value
{
	if (value <= USHRT_MAX)
		return [self initWithUnsignedShort: (unsigned short)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedIntZeroNumberInit);
		return (id)unsignedIntZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedInt: value];
}

- (instancetype)initWithUnsignedLong: (unsigned long)value
{
	if (value <= UINT_MAX)
		return [self initWithUnsignedInt: (unsigned int)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedLongZeroNumberInit);
		return (id)unsignedLongZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLong: value];
}

- (instancetype)initWithUnsignedLongLong: (unsigned long long)value
{
	if (value <= ULONG_MAX)
		return [self initWithUnsignedLong: (unsigned long)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedLongLongZeroNumberInit);
		return (id)unsignedLongLongZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: value];
}

- (instancetype)initWithInt8: (int8_t)value
{
	if (value >= 0)
		return [self initWithUInt8: value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, int8ZeroNumberInit);
		return (id)int8ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithInt8: value];
}

- (instancetype)initWithInt16: (int16_t)value
{
	if (value >= 0)
		return [self initWithUInt16: value];
	if (value >= INT8_MIN)
		return [self initWithInt8: (int8_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, int16ZeroNumberInit);
		return (id)int16ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithInt16: value];
}

- (instancetype)initWithInt32: (int32_t)value
{
	if (value >= 0)
		return [self initWithUInt32: value];
	if (value >= INT16_MIN)
		return [self initWithInt16: (int16_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, int32ZeroNumberInit);
		return (id)int32ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithInt32: value];
}

- (instancetype)initWithInt64: (int64_t)value
{
	if (value >= 0)
		return [self initWithUInt64: value];
	if (value >= INT32_MIN)
		return [self initWithInt32: (int32_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, int64ZeroNumberInit);
		return (id)int64ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithInt64: value];
}

- (instancetype)initWithUInt8: (uint8_t)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, uInt8ZeroNumberInit);
		return (id)uInt8ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUInt8: value];
}

- (instancetype)initWithUInt16: (uint16_t)value
{
	if (value <= UINT8_MAX)
		return [self initWithUInt8: (uint8_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, uInt16ZeroNumberInit);
		return (id)uInt16ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUInt16: value];
}

- (instancetype)initWithUInt32: (uint32_t)value
{
	if (value <= UINT16_MAX)
		return [self initWithUInt16: (uint16_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, uInt32ZeroNumberInit);
		return (id)uInt32ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUInt32: value];
}

- (instancetype)initWithUInt64: (uint64_t)value
{
	if (value <= UINT32_MAX)
		return [self initWithUInt32: (uint32_t)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, uInt64ZeroNumberInit);
		return (id)uInt64ZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithUInt64: value];
}

- (instancetype)initWithSize: (size_t)value
{
	if (value <= ULONG_MAX)
		return [self initWithUnsignedLong: (unsigned long)value];

	return (id)[[OFNumber of_alloc] initWithSize: value];
}

#ifdef __clang__
/*
 * This warning should probably not exist at all, as it prevents checking
 * whether one type fits into another in a portable way.
 */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif

- (instancetype)initWithPtrDiff: (ptrdiff_t)value
{
	if (value >= LLONG_MIN && value <= LLONG_MAX)
		return [self initWithLongLong: (long long)value];

	return (id)[[OFNumber of_alloc] initWithPtrDiff: value];
}

- (instancetype)initWithIntPtr: (intptr_t)value
{
	if (value >= 0)
		return [self initWithUIntPtr: value];
	if (value >= LLONG_MIN)
		return [self initWithLongLong: (long long)value];

	return (id)[[OFNumber of_alloc] initWithIntPtr: value];
}

- (instancetype)initWithUIntPtr: (uintptr_t)value
{
	if (value <= ULLONG_MAX)
		return [self initWithUnsignedLongLong:
		    (unsigned long long)value];

	return (id)[[OFNumber of_alloc] initWithUIntPtr: value];
}

#ifdef __clang__
# pragma clang diagnostic pop
#endif

- (instancetype)initWithFloat: (float)value
{
	if (value == (unsigned long long)value)
		return [self initWithUnsignedLongLong:
		    (unsigned long long)value];
	if (value == (long long)value)
		return [self initWithLongLong: (long long)value];
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, floatZeroNumberInit);
		return (id)floatZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithFloat: value];
}

- (instancetype)initWithDouble: (double)value
{
	if (value == (unsigned long long)value)
		return [self initWithUnsignedLongLong:
		    (unsigned long long)value];
	if (value == (long long)value)
	if (value == 0) {
		return [self initWithLongLong: (long long)value];
	if (value == (float)value)
		return [self initWithFloat: (float)value];
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, doubleZeroNumberInit);
		return (id)doubleZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithDouble: value];
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	return (id)[[OFNumber of_alloc] initWithSerialization: element];
1100
1101
1102
1103
1104
1105
1106
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116

1117
1118
1119
1120
1121
1122
1123
1097
1098
1099
1100
1101
1102
1103

1104









1105
1106
1107
1108
1109
1110
1111
1112







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







		if (isnan(self.doubleValue))
			return 0;

		d = OF_BSWAP_DOUBLE_IF_BE(self.doubleValue);

		for (uint_fast8_t i = 0; i < sizeof(double); i++)
			OF_HASH_ADD(hash, ((char *)&d)[i]);
	} else if (type == OF_NUMBER_TYPE_SIGNED) {
	} else if (type == OF_NUMBER_TYPE_SIGNED ||
		long long value = self.longLongValue * -1;

		while (value != 0) {
			OF_HASH_ADD(hash, value & 0xFF);
			value >>= 8;
		}

		OF_HASH_ADD(hash, 1);
	} else if (type == OF_NUMBER_TYPE_UNSIGNED) {
	    type == OF_NUMBER_TYPE_UNSIGNED) {
		unsigned long long value = self.unsignedLongLongValue;

		while (value != 0) {
			OF_HASH_ADD(hash, value & 0xFF);
			value >>= 8;
		}
	} else

Modified tests/serialization.xml from [904cd48eb0] to [c1b84078b7].

13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27







-
+







    <object>
      <OFString>data</OFString>
    </object>
    <key>
      <OFArray>
        <OFString>Qu&quot;xbar
test</OFString>
        <OFNumber type='unsigned'>1234</OFNumber>
        <OFNumber type='signed'>1234</OFNumber>
        <OFNumber type='float'>40934a456d5cfaad</OFNumber>
        <OFMutableString>asd</OFMutableString>
        <OFDate>40934a456d5cfaad</OFDate>
      </OFArray>
    </key>
    <object>
      <OFString>Hello</OFString>