Revision: 22103
          http://sourceforge.net/p/bibdesk/svn/22103
Author:   hofman
Date:     2018-03-07 15:01:27 +0000 (Wed, 07 Mar 2018)
Log Message:
-----------
Find closing bracket for bibtex in web parser so we don't overfill the buffer 
with junk

Modified Paths:
--------------
    trunk/bibdesk/BDSKBibTeXWebParser.m
    trunk/bibdesk/NSString_BDSKExtensions.h
    trunk/bibdesk/NSString_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKBibTeXWebParser.m
===================================================================
--- trunk/bibdesk/BDSKBibTeXWebParser.m 2018-03-07 07:30:22 UTC (rev 22102)
+++ trunk/bibdesk/BDSKBibTeXWebParser.m 2018-03-07 15:01:27 UTC (rev 22103)
@@ -67,14 +67,21 @@
     NSUInteger i, iMax = [matches count];
     
     for (i = 0; i < iMax; i++) {
-        NSUInteger start = [[matches objectAtIndex:i] range].location;
+        NSRange range = [[matches objectAtIndex:i] range];
         NSUInteger end = i + 1 == iMax ? [text length] : [[matches 
objectAtIndex:i + 1] range].location;
-        NSString *string = [text substringWithRange:NSMakeRange(start, end - 
start)];
-        NSError *error = nil;
+        unichar leftBracket = [text characterAtIndex:NSMaxRange(range) - 1];
+        unichar rightBracket = leftBracket == '(' ? ')' : '}';
         
-        if ([BDSKBibTeXParser canParseString:string]) {
-            NSArray *bibtexItems = [BDSKBibTeXParser itemsFromString:string 
owner:[self delegate] error:&error];
-            [items addObjectsFromArray:bibtexItems];
+        end = [text indexOfRightBracket:rightBracket 
matchingLeftBracket:leftBracket inRange:NSMakeRange(NSMaxRange(range) - 1, end 
- NSMaxRange(range) - 1)];
+
+        if (end != NSNotFound) {
+            NSString *string = [text 
substringWithRange:NSMakeRange(range.location, end + 1 - range.location)];
+            NSError *error = nil;
+            
+            if ([BDSKBibTeXParser canParseString:string]) {
+                NSArray *bibtexItems = [BDSKBibTeXParser 
itemsFromString:string owner:[self delegate] error:&error];
+                [items addObjectsFromArray:bibtexItems];
+            }
         }
        }
        

Modified: trunk/bibdesk/NSString_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.h     2018-03-07 07:30:22 UTC (rev 
22102)
+++ trunk/bibdesk/NSString_BDSKExtensions.h     2018-03-07 15:01:27 UTC (rev 
22103)
@@ -236,6 +236,18 @@
 - (NSString *)localizedFieldName;
 
 /*!
+ @method     indexOfRightBracket:matchingLeftBracket:inRange:
+ @abstract   Counts matching brackets from left-to-right, in order to find a 
match for a left bracket.
+ @discussion Raises an exception if the character at <tt>startLoc</tt> is not 
a bracket of the given type, and escaped braces are not (yet?) considered.
+ An inline buffer is used for speed in accessing each character.
+ @param      rightBracket The right bracket character.
+ @param      leftBracket The left bracket character.
+ @param      range The range to search for matching brackets, the first 
character should be the left bracket.
+ @result     The index of the matching right bracket character.
+ */
+- (NSUInteger)indexOfRightBracket:(unichar)rightBracket 
matchingLeftBracket:(unichar)leftBracket inRange:(NSRange)range;
+
+/*!
 @method     indexOfRightBraceMatchingLeftBraceInRange:
 @abstract   Counts curly braces from left-to-right, in order to find a match 
for a left brace <tt>{</tt>.
 @discussion Raises an exception if the character at <tt>startLoc</tt> is not a 
brace, and escaped braces are not (yet?) considered.
@@ -246,7 +258,7 @@
 - (NSUInteger)indexOfRightBraceMatchingLeftBraceInRange:(NSRange)range;
 
 - (NSUInteger)indexOfRightBraceMatchingLeftBraceAtIndex:(NSUInteger)startLoc;
-    
+
     /*!
     @method     isStringTeXQuotingBalancedWithBraces:connected:
     @abstract   Invoces isStringTeXQuotingBalancedWithBraces:connected:range: 
with the full range of the receiver. 

Modified: trunk/bibdesk/NSString_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.m     2018-03-07 07:30:22 UTC (rev 
22102)
+++ trunk/bibdesk/NSString_BDSKExtensions.m     2018-03-07 15:01:27 UTC (rev 
22103)
@@ -413,13 +413,8 @@
     return self;
 }
 
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceAtIndex:(NSUInteger)startLoc
+- (NSUInteger)indexOfRightBracket:(unichar)rightBracket 
matchingLeftBracket:(unichar)leftBracket inRange:(NSRange)range
 {
-    return [self 
indexOfRightBraceMatchingLeftBraceInRange:NSMakeRange(startLoc, [self length] - 
startLoc)];
-}
-
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceInRange:(NSRange)range
-{
     
     CFStringInlineBuffer inlineBuffer;
     CFIndex length = CFStringGetLength((CFStringRef)self);
@@ -432,7 +427,7 @@
     UniChar ch;
     NSInteger nesting = 0;
     
-    if(CFStringGetCharacterFromInlineBuffer(&inlineBuffer, startLoc) != '{')
+    if(CFStringGetCharacterFromInlineBuffer(&inlineBuffer, startLoc) != 
leftBracket)
         [NSException raise:NSInternalInconsistencyException format:@"character 
at index %ld is not a brace", (long)startLoc];
     
     // we don't consider escaped braces yet
@@ -440,9 +435,9 @@
         ch = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, cnt);
         if(ch == '\\')
             cnt++;
-        else if(ch == '{')
+        else if(ch == leftBracket)
             nesting++;
-        else if(ch == '}')
+        else if(ch == rightBracket)
             nesting--;
         if(nesting == 0){
             //NSLog(@"match found at index %ld", (long)cnt);
@@ -454,6 +449,15 @@
     return matchFound ? (NSUInteger)cnt : NSNotFound;    
 }
 
+- (NSUInteger)indexOfRightBraceMatchingLeftBraceInRange:(NSRange)range {
+    return [self indexOfRightBracket:'}' matchingLeftBracket:'{' 
inRange:range];
+}
+
+- (NSUInteger)indexOfRightBraceMatchingLeftBraceAtIndex:(NSUInteger)startLoc
+{
+    return [self 
indexOfRightBraceMatchingLeftBraceInRange:NSMakeRange(startLoc, [self length] - 
startLoc)];
+}
+
 - (BOOL)isStringTeXQuotingBalancedWithBraces:(BOOL)braces 
connected:(BOOL)connected{
        return [self isStringTeXQuotingBalancedWithBraces:braces 
connected:connected range:NSMakeRange(0,[self length])];
 }

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to