@@ -68,10 +68,63 @@ SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0) SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0) SINGLETON(floatZeroNumber, initWithFloat:, 0) SINGLETON(doubleZeroNumber, initWithDouble:, 0) #undef SINGLETON + +static bool +isUnsigned(OFNumber *number) +{ + switch (*number.objCType) { + case 'B': + return true; + case 'C': + return true; + case 'S': + return true; + case 'I': + return true; + case 'L': + return true; + case 'Q': + return true; + default: + return false; + } +} + +static bool +isSigned(OFNumber *number) +{ + switch (*number.objCType) { + case 'c': + return true; + case 's': + return true; + case 'i': + return true; + case 'l': + return true; + case 'q': + return true; + default: + return false; + } +} + +static bool +isFloat(OFNumber *number) +{ + switch (*number.objCType) { + case 'f': + return true; + case 'd': + return true; + default: + return false; + } +} @implementation OFNumberPlaceholder - (instancetype)initWithBool: (bool)value { if (value) { @@ -337,11 +390,10 @@ - (instancetype)initWithBool: (bool)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(bool); return self; } @@ -348,11 +400,10 @@ - (instancetype)initWithChar: (signed char)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(signed char); return self; } @@ -359,11 +410,10 @@ - (instancetype)initWithShort: (short)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(short); return self; } @@ -370,11 +420,10 @@ - (instancetype)initWithInt: (int)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(int); return self; } @@ -381,11 +430,10 @@ - (instancetype)initWithLong: (long)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(long); return self; } @@ -392,11 +440,10 @@ - (instancetype)initWithLongLong: (long long)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(long long); return self; } @@ -403,11 +450,10 @@ - (instancetype)initWithUnsignedChar: (unsigned char)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(unsigned long); return self; } @@ -414,11 +460,10 @@ - (instancetype)initWithUnsignedShort: (unsigned short)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(unsigned short); return self; } @@ -425,11 +470,10 @@ - (instancetype)initWithUnsignedInt: (unsigned int)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(unsigned int); return self; } @@ -436,11 +480,10 @@ - (instancetype)initWithUnsignedLong: (unsigned long)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(unsigned long); return self; } @@ -447,11 +490,10 @@ - (instancetype)initWithUnsignedLongLong: (unsigned long long)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(unsigned long long); return self; } @@ -458,11 +500,10 @@ - (instancetype)initWithPtrDiff: (ptrdiff_t)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(ptrdiff_t); return self; } @@ -469,11 +510,10 @@ - (instancetype)initWithIntPtr: (intptr_t)value { self = [super init]; _value.signed_ = value; - _type = OF_NUMBER_TYPE_SIGNED; _typeEncoding = @encode(intptr_t); return self; } @@ -480,11 +520,10 @@ - (instancetype)initWithUIntPtr: (uintptr_t)value { self = [super init]; _value.unsigned_ = value; - _type = OF_NUMBER_TYPE_UNSIGNED; _typeEncoding = @encode(uintptr_t); return self; } @@ -491,11 +530,10 @@ - (instancetype)initWithFloat: (float)value { self = [super init]; _value.float_ = value; - _type = OF_NUMBER_TYPE_FLOAT; _typeEncoding = @encode(float); return self; } @@ -502,11 +540,10 @@ - (instancetype)initWithDouble: (double)value { self = [super init]; _value.float_ = value; - _type = OF_NUMBER_TYPE_FLOAT; _typeEncoding = @encode(double); return self; } @@ -564,116 +601,125 @@ } - (void)getValue: (void *)value size: (size_t)size { - switch (*_typeEncoding) { -#define CASE(enc, type, field) \ + switch (*self.objCType) { +#define CASE(enc, type, property) \ case enc: { \ - type tmp = (type)_value.field; \ + type tmp = (type)self.property; \ \ if (size != sizeof(type)) \ @throw [OFOutOfRangeException exception]; \ \ memcpy(value, &tmp, size); \ break; \ } - CASE('B', bool, unsigned_) - CASE('c', signed char, signed_) - CASE('s', short, signed_) - CASE('i', int, signed_) - CASE('l', long, signed_) - CASE('q', long long, signed_) - CASE('C', unsigned char, unsigned_) - CASE('S', unsigned short, unsigned_) - CASE('I', unsigned int, unsigned_) - CASE('L', unsigned long, unsigned_) - CASE('Q', unsigned long long, unsigned_) - CASE('f', float, float_) - CASE('d', double, float_) + CASE('B', bool, unsignedLongLongValue) + CASE('c', signed char, longLongValue) + CASE('s', short, longLongValue) + CASE('i', int, longLongValue) + CASE('l', long, longLongValue) + CASE('q', long long, longLongValue) + CASE('C', unsigned char, unsignedLongLongValue) + CASE('S', unsigned short, unsignedLongLongValue) + CASE('I', unsigned int, unsignedLongLongValue) + CASE('L', unsigned long, unsignedLongLongValue) + CASE('Q', unsigned long long, unsignedLongLongValue) + CASE('f', float, doubleValue) + CASE('d', double, doubleValue) #undef CASE default: @throw [OFInvalidFormatException exception]; } } -#define RETURN_AS(t) \ - switch (_type) { \ - case OF_NUMBER_TYPE_FLOAT: \ - return (t)_value.float_; \ - case OF_NUMBER_TYPE_SIGNED: \ - return (t)_value.signed_; \ - case OF_NUMBER_TYPE_UNSIGNED: \ - return (t)_value.unsigned_; \ - default: \ - @throw [OFInvalidFormatException exception]; \ - } +- (long long)longLongValue +{ + if (isFloat(self)) + return _value.float_; + else if (isSigned(self)) + return _value.signed_; + else if (isUnsigned(self)) + return _value.unsigned_; + else + @throw [OFInvalidFormatException exception]; +} + +- (unsigned long long)unsignedLongLongValue +{ + if (isFloat(self)) + return _value.float_; + else if (isSigned(self)) + return _value.signed_; + else if (isUnsigned(self)) + return _value.unsigned_; + else + @throw [OFInvalidFormatException exception]; +} + +- (double)doubleValue +{ + if (isFloat(self)) + return _value.float_; + else if (isSigned(self)) + return _value.signed_; + else if (isUnsigned(self)) + return _value.unsigned_; + else + @throw [OFInvalidFormatException exception]; +} + - (bool)boolValue { - RETURN_AS(bool) + return (bool)self.unsignedLongLongValue; } - (signed char)charValue { - RETURN_AS(signed char) + return (signed char)self.longLongValue; } - (short)shortValue { - RETURN_AS(short) + return (short)self.longLongValue; } - (int)intValue { - RETURN_AS(int) + return (int)self.longLongValue; } - (long)longValue { - RETURN_AS(long) -} - -- (long long)longLongValue -{ - RETURN_AS(long long) + return (long)self.longLongValue; } - (unsigned char)unsignedCharValue { - RETURN_AS(unsigned char) + return (unsigned char)self.unsignedLongLongValue; } - (unsigned short)unsignedShortValue { - RETURN_AS(unsigned short) + return (unsigned short)self.unsignedLongLongValue; } - (unsigned int)unsignedIntValue { - RETURN_AS(unsigned int) + return (unsigned int)self.unsignedLongLongValue; } - (unsigned long)unsignedLongValue { - RETURN_AS(unsigned long) -} - -- (unsigned long long)unsignedLongLongValue -{ - RETURN_AS(unsigned long long) + return (unsigned long)self.unsignedLongLongValue; } - (float)floatValue { - RETURN_AS(float) -} - -- (double)doubleValue -{ - RETURN_AS(double) -} -#undef RETURN_AS + return (float)self.doubleValue; +} - (bool)isEqual: (id)object { OFNumber *number; @@ -683,12 +729,11 @@ if (![object isKindOfClass: [OFNumber class]]) return false; number = object; - if (_type == OF_NUMBER_TYPE_FLOAT || - number->_type == OF_NUMBER_TYPE_FLOAT) { + if (isFloat(self) || isFloat(number)) { double value1 = number.doubleValue; double value2 = self.doubleValue; if (isnan(value1) && isnan(value2)) return true; @@ -696,12 +741,11 @@ return false; return (value1 == value2); } - if (_type == OF_NUMBER_TYPE_SIGNED || - number->_type == OF_NUMBER_TYPE_SIGNED) + if (isSigned(self) || isSigned(number)) return (number.longLongValue == self.longLongValue); return (number.unsignedLongLongValue == self.unsignedLongLongValue); } @@ -712,23 +756,21 @@ if (![(id)object isKindOfClass: [OFNumber class]]) @throw [OFInvalidArgumentException exception]; number = (OFNumber *)object; - if (_type == OF_NUMBER_TYPE_FLOAT || - number->_type == OF_NUMBER_TYPE_FLOAT) { + if (isFloat(self) || isFloat(number)) { double double1 = self.doubleValue; double double2 = number.doubleValue; if (double1 > double2) return OF_ORDERED_DESCENDING; if (double1 < double2) return OF_ORDERED_ASCENDING; return OF_ORDERED_SAME; - } else if (_type == OF_NUMBER_TYPE_SIGNED || - number->_type == OF_NUMBER_TYPE_SIGNED) { + } else if (isSigned(self) || isSigned(number)) { long long int1 = self.longLongValue; long long int2 = number.longLongValue; if (int1 > int2) return OF_ORDERED_DESCENDING; @@ -749,27 +791,25 @@ } } - (uint32_t)hash { - enum of_number_type type = _type; uint32_t hash; OF_HASH_INIT(hash); - if (type == OF_NUMBER_TYPE_FLOAT) { + if (isFloat(self)) { double d; 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 || - type == OF_NUMBER_TYPE_UNSIGNED) { + } else if (isSigned(self) || isUnsigned(self)) { unsigned long long value = self.unsignedLongLongValue; while (value != 0) { OF_HASH_ADD(hash, value & 0xFF); value >>= 8; @@ -792,18 +832,19 @@ return [self stringValue]; } - (OFString *)stringValue { - if (*_typeEncoding == 'B') - return (_value.unsigned_ ? @"true" : @"false"); - if (_type == OF_NUMBER_TYPE_FLOAT) - return [OFString stringWithFormat: @"%g", _value.float_]; - if (_type == OF_NUMBER_TYPE_SIGNED) - return [OFString stringWithFormat: @"%lld", _value.signed_]; - if (_type == OF_NUMBER_TYPE_UNSIGNED) - return [OFString stringWithFormat: @"%llu", _value.unsigned_]; + if (*self.objCType == 'B') + return (self.boolValue ? @"true" : @"false"); + if (isFloat(self)) + return [OFString stringWithFormat: @"%g", self.doubleValue]; + if (isSigned(self)) + return [OFString stringWithFormat: @"%lld", self.longLongValue]; + if (isUnsigned(self)) + return [OFString stringWithFormat: @"%llu", + self.unsignedLongLongValue]; @throw [OFInvalidFormatException exception]; } - (OFXMLElement *)XMLElementBySerializing @@ -813,24 +854,24 @@ element = [OFXMLElement elementWithName: self.className namespace: OF_SERIALIZATION_NS stringValue: self.description]; - if (*_typeEncoding == 'B') + if (*self.objCType == 'B') [element addAttributeWithName: @"type" stringValue: @"bool"]; - else if (_type == OF_NUMBER_TYPE_FLOAT) { + else if (isFloat(self)) { [element addAttributeWithName: @"type" stringValue: @"float"]; element.stringValue = [OFString stringWithFormat: @"%016" PRIx64, OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE( - _value.float_)))]; - } else if (_type == OF_NUMBER_TYPE_SIGNED) + self.doubleValue)))]; + } else if (isSigned(self)) [element addAttributeWithName: @"type" stringValue: @"signed"]; - else if (_type == OF_NUMBER_TYPE_UNSIGNED) + else if (isUnsigned(self)) [element addAttributeWithName: @"type" stringValue: @"unsigned"]; else @throw [OFInvalidFormatException exception]; @@ -856,12 +897,12 @@ - (OFString *)of_JSONRepresentationWithOptions: (int)options depth: (size_t)depth { double doubleValue; - if (*_typeEncoding == 'B') - return (_value.unsigned_ ? @"true" : @"false"); + if (*self.objCType == 'B') + return (self.boolValue ? @"true" : @"false"); doubleValue = self.doubleValue; if (isinf(doubleValue)) { if (options & OF_JSON_REPRESENTATION_JSON5) { if (doubleValue > 0) @@ -876,37 +917,38 @@ } - (OFData *)messagePackRepresentation { OFMutableData *data; + const char *typeEncoding = self.objCType; - if (*_typeEncoding == 'B') { - uint8_t type = (_value.unsigned_ ? 0xC3 : 0xC2); + if (*typeEncoding == 'B') { + uint8_t type = (self.boolValue ? 0xC3 : 0xC2); data = [OFMutableData dataWithItems: &type count: 1]; - } else if (*_typeEncoding == 'f') { + } else if (*typeEncoding == 'f') { uint8_t type = 0xCA; - float tmp = OF_BSWAP_FLOAT_IF_LE(_value.float_); + float tmp = OF_BSWAP_FLOAT_IF_LE(self.floatValue); data = [OFMutableData dataWithItemSize: 1 capacity: 5]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; - } else if (*_typeEncoding == 'd') { + } else if (*typeEncoding == 'd') { uint8_t type = 0xCB; - double tmp = OF_BSWAP_DOUBLE_IF_LE(_value.float_); + double tmp = OF_BSWAP_DOUBLE_IF_LE(self.doubleValue); data = [OFMutableData dataWithItemSize: 1 capacity: 9]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; - } else if (_type == OF_NUMBER_TYPE_SIGNED) { + } else if (isSigned(self)) { long long value = self.longLongValue; if (value >= -32 && value < 0) { uint8_t tmp = 0xE0 | ((uint8_t)(value - 32) & 0x1F); @@ -951,11 +993,11 @@ [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else @throw [OFOutOfRangeException exception]; - } else if (_type == OF_NUMBER_TYPE_UNSIGNED) { + } else if (isUnsigned(self)) { unsigned long long value = self.unsignedLongLongValue; if (value <= 127) { uint8_t tmp = ((uint8_t)value & 0x7F);