Index: src/OFNumber.h ================================================================== --- src/OFNumber.h +++ src/OFNumber.h @@ -69,20 +69,20 @@ OF_NUMBER_TYPE_UINTMAX = 0x0D, OF_NUMBER_TYPE_SIGNED = 0x10, /*! `signed char` */ OF_NUMBER_TYPE_CHAR = OF_NUMBER_TYPE_UCHAR | OF_NUMBER_TYPE_SIGNED, - /*! `signed short` */ + /*! `short` */ OF_NUMBER_TYPE_SHORT = OF_NUMBER_TYPE_USHORT | OF_NUMBER_TYPE_SIGNED, - /*! `signed int` */ + /*! `int` */ OF_NUMBER_TYPE_INT = OF_NUMBER_TYPE_UINT | OF_NUMBER_TYPE_SIGNED, - /*! `signed long` */ + /*! `long` */ OF_NUMBER_TYPE_LONG = OF_NUMBER_TYPE_ULONG | OF_NUMBER_TYPE_SIGNED, - /*! `signed long long` */ + /*! `long long` */ OF_NUMBER_TYPE_LONGLONG = OF_NUMBER_TYPE_ULONGLONG | OF_NUMBER_TYPE_SIGNED, /*! `int8_t` */ OF_NUMBER_TYPE_INT8 = OF_NUMBER_TYPE_UINT8 | OF_NUMBER_TYPE_SIGNED, @@ -121,38 +121,38 @@ #endif @interface OFNumber: OFValue { union of_number_value { - bool bool_; - signed char sChar; - signed short sShort; - signed int sInt; - signed long sLong; - signed long long sLongLong; - unsigned char uChar; - unsigned short uShort; - unsigned int uInt; - unsigned long uLong; + bool bool_; + signed char sChar; + short sShort; + int sInt; + long sLong; + long long sLongLong; + unsigned char uChar; + unsigned short uShort; + unsigned int uInt; + unsigned long uLong; unsigned long long uLongLong; - int8_t int8; - int16_t int16; - int32_t int32; - int64_t int64; - uint8_t uInt8; - uint16_t uInt16; - uint32_t uInt32; - uint64_t uInt64; - size_t size; - ssize_t sSize; - intmax_t intMax; - uintmax_t uIntMax; - ptrdiff_t ptrDiff; - intptr_t intPtr; - uintptr_t uIntPtr; - float float_; - double double_; + int8_t int8; + int16_t int16; + int32_t int32; + int64_t int64; + uint8_t uInt8; + uint16_t uInt16; + uint32_t uInt32; + uint64_t uInt64; + size_t size; + ssize_t sSize; + intmax_t intMax; + uintmax_t uIntMax; + ptrdiff_t ptrDiff; + intptr_t intPtr; + uintptr_t uIntPtr; + float float_; + double double_; } _value; of_number_type_t _type; } /*! @@ -169,28 +169,28 @@ * @brief The OFNumber as a `signed char`. */ @property (readonly, nonatomic) signed char charValue; /*! - * @brief The OFNumber as a `signed short`. - */ -@property (readonly, nonatomic) signed short shortValue; - -/*! - * @brief The OFNumber as a `signed int`. - */ -@property (readonly, nonatomic) signed int intValue; - -/*! - * @brief The OFNumber as a `signed long`. - */ -@property (readonly, nonatomic) signed long longValue; - -/*! - * @brief The OFNumber as a `signed long long`. - */ -@property (readonly, nonatomic) signed long long longLongValue; + * @brief The OFNumber as a `short`. + */ +@property (readonly, nonatomic) short shortValue; + +/*! + * @brief The OFNumber as an `int`. + */ +@property (readonly, nonatomic) int intValue; + +/*! + * @brief The OFNumber as a `long`. + */ +@property (readonly, nonatomic) long longValue; + +/*! + * @brief The OFNumber as a `long long`. + */ +@property (readonly, nonatomic) long long longLongValue; /*! * @brief The OFNumber as an `unsigned char`. */ @property (readonly, nonatomic) unsigned char unsignedCharValue; @@ -331,40 +331,40 @@ * @return A new autoreleased OFNumber */ + (instancetype)numberWithChar: (signed char)sChar; /*! - * @brief Creates a new OFNumber with the specified `signed short`. - * - * @param sShort A `signed short` which the OFNumber should contain - * @return A new autoreleased OFNumber - */ -+ (instancetype)numberWithShort: (signed short)sShort; - -/*! - * @brief Creates a new OFNumber with the specified `signed int`. - * - * @param sInt A `signed int` which the OFNumber should contain - * @return A new autoreleased OFNumber - */ -+ (instancetype)numberWithInt: (signed int)sInt; - -/*! - * @brief Creates a new OFNumber with the specified `signed long`. - * - * @param sLong A `signed long` which the OFNumber should contain - * @return A new autoreleased OFNumber - */ -+ (instancetype)numberWithLong: (signed long)sLong; - -/*! - * @brief Creates a new OFNumber with the specified `signed long long`. - * - * @param sLongLong A `signed long long` which the OFNumber should contain - * @return A new autoreleased OFNumber - */ -+ (instancetype)numberWithLongLong: (signed long long)sLongLong; + * @brief Creates a new OFNumber with the specified `short`. + * + * @param sShort A `short` which the OFNumber should contain + * @return A new autoreleased OFNumber + */ ++ (instancetype)numberWithShort: (short)sShort; + +/*! + * @brief Creates a new OFNumber with the specified `int`. + * + * @param sInt An `int` which the OFNumber should contain + * @return A new autoreleased OFNumber + */ ++ (instancetype)numberWithInt: (int)sInt; + +/*! + * @brief Creates a new OFNumber with the specified `long`. + * + * @param sLong A `long` which the OFNumber should contain + * @return A new autoreleased OFNumber + */ ++ (instancetype)numberWithLong: (long)sLong; + +/*! + * @brief Creates a new OFNumber with the specified `long long`. + * + * @param sLongLong A `long long` which the OFNumber should contain + * @return A new autoreleased OFNumber + */ ++ (instancetype)numberWithLongLong: (long long)sLongLong; /*! * @brief Creates a new OFNumber with the specified `unsigned char`. * * @param uChar An `unsigned char` which the OFNumber should contain @@ -568,44 +568,41 @@ * @return An initialized OFNumber */ - (instancetype)initWithChar: (signed char)sChar; /*! - * @brief Initializes an already allocated OFNumber with the specified - * `signed short`. - * - * @param sShort A `signed short` which the OFNumber should contain - * @return An initialized OFNumber - */ -- (instancetype)initWithShort: (signed short)sShort; - -/*! - * @brief Initializes an already allocated OFNumber with the specified - * `signed int`. - * - * @param sInt A `signed int` which the OFNumber should contain - * @return An initialized OFNumber - */ -- (instancetype)initWithInt: (signed int)sInt; - -/*! - * @brief Initializes an already allocated OFNumber with the specified - * `signed long`. - * - * @param sLong A `signed long` which the OFNumber should contain - * @return An initialized OFNumber - */ -- (instancetype)initWithLong: (signed long)sLong; - -/*! - * @brief Initializes an already allocated OFNumber with the specified - * `signed long long`. - * - * @param sLongLong A `signed long long` which the OFNumber should contain - * @return An initialized OFNumber - */ -- (instancetype)initWithLongLong: (signed long long)sLongLong; + * @brief Initializes an already allocated OFNumber with the specified `short`. + * + * @param sShort A `short` which the OFNumber should contain + * @return An initialized OFNumber + */ +- (instancetype)initWithShort: (short)sShort; + +/*! + * @brief Initializes an already allocated OFNumber with the specified `int`. + * + * @param sInt An `int` which the OFNumber should contain + * @return An initialized OFNumber + */ +- (instancetype)initWithInt: (int)sInt; + +/*! + * @brief Initializes an already allocated OFNumber with the specified `long`. + * + * @param sLong A `long` which the OFNumber should contain + * @return An initialized OFNumber + */ +- (instancetype)initWithLong: (long)sLong; + +/*! + * @brief Initializes an already allocated OFNumber with the specified + * `long long`. + * + * @param sLongLong A `long long` which the OFNumber should contain + * @return An initialized OFNumber + */ +- (instancetype)initWithLongLong: (long long)sLongLong; /*! * @brief Initializes an already allocated OFNumber with the specified * `unsigned char`. * Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -112,30 +112,55 @@ return (id)[[OFNumber of_alloc] initWithBool: bool_]; } - (instancetype)initWithChar: (signed char)sChar { + if (sChar >= 0) + return (id)[[OFNumber of_alloc] initWithUnsignedChar: sChar]; + return (id)[[OFNumber of_alloc] initWithChar: sChar]; } -- (instancetype)initWithShort: (signed short)sShort +- (instancetype)initWithShort: (short)sShort { + if (sShort >= 0) + return (id)[[OFNumber of_alloc] initWithUnsignedShort: sShort]; + if (sShort >= SCHAR_MIN) + return (id)[[OFNumber of_alloc] + initWithChar: (signed char)sShort]; + return (id)[[OFNumber of_alloc] initWithShort: sShort]; } -- (instancetype)initWithInt: (signed int)sInt +- (instancetype)initWithInt: (int)sInt { + if (sInt >= 0) + return (id)[[OFNumber of_alloc] initWithUnsignedInt: sInt]; + if (sInt >= SHRT_MIN) + return (id)[[OFNumber of_alloc] initWithShort: (short)sInt]; + return (id)[[OFNumber of_alloc] initWithInt: sInt]; } -- (instancetype)initWithLong: (signed long)sLong +- (instancetype)initWithLong: (long)sLong { + if (sLong >= 0) + return (id)[[OFNumber of_alloc] initWithUnsignedLong: sLong]; + if (sLong >= INT_MIN) + return (id)[[OFNumber of_alloc] initWithShort: (int)sLong]; + return (id)[[OFNumber of_alloc] initWithLong: sLong]; } -- (instancetype)initWithLongLong: (signed long long)sLongLong +- (instancetype)initWithLongLong: (long long)sLongLong { + if (sLongLong >= 0) + return (id)[[OFNumber of_alloc] + initWithUnsignedLongLong: sLongLong]; + if (sLongLong >= LONG_MIN) + return (id)[[OFNumber of_alloc] initWithLong: (long)sLongLong]; + return (id)[[OFNumber of_alloc] initWithLongLong: sLongLong]; } - (instancetype)initWithUnsignedChar: (unsigned char)uChar { @@ -142,45 +167,79 @@ return (id)[[OFNumber of_alloc] initWithUnsignedChar: uChar]; } - (instancetype)initWithUnsignedShort: (unsigned short)uShort { + if (uShort <= UCHAR_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedChar: (unsigned char)uShort]; + return (id)[[OFNumber of_alloc] initWithUnsignedShort: uShort]; } - (instancetype)initWithUnsignedInt: (unsigned int)uInt { + if (uInt <= USHRT_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedShort: (unsigned short)uInt]; + return (id)[[OFNumber of_alloc] initWithUnsignedInt: uInt]; } - (instancetype)initWithUnsignedLong: (unsigned long)uLong { + if (uLong <= UINT_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedInt: (unsigned int)uLong]; + return (id)[[OFNumber of_alloc] initWithUnsignedLong: uLong]; } - (instancetype)initWithUnsignedLongLong: (unsigned long long)uLongLong { + if (uLongLong <= ULONG_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedLong: (unsigned long)uLongLong]; + return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: uLongLong]; } - (instancetype)initWithInt8: (int8_t)int8 { + if (int8 >= 0) + return (id)[[OFNumber of_alloc] initWithUInt8: int8]; + return (id)[[OFNumber of_alloc] initWithInt8: int8]; } - (instancetype)initWithInt16: (int16_t)int16 { + if (int16 >= 0) + return (id)[[OFNumber of_alloc] initWithUInt16: int16]; + if (int16 >= INT8_MIN) + return (id)[[OFNumber of_alloc] initWithInt8: (int8_t)int16]; + return (id)[[OFNumber of_alloc] initWithInt16: int16]; } - (instancetype)initWithInt32: (int32_t)int32 { + if (int32 >= 0) + return (id)[[OFNumber of_alloc] initWithUInt32: int32]; + if (int32 >= INT16_MIN) + return (id)[[OFNumber of_alloc] initWithInt16: (int16_t)int32]; + return (id)[[OFNumber of_alloc] initWithInt32: int32]; } - (instancetype)initWithInt64: (int64_t)int64 { + if (int64 >= 0) + return (id)[[OFNumber of_alloc] initWithUInt64: int64]; + if (int64 >= INT32_MIN) + return (id)[[OFNumber of_alloc] initWithInt32: (int32_t)int64]; + return (id)[[OFNumber of_alloc] initWithInt64: int64]; } - (instancetype)initWithUInt8: (uint8_t)uInt8 { @@ -187,65 +246,125 @@ return (id)[[OFNumber of_alloc] initWithUInt8: uInt8]; } - (instancetype)initWithUInt16: (uint16_t)uInt16 { + if (uInt16 <= UINT8_MAX) + return (id)[[OFNumber of_alloc] initWithUInt8: (uint8_t)uInt16]; + return (id)[[OFNumber of_alloc] initWithUInt16: uInt16]; } - (instancetype)initWithUInt32: (uint32_t)uInt32 { + if (uInt32 <= UINT16_MAX) + return (id)[[OFNumber of_alloc] + initWithUInt16: (uint16_t)uInt32]; + return (id)[[OFNumber of_alloc] initWithUInt32: uInt32]; } - (instancetype)initWithUInt64: (uint64_t)uInt64 { + if (uInt64 <= UINT32_MAX) + return (id)[[OFNumber of_alloc] + initWithUInt32: (uint32_t)uInt64]; + return (id)[[OFNumber of_alloc] initWithUInt64: uInt64]; } - (instancetype)initWithSize: (size_t)size { + if (size <= ULONG_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedLong: (unsigned long)size]; + return (id)[[OFNumber of_alloc] initWithSize: size]; } - (instancetype)initWithSSize: (ssize_t)sSize { + if (sSize >= 0) + return (id)[[OFNumber of_alloc] initWithSize: sSize]; + if (sSize <= LONG_MIN) + return (id)[[OFNumber of_alloc] initWithLong: (long)sSize]; + return (id)[[OFNumber of_alloc] initWithSSize: sSize]; } - (instancetype)initWithIntMax: (intmax_t)intMax { + if (intMax >= 0) + return (id)[[OFNumber of_alloc] initWithUIntMax: intMax]; + if (intMax <= LLONG_MIN) + return (id)[[OFNumber of_alloc] + initWithLongLong: (long long)intMax]; + return (id)[[OFNumber of_alloc] initWithIntMax: intMax]; } - (instancetype)initWithUIntMax: (uintmax_t)uIntMax { + if (uIntMax <= ULLONG_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedLongLong: (unsigned long long)uIntMax]; + return (id)[[OFNumber of_alloc] initWithUIntMax: uIntMax]; } - (instancetype)initWithPtrDiff: (ptrdiff_t)ptrDiff { + if (ptrDiff >= LLONG_MIN && ptrDiff <= LLONG_MAX) + return (id)[[OFNumber of_alloc] + initWithLongLong: (long long)ptrDiff]; + return (id)[[OFNumber of_alloc] initWithPtrDiff: ptrDiff]; } - (instancetype)initWithIntPtr: (intptr_t)intPtr { + if (intPtr >= 0) + return (id)[[OFNumber of_alloc] initWithUIntPtr: intPtr]; + if (intPtr >= LLONG_MIN) + return (id)[[OFNumber of_alloc] + initWithLongLong: (long long)intPtr]; + return (id)[[OFNumber of_alloc] initWithIntPtr: intPtr]; } - (instancetype)initWithUIntPtr: (uintptr_t)uIntPtr { + if (uIntPtr <= ULLONG_MAX) + return (id)[[OFNumber of_alloc] + initWithUnsignedLongLong: (unsigned long long)uIntPtr]; + return (id)[[OFNumber of_alloc] initWithUIntPtr: uIntPtr]; } - (instancetype)initWithFloat: (float)float_ { + if (float_ == (uintmax_t)float_) + return (id)[[OFNumber of_alloc] + initWithUIntMax: (uintmax_t)float_]; + if (float_ == (intmax_t)float_) + return (id)[[OFNumber of_alloc] + initWithIntMax: (intmax_t)float_]; + return (id)[[OFNumber of_alloc] initWithFloat: float_]; } - (instancetype)initWithDouble: (double)double_ { + if (double_ == (uintmax_t)double_) + return (id)[[OFNumber of_alloc] + initWithUIntMax: (uintmax_t)double_]; + if (double_ == (intmax_t)double_) + return (id)[[OFNumber of_alloc] + initWithIntMax: (intmax_t)double_]; + if (double_ == (float)double_) + return (id)[[OFNumber of_alloc] initWithFloat: (float)double_]; + return (id)[[OFNumber of_alloc] initWithDouble: double_]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { @@ -283,26 +402,26 @@ + (instancetype)numberWithChar: (signed char)sChar { return [[[self alloc] initWithChar: sChar] autorelease]; } -+ (instancetype)numberWithShort: (signed short)sShort ++ (instancetype)numberWithShort: (short)sShort { return [[[self alloc] initWithShort: sShort] autorelease]; } -+ (instancetype)numberWithInt: (signed int)sInt ++ (instancetype)numberWithInt: (int)sInt { return [[[self alloc] initWithInt: sInt] autorelease]; } -+ (instancetype)numberWithLong: (signed long)sLong ++ (instancetype)numberWithLong: (long)sLong { return [[[self alloc] initWithLong: sLong] autorelease]; } -+ (instancetype)numberWithLongLong: (signed long long)sLongLong ++ (instancetype)numberWithLongLong: (long long)sLongLong { return [[[self alloc] initWithLongLong: sLongLong] autorelease]; } + (instancetype)numberWithUnsignedChar: (unsigned char)uChar @@ -438,41 +557,41 @@ _type = OF_NUMBER_TYPE_CHAR; return self; } -- (instancetype)initWithShort: (signed short)sShort +- (instancetype)initWithShort: (short)sShort { self = [super init]; _value.sShort = sShort; _type = OF_NUMBER_TYPE_SHORT; return self; } -- (instancetype)initWithInt: (signed int)sInt +- (instancetype)initWithInt: (int)sInt { self = [super init]; _value.sInt = sInt; _type = OF_NUMBER_TYPE_INT; return self; } -- (instancetype)initWithLong: (signed long)sLong +- (instancetype)initWithLong: (long)sLong { self = [super init]; _value.sLong = sLong; _type = OF_NUMBER_TYPE_LONG; return self; } -- (instancetype)initWithLongLong: (signed long long)sLongLong +- (instancetype)initWithLongLong: (long long)sLongLong { self = [super init]; _value.sLongLong = sLongLong; _type = OF_NUMBER_TYPE_LONGLONG; @@ -761,17 +880,17 @@ case OF_NUMBER_TYPE_BOOL: return @encode(bool); case OF_NUMBER_TYPE_CHAR: return @encode(signed char); case OF_NUMBER_TYPE_SHORT: - return @encode(signed short); + return @encode(short); case OF_NUMBER_TYPE_INT: - return @encode(signed int); + return @encode(int); case OF_NUMBER_TYPE_LONG: - return @encode(signed long); + return @encode(long); case OF_NUMBER_TYPE_LONGLONG: - return @encode(signed long long); + return @encode(long long); case OF_NUMBER_TYPE_UCHAR: return @encode(unsigned char); case OF_NUMBER_TYPE_USHORT: return @encode(unsigned short); case OF_NUMBER_TYPE_UINT: @@ -834,32 +953,32 @@ @throw [OFOutOfRangeException exception]; memcpy(value, &_value.sChar, sizeof(signed char)); break; case OF_NUMBER_TYPE_SHORT: - if (size != sizeof(signed short)) + if (size != sizeof(short)) @throw [OFOutOfRangeException exception]; - memcpy(value, &_value.sShort, sizeof(signed short)); + memcpy(value, &_value.sShort, sizeof(short)); break; case OF_NUMBER_TYPE_INT: - if (size != sizeof(signed int)) + if (size != sizeof(int)) @throw [OFOutOfRangeException exception]; - memcpy(value, &_value.sInt, sizeof(signed int)); + memcpy(value, &_value.sInt, sizeof(int)); break; case OF_NUMBER_TYPE_LONG: - if (size != sizeof(signed long)) + if (size != sizeof(long)) @throw [OFOutOfRangeException exception]; - memcpy(value, &_value.sLong, sizeof(signed long)); + memcpy(value, &_value.sLong, sizeof(long)); break; case OF_NUMBER_TYPE_LONGLONG: - if (size != sizeof(signed long long)) + if (size != sizeof(long long)) @throw [OFOutOfRangeException exception]; - memcpy(value, &_value.sLongLong, sizeof(signed long long)); + memcpy(value, &_value.sLongLong, sizeof(long long)); break; case OF_NUMBER_TYPE_UCHAR: if (size != sizeof(unsigned char)) @throw [OFOutOfRangeException exception]; @@ -1004,28 +1123,28 @@ - (signed char)charValue { RETURN_AS(signed char) } -- (signed short)shortValue -{ - RETURN_AS(signed short) -} - -- (signed int)intValue -{ - RETURN_AS(signed int) -} - -- (signed long)longValue -{ - RETURN_AS(signed long) -} - -- (signed long long)longLongValue -{ - RETURN_AS(signed long long) +- (short)shortValue +{ + RETURN_AS(short) +} + +- (int)intValue +{ + RETURN_AS(int) +} + +- (long)longValue +{ + RETURN_AS(long) +} + +- (long long)longLongValue +{ + RETURN_AS(long long) } - (unsigned char)unsignedCharValue { RETURN_AS(unsigned char) Index: tests/serialization.xml ================================================================== --- tests/serialization.xml +++ tests/serialization.xml @@ -15,11 +15,11 @@ Qu"xbar test - 1234 + 1234 40934a456d5cfaad asd 40934a456d5cfaad