Revision: 28974
          http://sourceforge.net/p/bibdesk/svn/28974
Author:   hofman
Date:     2024-11-12 16:22:42 +0000 (Tue, 12 Nov 2024)
Log Message:
-----------
reduce selection range by all deleted characters

Modified Paths:
--------------
    trunk/bibdesk/BDSKCitationFormatter.m
    trunk/bibdesk/BDSKCiteKeyFormatter.m
    trunk/bibdesk/BDSKFieldNameFormatter.m
    trunk/bibdesk/BDSKTypeNameFormatter.m
    trunk/bibdesk/NSString_BDSKExtensions.h
    trunk/bibdesk/NSString_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKCitationFormatter.m
===================================================================
--- trunk/bibdesk/BDSKCitationFormatter.m       2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/BDSKCitationFormatter.m       2024-11-12 16:22:42 UTC (rev 
28974)
@@ -134,21 +134,21 @@
 
 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
originalString:(NSString *)origString 
originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error 
{
     NSString *partialString = *partialStringPtr;
-    NSRange r = [partialString rangeOfCharacterFromSet:invalidSet];
-    if (r.location != NSNotFound) {
-        NSMutableString *new = [partialString mutableCopy];
-        [new replaceOccurrencesOfCharactersInSet:invalidSet withString:@""];
-        if ([new length]) {
+    NSIndexSet *indexes = [partialString indexesOfCharactersInSet:invalidSet];
+    if ([indexes count]) {
+        if ([indexes count] < [partialString length]) {
+            NSMutableString *new = [partialString mutableCopy];
+            [new deleteCharactersAtIndexes:indexes];
             *partialStringPtr = new;
-            if (r.location < proposedSelRangePtr->location)
-                --proposedSelRangePtr->location;
-            if (NSMaxRange(*proposedSelRangePtr) > [new length])
-                *proposedSelRangePtr = NSMakeRange(r.location, 0);
+            if (proposedSelRangePtr->length > 0)
+                proposedSelRangePtr->length -= [indexes 
countOfIndexesInRange:*proposedSelRangePtr];
+            if (proposedSelRangePtr->location > 0)
+                proposedSelRangePtr->location -= [indexes 
countOfIndexesInRange:NSMakeRange(0, proposedSelRangePtr->location)];
         } else {
             *partialStringPtr = origString;
             *proposedSelRangePtr = origSelRange;
         }
-        if(error) *error = [NSString stringWithFormat:NSLocalizedString(@"The 
character \"%@\" is not allowed in a BibTeX cite key.", @"Error description"), 
[partialString substringWithRange:r]];
+        if(error) *error = [NSString stringWithFormat:NSLocalizedString(@"The 
character \"%@\" is not allowed in a BibTeX cite key.", @"Error description"), 
[partialString substringWithRange:NSMakeRange([indexes firstIndex], 1)]];
         return NO;
     }else
         return YES;

Modified: trunk/bibdesk/BDSKCiteKeyFormatter.m
===================================================================
--- trunk/bibdesk/BDSKCiteKeyFormatter.m        2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/BDSKCiteKeyFormatter.m        2024-11-12 16:22:42 UTC (rev 
28974)
@@ -68,24 +68,25 @@
 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
originalString:(NSString *)origString 
originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error 
{
     NSString *partialString = *partialStringPtr;
        NSCharacterSet *invalidSet = [[BDSKTypeManager sharedManager] 
invalidCharactersForField:BDSKCiteKeyString];
-    NSRange r = [partialString rangeOfCharacterFromSet:invalidSet];
-    if ( r.location != NSNotFound) {
-        NSMutableString *new = [partialString mutableCopy];
-        [new replaceOccurrencesOfCharactersInSet:invalidSet withString:@""];
-        if ([new length]) {
+    NSIndexSet *indexes = [partialString indexesOfCharactersInSet:invalidSet];
+    if ([indexes count]) {
+        if ([indexes count] < [partialString length]) {
+            NSMutableString *new = [partialString mutableCopy];
+            [new deleteCharactersAtIndexes:indexes];
             *partialStringPtr = new;
-            if (r.location < proposedSelRangePtr->location)
-                --proposedSelRangePtr->location;
-            if (NSMaxRange(*proposedSelRangePtr) > [new length])
-                *proposedSelRangePtr = NSMakeRange(r.location, 0);
+            if (proposedSelRangePtr->length > 0)
+                proposedSelRangePtr->length -= [indexes 
countOfIndexesInRange:*proposedSelRangePtr];
+            if (proposedSelRangePtr->location > 0)
+                proposedSelRangePtr->location -= [indexes 
countOfIndexesInRange:NSMakeRange(0, proposedSelRangePtr->location)];
         } else {
             *partialStringPtr = origString;
             *proposedSelRangePtr = origSelRange;
         }
-        if(error) *error = [NSString stringWithFormat:NSLocalizedString(@"The 
character \"%@\" is not allowed in a BibTeX cite key.", @"Error description"), 
[partialString substringWithRange:r]];
+        if(error) *error = [NSString stringWithFormat:NSLocalizedString(@"The 
character \"%@\" is not allowed in a BibTeX cite key.", @"Error description"), 
[partialString substringWithRange:NSMakeRange([indexes firstIndex], 1)]];
         return NO;
-    }else
+    } else {
         return YES;
+    }
 }
 
 @end

Modified: trunk/bibdesk/BDSKFieldNameFormatter.m
===================================================================
--- trunk/bibdesk/BDSKFieldNameFormatter.m      2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/BDSKFieldNameFormatter.m      2024-11-12 16:22:42 UTC (rev 
28974)
@@ -110,21 +110,21 @@
         }
     }
     NSCharacterSet *invalidSet = [[BDSKTypeManager sharedManager] 
invalidFieldNameCharacterSet];
-    NSRange r = [partialString rangeOfCharacterFromSet:invalidSet];
-    if (r.location != NSNotFound) {
-        if (error) *error = NSLocalizedString(@"The field name contains an 
invalid character", @"field name warning");
-        NSMutableString *new = [partialString mutableCopy];
-        [new replaceOccurrencesOfCharactersInSet:invalidSet withString:@""];
-        if ([new length]) {
-            if (r.location < proposedSelRangePtr->location)
-                --proposedSelRangePtr->location;
+    NSIndexSet *indexes = [partialString indexesOfCharactersInSet:invalidSet];
+    if ([indexes count]) {
+        if ([indexes count] < [partialString length]) {
+            NSMutableString *new = [partialString mutableCopy];
+            [new deleteCharactersAtIndexes:indexes];
             *partialStringPtr = new;
-            if (NSMaxRange(*proposedSelRangePtr) > [new length])
-                *proposedSelRangePtr = NSMakeRange(r.location, 0);
+            if (proposedSelRangePtr->length > 0)
+                proposedSelRangePtr->length -= [indexes 
countOfIndexesInRange:*proposedSelRangePtr];
+            if (proposedSelRangePtr->location > 0)
+                proposedSelRangePtr->location -= [indexes 
countOfIndexesInRange:NSMakeRange(0, proposedSelRangePtr->location)];
         } else {
             *partialStringPtr = origString;
             *proposedSelRangePtr = origSelRange;
         }
+        if (error) *error = NSLocalizedString(@"The field name contains an 
invalid character", @"field name warning");
                return NO;
     } else if ([partialString length] && [[NSCharacterSet 
decimalDigitCharacterSet] characterIsMember:[partialString 
characterAtIndex:0]]) {
         *partialStringPtr = nil;

Modified: trunk/bibdesk/BDSKTypeNameFormatter.m
===================================================================
--- trunk/bibdesk/BDSKTypeNameFormatter.m       2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/BDSKTypeNameFormatter.m       2024-11-12 16:22:42 UTC (rev 
28974)
@@ -80,16 +80,16 @@
 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
originalString:(NSString *)origString 
originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error 
{
     NSString *partialString = *partialStringPtr;
     NSCharacterSet *invalidSet = [[BDSKTypeManager sharedManager] 
invalidFieldNameCharacterSet];
-    NSRange r = [partialString rangeOfCharacterFromSet:invalidSet];
-    if ( r.location != NSNotFound) {
-        NSMutableString *new = [partialString mutableCopy];
-        [new replaceOccurrencesOfCharactersInSet:invalidSet withString:@""];
-        if ([new length]) {
+    NSIndexSet *indexes = [partialString indexesOfCharactersInSet:invalidSet];
+    if ([indexes count]) {
+        if ([indexes count] < [partialString length]) {
+            NSMutableString *new = [partialString mutableCopy];
+            [new deleteCharactersAtIndexes:indexes];
             *partialStringPtr = new;
-            if (r.location < proposedSelRangePtr->location)
-                --proposedSelRangePtr->location;
-            if (NSMaxRange(*proposedSelRangePtr) > [new length])
-                *proposedSelRangePtr = NSMakeRange(r.location, 0);
+            if (proposedSelRangePtr->length > 0)
+                proposedSelRangePtr->length -= [indexes 
countOfIndexesInRange:*proposedSelRangePtr];
+            if (proposedSelRangePtr->location > 0)
+                proposedSelRangePtr->location -= [indexes 
countOfIndexesInRange:NSMakeRange(0, proposedSelRangePtr->location)];
         } else {
             *partialStringPtr = origString;
             *proposedSelRangePtr = origSelRange;
@@ -97,8 +97,7 @@
         if (error) *error = NSLocalizedString(@"The type name contains an 
invalid character", @"field name warning");
         return NO;
     }
-    r = [partialString rangeOfCharacterFromSet:[NSCharacterSet 
uppercaseLetterCharacterSet]];
-    if ( r.location != NSNotFound) {
+    if ( [partialString rangeOfCharacterFromSet:[NSCharacterSet 
uppercaseLetterCharacterSet]].location != NSNotFound) {
         // this is a BibDesk requirement, since we expect type names to be 
lowercase
         *partialStringPtr = [partialString entryType];
         return NO;

Modified: trunk/bibdesk/NSString_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.h     2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/NSString_BDSKExtensions.h     2024-11-12 16:22:42 UTC (rev 
28974)
@@ -401,6 +401,8 @@
 
 @property (nonatomic, readonly) NSArray *componentsSeparatedByFieldSeparators;
 
+- (NSIndexSet *)indexesOfCharactersInSet:(NSCharacterSet *)charSet;
+
 /*!
 @method     containsWord:
 @abstract   Determine whether a string contains the argument aWord; if it 
contains aWord as a substring, it then tests to see if it is bounded by null, 
punctuation, or whitespace.
@@ -544,5 +546,6 @@
 - (void)appendStrings:(NSString *)first, ...;
 - (void)backslashEscapeCharactersInSet:(NSCharacterSet *)charSet;
 - (void)backslashUnescapeCharactersInSet:(NSCharacterSet *)charSet;
+- (void)deleteCharactersAtIndexes:(NSIndexSet *)indexes;
 
 @end

Modified: trunk/bibdesk/NSString_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSString_BDSKExtensions.m     2024-11-12 15:12:55 UTC (rev 
28973)
+++ trunk/bibdesk/NSString_BDSKExtensions.m     2024-11-12 16:22:42 UTC (rev 
28974)
@@ -1115,6 +1115,23 @@
     return [self componentsSeparatedByCharactersInSet:[NSCharacterSet 
commaCharacterSet] trimWhitespace:YES];
 }
 
+- (NSIndexSet *)indexesOfCharactersInSet:(NSCharacterSet *)charSet
+{
+    NSRange r = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch];
+    if (r.location == NSNotFound)
+        return nil;
+    NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
+    NSUInteger length = [self length];
+    do {
+        [indexes addIndex:r.location];
+        if (r.location + 1 < length)
+            r = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch 
range:NSMakeRange(r.location + 1, length - r.location - 1)];
+        else
+            break;
+    } while (r.location != NSNotFound);
+    return indexes;
+}
+
 - (NSString *)stringByCollapsingCharactersInSet:(NSCharacterSet *)charSet;
 {
     return 
CFBridgingRelease(BDStringCreateByCollapsingCharactersInSet(CFAllocatorGetDefault(),
 (__bridge CFStringRef)self, (__bridge CFCharacterSetRef)charSet));
@@ -2106,4 +2123,10 @@
     }
 }
 
+- (void)deleteCharactersAtIndexes:(NSIndexSet *)indexes {
+    [indexes enumerateRangesWithOptions:NSEnumerationReverse 
usingBlock:^(NSRange r, BOOL *stop){
+        [self deleteCharactersInRange:r];
+    }];
+}
+
 @end

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to