Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions ARCMacros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ARCMacros.h
// InnerBand
//
// For an explanation of why these work, see:
//
// http://raptureinvenice.com/arc-support-without-branches/
//
// Created by John Blanco on 1/28/12.
// Copyright (c) 2012 Rapture In Venice. All rights reserved.
//
// NOTE: __bridge_tranfer is not included here because releasing would be inconsistent.
// Avoid it unless you're using ARC exclusively or managing it with __has_feature(objc_arc).
//

#if !defined(__clang__) || __clang_major__ < 3
#ifndef __bridge
#define __bridge
#endif

#ifndef __bridge_retain
#define __bridge_retain
#endif

#ifndef __bridge_retained
#define __bridge_retained
#endif

#ifndef __autoreleasing
#define __autoreleasing
#endif

#ifndef __strong
#define __strong
#endif

#ifndef __unsafe_unretained
#define __unsafe_unretained
#endif

#ifndef __weak
#define __weak
#endif
#endif

#if __has_feature(objc_arc)
#define SAFE_ARC_PROP_RETAIN strong
#define SAFE_ARC_RETAIN(x) (x)
#define SAFE_ARC_RELEASE(x)
#define SAFE_ARC_AUTORELEASE(x) (x)
#define SAFE_ARC_BLOCK_COPY(x) (x)
#define SAFE_ARC_BLOCK_RELEASE(x)
#define SAFE_ARC_SUPER_DEALLOC()
#define SAFE_ARC_AUTORELEASE_POOL_START() @autoreleasepool {
#define SAFE_ARC_AUTORELEASE_POOL_END() }
#else
#define SAFE_ARC_PROP_RETAIN retain
#define SAFE_ARC_RETAIN(x) ([(x) retain])
#define SAFE_ARC_RELEASE(x) ([(x) release])
#define SAFE_ARC_AUTORELEASE(x) ([(x) autorelease])
#define SAFE_ARC_BLOCK_COPY(x) (Block_copy(x))
#define SAFE_ARC_BLOCK_RELEASE(x) (Block_release(x))
#define SAFE_ARC_SUPER_DEALLOC() ([super dealloc])
#define SAFE_ARC_AUTORELEASE_POOL_START() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#define SAFE_ARC_AUTORELEASE_POOL_END() [pool release];
#endif
46 changes: 25 additions & 21 deletions ISO8601DateFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#import <Foundation/Foundation.h>
#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"

#ifndef DEFAULT_TIME_SEPARATOR
# define DEFAULT_TIME_SEPARATOR ':'
Expand Down Expand Up @@ -44,20 +45,23 @@ + (void) initialize {
+ (void) purgeGlobalCaches {
NSMutableDictionary *oldCache = timeZonesByOffset;
timeZonesByOffset = nil;
[oldCache release];
#if __has_feature(objc_arc)
[oldCache removeAllObjects];
#endif
SAFE_ARC_RELEASE(oldCache);
}

- (NSCalendar *) makeCalendarWithDesiredConfiguration {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSCalendar *calendar = SAFE_ARC_AUTORELEASE([[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]);
calendar.firstWeekday = 2; //Monday
calendar.timeZone = [NSTimeZone defaultTimeZone];
return calendar;
}

- (id) init {
if ((self = [super init])) {
parsingCalendar = [[self makeCalendarWithDesiredConfiguration] retain];
unparsingCalendar = [[self makeCalendarWithDesiredConfiguration] retain];
parsingCalendar = SAFE_ARC_RETAIN([self makeCalendarWithDesiredConfiguration]);
unparsingCalendar = SAFE_ARC_RETAIN([self makeCalendarWithDesiredConfiguration]);

format = ISO8601DateFormatCalendar;
timeSeparator = ISO8601DefaultTimeSeparatorCharacter;
Expand All @@ -67,21 +71,21 @@ - (id) init {
return self;
}
- (void) dealloc {
[defaultTimeZone release];
SAFE_ARC_RELEASE(defaultTimeZone);

[unparsingFormatter release];
[lastUsedFormatString release];
[parsingCalendar release];
[unparsingCalendar release];
SAFE_ARC_RELEASE(unparsingFormatter);
SAFE_ARC_RELEASE(lastUsedFormatString);
SAFE_ARC_RELEASE(parsingCalendar);
SAFE_ARC_RELEASE(unparsingCalendar);

[super dealloc];
SAFE_ARC_SUPER_DEALLOC();
}

@synthesize defaultTimeZone;
- (void) setDefaultTimeZone:(NSTimeZone *)tz {
if (defaultTimeZone != tz) {
[defaultTimeZone release];
defaultTimeZone = [tz retain];
SAFE_ARC_RELEASE(defaultTimeZone);
defaultTimeZone = SAFE_ARC_RETAIN(tz);

unparsingCalendar.timeZone = defaultTimeZone;
}
Expand All @@ -105,7 +109,7 @@ - (void) setDefaultTimeZone:(NSTimeZone *)tz {
*YYYY-MM-DD
*YYYY-MM
*YYYY
*YY //century
*YY //century
* //Implied century: YY is 00-99
* YYMMDD
* YY-MM-DD
Expand Down Expand Up @@ -156,7 +160,7 @@ - (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out
- (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange {
NSDate *now = [NSDate date];

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
NSDateComponents *components = SAFE_ARC_AUTORELEASE([[NSDateComponents alloc] init]);
NSDateComponents *nowComponents = [parsingCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];

NSUInteger
Expand Down Expand Up @@ -642,7 +646,7 @@ - (BOOL)getObjectValue:(id *)outValue forString:(NSString *)string errorDescript

- (NSString *) replaceColonsInString:(NSString *)timeFormat withTimeSeparator:(unichar)timeSep {
if (timeSep != ':') {
NSMutableString *timeFormatMutable = [[timeFormat mutableCopy] autorelease];
NSMutableString *timeFormatMutable = SAFE_ARC_AUTORELEASE([timeFormat mutableCopy]);
[timeFormatMutable replaceOccurrencesOfString:@":"
withString:[NSString stringWithCharacters:&timeSep length:1U]
options:NSBackwardsSearch | NSLiteralSearch
Expand All @@ -667,7 +671,7 @@ - (NSString *) stringFromDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone {
case ISO8601DateFormatOrdinal:
return [self stringFromDate:date formatString:ISO_ORDINAL_DATE_FORMAT timeZone:timeZone];
default:
[NSException raise:NSInternalInconsistencyException format:@"self.format was %ld, not calendar (%d), week (%d), or ordinal (%d)", self.format, ISO8601DateFormatCalendar, ISO8601DateFormatWeek, ISO8601DateFormatOrdinal];
[NSException raise:NSInternalInconsistencyException format:@"self.format was %u, not calendar (%d), week (%d), or ordinal (%d)", self.format, ISO8601DateFormatCalendar, ISO8601DateFormatWeek, ISO8601DateFormatOrdinal];
return nil;
}
}
Expand All @@ -679,11 +683,11 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
unparsingCalendar.timeZone = timeZone;

if (dateFormat != lastUsedFormatString) {
[unparsingFormatter release];
SAFE_ARC_RELEASE(unparsingFormatter);
unparsingFormatter = nil;

[lastUsedFormatString release];
lastUsedFormatString = [dateFormat retain];
SAFE_ARC_RELEASE(lastUsedFormatString);
lastUsedFormatString = SAFE_ARC_RETAIN(dateFormat);
}

if (!unparsingFormatter) {
Expand All @@ -701,7 +705,7 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
if (offset == 0)
str = [str stringByAppendingString:ISO_TIMEZONE_UTC_FORMAT];
else
str = [str stringByAppendingFormat:ISO_TIMEZONE_OFFSET_FORMAT, (int)(offset / 60), (int)(offset % 60)];
str = [str stringByAppendingFormat:ISO_TIMEZONE_OFFSET_FORMAT, (int)offset / 60, (int)offset % 60];
}

//Undo the change we made earlier
Expand Down Expand Up @@ -783,7 +787,7 @@ - (NSString *) weekDateStringForDate:(NSDate *)date timeZone:(NSTimeZone *)timeZ

timeString = [formatter stringForObjectValue:date];

[formatter release];
SAFE_ARC_RELEASE(formatter);
} else
timeString = @"";

Expand Down
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
CLANG=/Developer/usr/bin/clang
ifeq ($(wildcard /Developer/usr/bin/clang),)
CLANG=/usr/bin/clang
else
CLANG=/Developer/usr/bin/clang
endif
CC=$(CLANG)
CFLAGS+=-std=c99 -g -Werror -Wmissing-field-initializers -Wreturn-type -Wmissing-braces -Wparentheses -Wswitch -Wunused-function -Wunused-label -Wunused-variable -Wunused-value -Wshadow -Wsign-compare -Wnewline-eof -Wshorten-64-to-32 -Wundeclared-selector -Wmissing-prototypes -Wformat -Wunknown-pragmas
LDFLAGS+=-framework Foundation
Expand Down Expand Up @@ -30,3 +34,16 @@ ISO8601DateFormatter-analysis.plist: ISO8601DateFormatter.m
$(CLANG) $^ --analyze -o /dev/null

timetrial: timetrial.o ISO8601DateFormatter.o

clean:
rm *.o
rm *.out
rm testparser
rm testparser.sh
rm testunparsewithtime
rm timetrial
rm unparse-date
rm unparse-ordinaldate
rm unparse-weekdate

alltests: test testunparsewithtime timetrial
7 changes: 4 additions & 3 deletions testparser.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import <Foundation/Foundation.h>
#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"

int main(int argc, const char **argv) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
SAFE_ARC_AUTORELEASE_POOL_START()

BOOL parseStrictly = NO;
if((argc > 1) && (strcmp(argv[1], "--strict") == 0)) {
Expand All @@ -12,7 +13,7 @@ int main(int argc, const char **argv) {

[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+0]];

ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
ISO8601DateFormatter *formatter = SAFE_ARC_AUTORELEASE([[ISO8601DateFormatter alloc] init]);
formatter.parsesStrictly = parseStrictly;

while(--argc) {
Expand All @@ -22,6 +23,6 @@ int main(int argc, const char **argv) {
fputs([[NSString stringWithFormat:@"%@ %C %@\n", str, 0x2192, date] UTF8String], stdout);
}

[pool release];
SAFE_ARC_AUTORELEASE_POOL_END()
return 0;
}
11 changes: 6 additions & 5 deletions testunparsewithtime.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#import <Foundation/Foundation.h>
#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"

static void testFormatStrings(int hour, int minute);

int main(void) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
SAFE_ARC_AUTORELEASE_POOL_START()
ISO8601DateFormatter *formatter = SAFE_ARC_AUTORELEASE([[ISO8601DateFormatter alloc] init]);
formatter.includeTime = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:336614400.0];
NSLog(@"2011-09-01 at 5 PM ET: %@", [formatter stringFromDate:date]);
Expand All @@ -14,7 +15,7 @@ int main(void) {
testFormatStrings(2, 6);
testFormatStrings(-2, 6);

[pool drain];
SAFE_ARC_AUTORELEASE_POOL_END()
return EXIT_SUCCESS;
}

Expand All @@ -32,7 +33,7 @@ static void testFormatStrings(int hour, int minute) {
}
printf("Testing with printf:\n");
for (NSString *format in formatStrings) {
format = [format stringByReplacingOccurrencesOfString:@"%@" withString:@"%s"];
printf([[format stringByAppendingString:@"\n"] UTF8String], [format UTF8String], hour, minute);
NSString *cFormat = [format stringByReplacingOccurrencesOfString:@"%@" withString:@"%s"];
printf([[cFormat stringByAppendingString:@"\n"] UTF8String], [cFormat UTF8String], hour, minute);
}
}
31 changes: 21 additions & 10 deletions timetrial.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#import <Foundation/Foundation.h>

#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"

int main(void) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

sleep(1);

ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
void test_part_one(void);
void test_part_one(void)
{
SAFE_ARC_AUTORELEASE_POOL_START()
ISO8601DateFormatter *formatter = SAFE_ARC_AUTORELEASE([[ISO8601DateFormatter alloc] init]);
NSString *inString = @"2011-04-12T13:15:17-0800";
NSUInteger numResults = 0;
NSDate *start, *end;
Expand All @@ -26,12 +26,23 @@ int main(void) {
NSLog(@"Number of dates and strings computed: %lu each", (unsigned long)numResults);
NSLog(@"Time taken per date: %f seconds", [end timeIntervalSinceDate:start] / numReps);

[pool drain];
pool = [[NSAutoreleasePool alloc] init];
SAFE_ARC_AUTORELEASE_POOL_END()
}

int main(void) {

sleep(1);

numResults = 0;
test_part_one();

SAFE_ARC_AUTORELEASE_POOL_START()

sleep(1);

NSString *inString = @"2011-04-12T13:15:17-0800";
NSUInteger numResults = 0;
NSDate *start, *end;
enum { numReps = 10000 };

NSLog(@"Timing C standard library parsing and unparsing");

Expand Down Expand Up @@ -63,6 +74,6 @@ int main(void) {

sleep(1);

[pool drain];
SAFE_ARC_AUTORELEASE_POOL_END()
return EXIT_SUCCESS;
}
8 changes: 5 additions & 3 deletions unparse-date.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"


int main(int argc, const char **argv) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
SAFE_ARC_AUTORELEASE_POOL_START()

ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
ISO8601DateFormatter *formatter = SAFE_ARC_AUTORELEASE([[ISO8601DateFormatter alloc] init]);
formatter.format = ISO8601DateFormatCalendar;

BOOL forceUTC = NO;
Expand All @@ -28,6 +30,6 @@ int main(int argc, const char **argv) {
printf("%s\n", [[NSString stringWithFormat:@"%@:\t%@", arg, [formatter stringFromDate:date timeZone:timeZone]] UTF8String]);
}

[pool release];
SAFE_ARC_AUTORELEASE_POOL_END()
return 0;
}
7 changes: 4 additions & 3 deletions unparse-ordinaldate.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#import "ISO8601DateFormatter.h"
#import "ARCMacros.h"

int main(int argc, const char **argv) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
SAFE_ARC_AUTORELEASE_POOL_START()

ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
ISO8601DateFormatter *formatter = SAFE_ARC_AUTORELEASE([[ISO8601DateFormatter alloc] init]);
formatter.format = ISO8601DateFormatOrdinal;

while(--argc) {
Expand All @@ -12,6 +13,6 @@ int main(int argc, const char **argv) {
printf("%s\n", [[NSString stringWithFormat:@"%@:\t%@", arg, [formatter stringFromDate:[formatter dateFromString:arg timeZone:&timeZone] timeZone:timeZone]] UTF8String]);
}

[pool release];
SAFE_ARC_AUTORELEASE_POOL_END()
return 0;
}
Loading