Revision: 22104
          http://sourceforge.net/p/bibdesk/svn/22104
Author:   hofman
Date:     2018-03-07 15:17:00 +0000 (Wed, 07 Mar 2018)
Log Message:
-----------
Make bracket matching method generic, get bracket character from the first 
character

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

Modified: trunk/bibdesk/BDSKBibTeXWebParser.m
===================================================================
--- trunk/bibdesk/BDSKBibTeXWebParser.m 2018-03-07 15:01:27 UTC (rev 22103)
+++ trunk/bibdesk/BDSKBibTeXWebParser.m 2018-03-07 15:17:00 UTC (rev 22104)
@@ -69,10 +69,8 @@
     for (i = 0; i < iMax; i++) {
         NSRange range = [[matches objectAtIndex:i] range];
         NSUInteger end = i + 1 == iMax ? [text length] : [[matches 
objectAtIndex:i + 1] range].location;
-        unichar leftBracket = [text characterAtIndex:NSMaxRange(range) - 1];
-        unichar rightBracket = leftBracket == '(' ? ')' : '}';
         
-        end = [text indexOfRightBracket:rightBracket 
matchingLeftBracket:leftBracket inRange:NSMakeRange(NSMaxRange(range) - 1, end 
- NSMaxRange(range) - 1)];
+        end = [text 
indexOfRightBracketMatchingLeftBracketInRange:NSMakeRange(NSMaxRange(range) - 
1, end - NSMaxRange(range) - 1)];
 
         if (end != NSNotFound) {
             NSString *string = [text 
substringWithRange:NSMakeRange(range.location, end + 1 - range.location)];

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2018-03-07 15:01:27 UTC (rev 22103)
+++ trunk/bibdesk/BibItem.m     2018-03-07 15:17:00 UTC (rev 22104)
@@ -2645,7 +2645,7 @@
         value = [value substringWithRange:NSMakeRange(5, [value length] - 6)];
     } else if([value hasPrefix:@"\\href{"]){
         // may also take the form 
\href{http://arXiv.org/abs/hep-th/0304033}{arXiv:hep-th/0304033}
-        NSUInteger loc = [value indexOfRightBraceMatchingLeftBraceAtIndex:5];
+        NSUInteger loc = [value 
indexOfRightBracketMatchingLeftBracketAtIndex:5];
         if (NSNotFound != loc)
             value = [value substringWithRange:NSMakeRange(6, loc - 6)];
     }

Modified: trunk/bibdesk/NSAttributedString_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSAttributedString_BDSKExtensions.m   2018-03-07 15:01:27 UTC 
(rev 22103)
+++ trunk/bibdesk/NSAttributedString_BDSKExtensions.m   2018-03-07 15:17:00 UTC 
(rev 22104)
@@ -95,7 +95,7 @@
         if (0 != newTrait) {
             
             // remember, we deleted our command, but not the brace
-            if([mutableString characterAtIndex:startLoc] == '{' && (endLoc = 
[mutableString indexOfRightBraceMatchingLeftBraceInRange:searchRange]) != 
NSNotFound){
+            if([mutableString characterAtIndex:startLoc] == '{' && (endLoc = 
[mutableString indexOfRightBracketMatchingLeftBracketInRange:searchRange]) != 
NSNotFound){
                 
                 // have to delete the braces as we go along, or else ranges 
will be hosed after deleting at the end
                 [mutableString deleteCharactersInRange:NSMakeRange(startLoc, 
1)];

Modified: trunk/bibdesk/NSString_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.h     2018-03-07 15:01:27 UTC (rev 
22103)
+++ trunk/bibdesk/NSString_BDSKExtensions.h     2018-03-07 15:17:00 UTC (rev 
22104)
@@ -236,28 +236,16 @@
 - (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.
+@method     indexOfRightBracketMatchingLeftBracketInRange:
+@abstract   Counts 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, and escaped brackets are not (yet?) considered.
 An inline buffer is used for speed in accessing each character.
-@param      range The range to search for matching braces, the first character 
should be the left brace.
-@result     The index of the matching brace character.
+@param      range The range to search for matching brackets, the first 
character should be the left bracket.
+@result     The index of the matching bracket character.
 */
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceInRange:(NSRange)range;
+- (NSUInteger)indexOfRightBracketMatchingLeftBracketInRange:(NSRange)range;
 
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceAtIndex:(NSUInteger)startLoc;
+- 
(NSUInteger)indexOfRightBracketMatchingLeftBracketAtIndex:(NSUInteger)startLoc;
 
     /*!
     @method     isStringTeXQuotingBalancedWithBraces:connected:

Modified: trunk/bibdesk/NSString_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.m     2018-03-07 15:01:27 UTC (rev 
22103)
+++ trunk/bibdesk/NSString_BDSKExtensions.m     2018-03-07 15:17:00 UTC (rev 
22104)
@@ -413,7 +413,7 @@
     return self;
 }
 
-- (NSUInteger)indexOfRightBracket:(unichar)rightBracket 
matchingLeftBracket:(unichar)leftBracket inRange:(NSRange)range
+- (NSUInteger)indexOfRightBracketMatchingLeftBracketInRange:(NSRange)range
 {
     
     CFStringInlineBuffer inlineBuffer;
@@ -426,9 +426,17 @@
     CFStringInitInlineBuffer((CFStringRef)self, &inlineBuffer, CFRangeMake(0, 
length));
     UniChar ch;
     NSInteger nesting = 0;
+    UniChar leftBracket = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, 
startLoc);
+    UniChar rightBracket = 0;
     
-    if(CFStringGetCharacterFromInlineBuffer(&inlineBuffer, startLoc) != 
leftBracket)
-        [NSException raise:NSInternalInconsistencyException format:@"character 
at index %ld is not a brace", (long)startLoc];
+    switch(leftBracket){
+        case '{': rightBracket = '}'; break;
+        case '(': rightBracket = ')'; break;
+        case '[': rightBracket = ']'; break;
+        default:
+            [NSException raise:NSInternalInconsistencyException 
format:@"character %C at index %ld is not a bracket", leftBracket, 
(long)startLoc];
+            break;
+    }
     
     // we don't consider escaped braces yet
     for(cnt = startLoc; cnt < endLoc; cnt++){
@@ -449,13 +457,9 @@
     return matchFound ? (NSUInteger)cnt : NSNotFound;    
 }
 
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceInRange:(NSRange)range {
-    return [self indexOfRightBracket:'}' matchingLeftBracket:'{' 
inRange:range];
-}
-
-- (NSUInteger)indexOfRightBraceMatchingLeftBraceAtIndex:(NSUInteger)startLoc
+- 
(NSUInteger)indexOfRightBracketMatchingLeftBracketAtIndex:(NSUInteger)startLoc
 {
-    return [self 
indexOfRightBraceMatchingLeftBraceInRange:NSMakeRange(startLoc, [self length] - 
startLoc)];
+    return [self 
indexOfRightBracketMatchingLeftBracketInRange:NSMakeRange(startLoc, [self 
length] - startLoc)];
 }
 
 - (BOOL)isStringTeXQuotingBalancedWithBraces:(BOOL)braces 
connected:(BOOL)connected{

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