Tweaks to NSDate changes

This commit is contained in:
rfm 2024-11-01 08:47:02 +00:00
parent 7a59450341
commit d0bff27c77
2 changed files with 113 additions and 78 deletions

View file

@ -1,3 +1,8 @@
2024-11-01 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSDate.m: Fix returing receiver when earlier/later argument
is equal to the receiver. Various formatting fixes.
2024-10-29 Richard Frith-Macdonald <rfm@gnu.org> 2024-10-29 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSUndoManager.m: set zero/nil return value when forwarding * Source/NSUndoManager.m: set zero/nil return value when forwarding

View file

@ -158,7 +158,9 @@ union DoubleBits {
}; };
}; };
static __attribute__((always_inline)) uintptr_t compressTimeInterval(NSTimeInterval interval) { static __attribute__((always_inline)) uintptr_t
compressTimeInterval(NSTimeInterval interval)
{
union CompressedDouble c; union CompressedDouble c;
union DoubleBits db; union DoubleBits db;
intptr_t exponent; intptr_t exponent;
@ -178,7 +180,9 @@ static __attribute__((always_inline)) uintptr_t compressTimeInterval(NSTimeInter
return c.data; return c.data;
} }
static __attribute__((always_inline)) NSTimeInterval decompressTimeInterval(uintptr_t compressed) { static __attribute__((always_inline)) NSTimeInterval
decompressTimeInterval(uintptr_t compressed)
{
union CompressedDouble c; union CompressedDouble c;
union DoubleBits d; union DoubleBits d;
intptr_t biased_exponent; intptr_t biased_exponent;
@ -234,10 +238,13 @@ static BOOL useSmallDate;
+ (void) load + (void) load
{ {
useSmallDate = objc_registerSmallObjectClass_np(self, SMALL_DATE_MASK); useSmallDate = objc_registerSmallObjectClass_np(self, SMALL_DATE_MASK);
// If this fails, someone else has already registered a small object class for this slot. /* If this fails, someone else has already registered
* a small object class for this slot.
*/
if (unlikely(useSmallDate == NO)) if (unlikely(useSmallDate == NO))
{ {
[NSException raise: NSInternalInconsistencyException format: @"Failed to register GSSmallDate small object class"]; [NSException raise: NSInternalInconsistencyException
format: @"Failed to register GSSmallDate small object class"];
} }
} }
@ -349,6 +356,7 @@ static BOOL useSmallDate;
- (id) initWithCoder: (NSCoder*)coder - (id) initWithCoder: (NSCoder*)coder
{ {
double secondsSinceRef; double secondsSinceRef;
if ([coder allowsKeyedCoding]) if ([coder allowsKeyedCoding])
{ {
secondsSinceRef = [coder decodeDoubleForKey: @"NS.time"]; secondsSinceRef = [coder decodeDoubleForKey: @"NS.time"];
@ -396,7 +404,9 @@ static BOOL useSmallDate;
if (IS_CONCRETE_CLASS(otherDate)) if (IS_CONCRETE_CLASS(otherDate))
{ {
otherTime = GET_INTERVAL(otherDate); otherTime = GET_INTERVAL(otherDate);
} else { }
else
{
otherTime = [otherDate timeIntervalSinceReferenceDate]; otherTime = [otherDate timeIntervalSinceReferenceDate];
} }
@ -424,10 +434,13 @@ static BOOL useSmallDate;
if (IS_CONCRETE_CLASS(other)) if (IS_CONCRETE_CLASS(other))
{ {
otherTime = GET_INTERVAL(other); otherTime = GET_INTERVAL(other);
} else if ([other isKindOfClass: abstractClass]) }
else if ([other isKindOfClass: abstractClass])
{ {
otherTime = [other timeIntervalSinceReferenceDate]; otherTime = [other timeIntervalSinceReferenceDate];
} else { }
else
{
return NO; return NO;
} }
@ -454,12 +467,16 @@ static BOOL useSmallDate;
if (IS_CONCRETE_CLASS(otherDate)) if (IS_CONCRETE_CLASS(otherDate))
{ {
otherTime = GET_INTERVAL(otherDate); otherTime = GET_INTERVAL(otherDate);
} else { }
else
{
otherTime = [otherDate timeIntervalSinceReferenceDate]; otherTime = [otherDate timeIntervalSinceReferenceDate];
} }
// If the receiver and anotherDate represent the same date, returns the receiver. /* If the receiver and anotherDate represent the same date,
if (selfTime <= otherTime) * returns the receiver.
*/
if (selfTime < otherTime)
{ {
return otherDate; return otherDate;
} }
@ -482,12 +499,16 @@ static BOOL useSmallDate;
if (IS_CONCRETE_CLASS(otherDate)) if (IS_CONCRETE_CLASS(otherDate))
{ {
otherTime = GET_INTERVAL(otherDate); otherTime = GET_INTERVAL(otherDate);
} else { }
else
{
otherTime = [otherDate timeIntervalSinceReferenceDate]; otherTime = [otherDate timeIntervalSinceReferenceDate];
} }
// If the receiver and anotherDate represent the same date, returns the receiver. /* If the receiver and anotherDate represent the same date,
if (selfTime >= otherTime) * returns the receiver.
*/
if (selfTime > otherTime)
{ {
return otherDate; return otherDate;
} }
@ -498,6 +519,7 @@ static BOOL useSmallDate;
- (void) encodeWithCoder: (NSCoder*)coder - (void) encodeWithCoder: (NSCoder*)coder
{ {
double time = GET_INTERVAL(self); double time = GET_INTERVAL(self);
if ([coder allowsKeyedCoding]) if ([coder allowsKeyedCoding])
{ {
[coder encodeDouble: time forKey:@"NS.time"]; [coder encodeDouble: time forKey:@"NS.time"];
@ -519,6 +541,7 @@ static BOOL useSmallDate;
- (NSTimeInterval) timeIntervalSinceDate: (NSDate*)otherDate - (NSTimeInterval) timeIntervalSinceDate: (NSDate*)otherDate
{ {
double otherTime; double otherTime;
if (unlikely(otherDate == nil)) if (unlikely(otherDate == nil))
{ {
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
@ -528,7 +551,9 @@ static BOOL useSmallDate;
if (IS_CONCRETE_CLASS(otherDate)) if (IS_CONCRETE_CLASS(otherDate))
{ {
otherTime = GET_INTERVAL(otherDate); otherTime = GET_INTERVAL(otherDate);
} else { }
else
{
otherTime = [otherDate timeIntervalSinceReferenceDate]; otherTime = [otherDate timeIntervalSinceReferenceDate];
} }
@ -700,7 +725,9 @@ otherTime(NSDate* other)
if (self == abstractClass) if (self == abstractClass)
{ {
#if USE_SMALL_DATE #if USE_SMALL_DATE
return [DATE_CONCRETE_CLASS_NAME alloc]; // alloc is overridden to return a small object /* alloc is overridden to return a small object
*/
return [DATE_CONCRETE_CLASS_NAME alloc];
#else #else
return NSAllocateObject(concreteClass, 0, NSDefaultMallocZone()); return NSAllocateObject(concreteClass, 0, NSDefaultMallocZone());
#endif #endif
@ -713,7 +740,9 @@ otherTime(NSDate* other)
if (self == abstractClass) if (self == abstractClass)
{ {
#if USE_SMALL_DATE #if USE_SMALL_DATE
return [DATE_CONCRETE_CLASS_NAME alloc]; // alloc is overridden to return a small object /* alloc is overridden to return a small object
*/
return [DATE_CONCRETE_CLASS_NAME alloc];
#else #else
return NSAllocateObject(concreteClass, 0, z); return NSAllocateObject(concreteClass, 0, z);
#endif #endif
@ -1784,6 +1813,7 @@ otherTime(NSDate* other)
- (NSDate*) laterDate: (NSDate*)otherDate - (NSDate*) laterDate: (NSDate*)otherDate
{ {
double selfTime; double selfTime;
if (otherDate == nil) if (otherDate == nil)
{ {
return nil; return nil;