Revision: 28365
          http://sourceforge.net/p/bibdesk/svn/28365
Author:   hofman
Date:     2023-09-22 14:52:05 +0000 (Fri, 22 Sep 2023)
Log Message:
-----------
don't bother to do full date parsing when just normalizing year format

Modified Paths:
--------------
    trunk/bibdesk/BDSKFormatParser.m
    trunk/bibdesk/BibItem.m
    trunk/bibdesk/NSDate_BDSKExtensions.h
    trunk/bibdesk/NSDate_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKFormatParser.m
===================================================================
--- trunk/bibdesk/BDSKFormatParser.m    2023-09-21 15:58:36 UTC (rev 28364)
+++ trunk/bibdesk/BDSKFormatParser.m    2023-09-22 14:52:05 UTC (rev 28365)
@@ -350,10 +350,8 @@
                        // year without century
                     NSString *yearString = [pub 
stringValueOfField:BDSKYearString];
                     if ([NSString isEmptyString:yearString] == NO) {
-                        NSDate *date = [[NSDate alloc] 
initWithMonthString:@"6" yearString:yearString];
-                                               yearString = [date 
descriptionWithCalendarFormat:@"%y" timeZone:nil locale:nil];
+                        yearString = [NSString stringWithFormat:@"%.2ld", 
(long)([yearString integerValue] % 100)];
                                                [parsedStr 
appendString:yearString];
-                        [date release];
                                        }
                                        break;
                                }
@@ -362,10 +360,11 @@
                        // year with century
                     NSString *yearString = [pub 
stringValueOfField:BDSKYearString];
                     if ([NSString isEmptyString:yearString] == NO) {
-                        NSDate *date = [[NSDate alloc] 
initWithMonthString:@"6" yearString:yearString];
-                                               yearString = [date 
descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil];
+                        NSInteger y = [yearString integerValue];
+                        if ([yearString length] <= 2)
+                            y += y < 50 ? 2000 : 1900;
+                        yearString = [NSString stringWithFormat:@"%ld", 
(long)y];
                                                [parsedStr 
appendString:yearString];
-                        [date release];
                                        }
                                        break;
                                }
@@ -383,10 +382,9 @@
                             else
                                 monthString = [(BDSKStringNode *)[nodes 
objectAtIndex:0] value];
                         }
-                        NSDate *date = [[NSDate alloc] 
initWithMonthString:monthString yearString:@"2000"];
+                        NSDate *date = [NSDate dateWithMonthString:monthString 
yearString:@"2000"];
                                                monthString = [date 
descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil];
                                                [parsedStr 
appendString:monthString];
-                        [date release];
                                        }
                                        break;
                                }

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2023-09-21 15:58:36 UTC (rev 28364)
+++ trunk/bibdesk/BibItem.m     2023-09-22 14:52:05 UTC (rev 28365)
@@ -4183,9 +4183,8 @@
     // pubDate is a derived field based on Month and Year fields; we take the 
15th day of the month to avoid edge cases
     if (key == nil || allFieldsChanged || [BDSKYearString isEqualToString:key] 
|| [BDSKMonthString isEqualToString:key]) {
         // allows month as number, name or abbreviated name
-        theDate = [[NSDate alloc] initWithMonthString:[pubFields 
objectForKey:BDSKMonthString] yearString:[pubFields 
objectForKey:BDSKYearString]];
+        theDate = [NSDate dateWithMonthString:[pubFields 
objectForKey:BDSKMonthString] yearString:[pubFields 
objectForKey:BDSKYearString]];
         [self setDate:theDate];
-        [theDate release];
        }
        
     // initially or when all fields are changed set the added and modified 
date based on the field values

Modified: trunk/bibdesk/NSDate_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-21 15:58:36 UTC (rev 
28364)
+++ trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-22 14:52:05 UTC (rev 
28365)
@@ -48,17 +48,7 @@
 
 @interface NSDate (BDSKExtensions)
 
-/*!
-    @method     initWithMonthDayYearString:
-    @abstract   Creates a new string by parsing a month-day-year string of the 
form Jan-1-1999; uses fuzzy matching, so Jan
-                could be january.
-    @discussion (comprehensive description)
-    @param      dateString (description)
-    @result     (description)
-*/
-- (id)initWithMonthDayYearString:(NSString *)dateString;
-// used for field values, tries to clean the month first and then uses 
initWithMonthDayYearString:
-- (id)initWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString;
++ (NSDate *)dateWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString;
 
 - (NSString *)dateDescription;
 - (NSString *)longDateDescription;

Modified: trunk/bibdesk/NSDate_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-21 15:58:36 UTC (rev 
28364)
+++ trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-22 14:52:05 UTC (rev 
28365)
@@ -45,139 +45,81 @@
 
 @implementation NSDate (BDSKExtensions)
 
-- (id)initWithMonthDayYearString:(NSString *)dateString;
-{    
-    [[self init] release];
-    self = nil;
-    
-    CFAllocatorRef alloc = CFAllocatorGetDefault();
-    
-    static id locale = nil;
-    if (nil == locale) {
-        locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
-    }
-    static CFDateFormatterRef dateFormatter = nil;
-    if (NULL == dateFormatter) {
-        // use the en locale, since dates use en short names as keys in BibTeX
-        CFLocaleRef enLocale = CFLocaleCreate(alloc, CFSTR("en_US_POSIX"));
-    
-        // the formatter styles aren't used here, since we set an explicit 
format
-        dateFormatter = CFDateFormatterCreate(alloc, enLocale, 
kCFDateFormatterLongStyle, kCFDateFormatterLongStyle);
-
-        if(NULL != dateFormatter){
-            // CFDateFormatter uses ICU formats: 
http://icu.sourceforge.net/userguide/formatDateTime.html
-            CFDateFormatterSetFormat(dateFormatter, CFSTR("MMM-dd-yy"));
-            CFDateFormatterSetProperty(dateFormatter, 
kCFDateFormatterIsLenient, kCFBooleanTrue);    
-        }
-        
-        if(enLocale) CFRelease(enLocale);
-    }
-    static CFDateFormatterRef numericDateFormatter = nil;
-    if (NULL == numericDateFormatter) {
-        // use the en locale, since dates use en short names as keys in BibTeX
-        CFLocaleRef enLocale = CFLocaleCreate(alloc, CFSTR("en_US_POSIX"));
-        
-        // the formatter styles aren't used here, since we set an explicit 
format
-        numericDateFormatter = CFDateFormatterCreate(alloc, enLocale, 
kCFDateFormatterLongStyle, kCFDateFormatterLongStyle);
-        
-        // CFDateFormatter uses ICU formats: 
http://icu.sourceforge.net/userguide/formatDateTime.html
-        CFDateFormatterSetFormat(numericDateFormatter, CFSTR("MM-dd-yy"));
-        CFDateFormatterSetProperty(numericDateFormatter, 
kCFDateFormatterIsLenient, kCFBooleanTrue);            
-        
-        if(enLocale) CFRelease(enLocale);
-    }
-    
-    CFDateRef date = CFDateFormatterCreateDateFromString(alloc, dateFormatter, 
(CFStringRef)dateString, NULL);
-    
-    if(date != nil)
-        return (NSDate *)date;
-    
-    // If we didn't get a valid date on the first attempt, let's try a purely 
numeric formatter    
-    date = CFDateFormatterCreateDateFromString(alloc, numericDateFormatter, 
(CFStringRef)dateString, NULL);
-    
-    if(date != nil)
-        return (NSDate *)date;
-    
-    // Now fall back to natural language parsing, which is fairly 
memory-intensive.
-    // We should be able to use NSDateFormatter with the natural language 
option, but it doesn't seem to work as well as +dateWithNaturalLanguageString
-    return [[NSDate dateWithNaturalLanguageString:dateString locale:locale] 
retain];
-}
-
-- (id)initWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString {
++ (NSDate *)dateWithMonthString:(NSString *)monthString yearString:(NSString 
*)yearString {
     if([yearString isComplex])
         yearString = [(BDSKStringNode *)[[yearString nodes] objectAtIndex:0] 
value];
-    if ([NSString isEmptyString:yearString]) {
-        [[self init] release];
-        self = nil;
-    } else {
-        if([monthString isComplex]) {
-            BDSKStringNode *node = nil;
-            NSArray *nodes = [monthString nodes];
-            for (node in nodes) {
-                if ([node type] == BDSKStringNodeMacro) {
-                    monthString = [node value];
-                    break;
-                }
+    if ([NSString isEmptyString:yearString])
+        return nil;
+    if([monthString isComplex]) {
+        BDSKStringNode *node = nil;
+        NSArray *nodes = [monthString nodes];
+        for (node in nodes) {
+            if ([node type] == BDSKStringNodeMacro) {
+                monthString = [node value];
+                break;
             }
-            if (node == nil)
-                monthString = [(BDSKStringNode *)[nodes objectAtIndex:0] 
value];
-        } else if ([NSString isEmptyString:monthString] == NO) {
-            NSRange r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
letterCharacterSet]];
-            NSUInteger start = 0, end = [monthString length];
+        }
+        if (node == nil)
+            monthString = [(BDSKStringNode *)[nodes objectAtIndex:0] value];
+    } else if ([NSString isEmptyString:monthString] == NO) {
+        NSRange r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
letterCharacterSet]];
+        NSUInteger start = 0, end = [monthString length];
+        if (r.location != NSNotFound) {
+            start = r.location;
+            r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
nonLetterCharacterSet] options:0 range:NSMakeRange(start, end - start)];
+            if (r.location != NSNotFound)
+                end = r.location;
+        } else {
+            r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
decimalDigitCharacterSet]];
             if (r.location != NSNotFound) {
                 start = r.location;
-                r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
nonLetterCharacterSet] options:0 range:NSMakeRange(start, end - start)];
+                r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
nonDecimalDigitCharacterSet] options:0 range:NSMakeRange(start, end - start)];
                 if (r.location != NSNotFound)
                     end = r.location;
-            } else {
-                r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
decimalDigitCharacterSet]];
-                if (r.location != NSNotFound) {
-                    start = r.location;
-                    r = [monthString rangeOfCharacterFromSet:[NSCharacterSet 
nonDecimalDigitCharacterSet] options:0 range:NSMakeRange(start, end - start)];
-                    if (r.location != NSNotFound)
-                        end = r.location;
-                }
             }
-            if (start > 0 || end < [monthString length])
-                monthString = [monthString 
substringWithRange:NSMakeRange(start, end - start)];
         }
-        static NSCalendar *calendar = nil;
-        if (calendar == nil) {
-            calendar = [[NSCalendar alloc] 
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
-            [calendar setLocale:[NSLocale 
localeWithLocaleIdentifier:@"en_US_POSIX"]];
+        if (start > 0 || end < [monthString length])
+            monthString = [monthString substringWithRange:NSMakeRange(start, 
end - start)];
+    }
+    static NSCalendar *calendar = nil;
+    if (calendar == nil) {
+        calendar = [[NSCalendar alloc] 
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
+        [calendar setLocale:[NSLocale 
localeWithLocaleIdentifier:@"en_US_POSIX"]];
+    }
+    if ([NSString isEmptyString:monthString])
+        monthString = @"1";
+    NSInteger year = [yearString integerValue];
+    if ([yearString length] <= 2) {
+        year += 2000;
+        if (year > [calendar component:NSCalendarUnitYear fromDate:[NSDate 
date]])
+            year -= 100;
+    }
+    NSInteger month = 1;
+    if ([monthString length] < 3 && [[NSCharacterSet decimalDigitCharacterSet] 
characterIsMember:[monthString characterAtIndex:0]]) {
+        month = [monthString integerValue];
+    } else {
+        monthString = [monthString lowercaseString];
+        NSUInteger i = [[[calendar shortMonthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
+        if (i == NSNotFound) {
+            i = [[[calendar monthSymbols] valueForKey:@"lowercaseString"] 
indexOfObject:monthString];
+            if (i == NSNotFound && [[[NSLocale currentLocale] 
localeIdentifier] hasPrefix:@"en"] == NO) {
+                [calendar setLocale:[NSLocale currentLocale]];
+                i = [[[calendar shortMonthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
+                if (i == NSNotFound)
+                    i = [[[calendar monthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:monthString];
+                [calendar setLocale:[NSLocale 
localeWithLocaleIdentifier:@"en_US_POSIX"]];
+            }
         }
-        if ([NSString isEmptyString:monthString])
-            monthString = @"1";
-        NSInteger year = [yearString integerValue], month = 1;
-        if ([monthString length] <= 2 && [[NSCharacterSet 
decimalDigitCharacterSet] characterIsMember:[monthString characterAtIndex:0]]) {
-            month = [monthString integerValue];
-        } else {
-            NSUInteger i = [[[calendar monthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:[monthString lowercaseString]];
-            if (i == NSNotFound)
-                i = [[[calendar shortMonthSymbols] 
valueForKey:@"lowercaseString"] indexOfObject:[monthString lowercaseString]];
-            month = i == NSNotFound ? 1 : i + 1;
-        }
-        if ([yearString length] <= 2) {
-            year += 2000;
-            if (year > [calendar component:NSCalendarUnitYear fromDate:[NSDate 
date]])
-                year -= 100;
-        }
-        NSDateComponents *components = [[[NSDateComponents alloc] init] 
autorelease];
-        [components setYear:year];
-        [components setMonth:month];
-        [components setDay:15];
-        [components setHour:12];
-        [components setMonth:0];
-        [components setSecond:0];
-        NSDate *date = [calendar dateFromComponents:components];
-        if (date) {
-            [self release];
-            self = [date retain];
-        } else {
-            self = [self initWithMonthDayYearString:[NSString 
stringWithFormat:@"%@-15-%@", monthString, yearString]];
-        }
+        month = i == NSNotFound ? 1 : i + 1;
     }
-    return self;
+    NSDateComponents *components = [[[NSDateComponents alloc] init] 
autorelease];
+    [components setYear:year];
+    [components setMonth:month];
+    [components setDay:15];
+    [components setHour:12];
+    [components setMonth:0];
+    [components setSecond:0];
+    return [calendar dateFromComponents:components];
 }
 
 - (NSString *)dateDescription{

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to