Commits

Stephen Holt  committed d2a966b

No sense in even scanning the string range if we're not even going to turn it into a link anyway.

  • Participants
  • Parent commits 637e184

Comments (0)

Files changed (1)

File Source/AHHyperlinkScanner.m

 	
 	// main scanning loop
 	while([self _scanString:m_scanString upToCharactersFromSet:skipSet intoRange:&scannedRange fromIndex:&scannedLocation]) {
-		
-		// Check for and filter  enclosures.  We can't add (, [, etc. to the skipSet as they may be in a URI
-		if([enclosureSet characterIsMember:[m_scanString characterAtIndex:scannedRange.location]]){
-			unsigned long encIdx = [enclosureStartArray indexOfObject:[m_scanString substringWithRange:NSMakeRange(scannedRange.location, 1)]];
-			NSRange encRange;
-			if(NSNotFound != encIdx) {
-				encRange = [m_scanString rangeOfString:[enclosureStopArray objectAtIndex:encIdx] options:NSBackwardsSearch range:scannedRange];
-				if(NSNotFound != encRange.location){
-					scannedRange.location++; scannedRange.length -= 2;
-				}
-			}
-		}
-		if(!scannedRange.length) break;
-		
-		// Find balanced enclosure chars
-		NSRange longestEnclosure = [self _longestBalancedEnclosureInRange:scannedRange];
-		while (scannedRange.length > 2 && [endSet characterIsMember:[m_scanString characterAtIndex:(scannedRange.location + scannedRange.length - 1)]]) {
-			if((longestEnclosure.location + longestEnclosure.length) < scannedRange.length){
-				scannedRange.length--;
-			}else break;
-		}
-		
-        // if we have a valid URL then save the scanned string, and make a SHMarkedHyperlink out of it.
-        // this way, we can preserve things like the matched string (to be converted to a NSURL),
-        // parent string, its validation status (valid, file, degenerate, etc), and its range in the parent string
-		AH_URI_VERIFICATION_STATUS	 validStatus;
-		NSString					*_scanString = nil;
-		unsigned long				 schemeLength = 0;
-		if(4 < scannedRange.length) _scanString = [m_scanString substringWithRange:scannedRange];
-        if((4 < scannedRange.length) && [[self class] isStringValidURI:_scanString usingStrict:m_strictChecking fromIndex:&m_scanLocation withStatus:&validStatus schemeLength:&schemeLength]){
-            AHMarkedHyperlink	*markedLink;
-			BOOL				 makeLink = TRUE;
-            //insert typical specifiers if the URL is degenerate
-            switch(validStatus){
-                case AH_URL_DEGENERATE:
-                {
-                    NSString *scheme = DEFAULT_URL_SCHEME;
-					unsigned long i = 0;
-					
-                    NSRange  firstComponent;
-					[self		  _scanString:_scanString
-				   upToCharactersFromSet:hostnameComponentSeparatorSet
-							   intoRange:&firstComponent
-							   fromIndex:&i];
-					
-                    if(NSNotFound != firstComponent.location) {
-                    	NSString *hostnameScheme = [m_urlSchemes objectForKey:[_scanString substringWithRange:firstComponent]];
-                    	if(hostnameScheme) scheme = hostnameScheme;
+        if (4 < scannedRange.length) {
+            // Check for and filter  enclosures.  We can't add (, [, etc. to the skipSet as they may be in a URI
+            if([enclosureSet characterIsMember:[m_scanString characterAtIndex:scannedRange.location]]){
+                unsigned long encIdx = [enclosureStartArray indexOfObject:[m_scanString substringWithRange:NSMakeRange(scannedRange.location, 1)]];
+                NSRange encRange;
+                if(NSNotFound != encIdx) {
+                    encRange = [m_scanString rangeOfString:[enclosureStopArray objectAtIndex:encIdx] options:NSBackwardsSearch range:scannedRange];
+                    if(NSNotFound != encRange.location){
+                        scannedRange.location++; scannedRange.length -= 2;
                     }
-					
-                    _scanString = [scheme stringByAppendingString:_scanString];
-					
-                    break;
                 }
-					
-                case AH_MAILTO_DEGENERATE:
-					_scanString = [@"mailto:" stringByAppendingString:_scanString];
-                    break;
-				case AH_URL_TENTATIVE:
-				{
-					NSString *scheme = [_scanString substringToIndex:schemeLength];
-					NSArray *apps = (NSArray *)LSCopyAllHandlersForURLScheme((CFStringRef)scheme);
-
-					if(!apps.count)
-						makeLink = FALSE;
-					[apps release];
-					break;
-				}
-                default:
-                    break;
+            }
+            if(!scannedRange.length) break;
+            
+            // Find balanced enclosure chars
+            NSRange longestEnclosure = [self _longestBalancedEnclosureInRange:scannedRange];
+            while (scannedRange.length > 2 && [endSet characterIsMember:[m_scanString characterAtIndex:(scannedRange.location + scannedRange.length - 1)]]) {
+                if((longestEnclosure.location + longestEnclosure.length) < scannedRange.length){
+                    scannedRange.length--;
+                }else break;
             }
             
-			if(makeLink){
-				//make a marked link
-				markedLink = [[AHMarkedHyperlink alloc] initWithString:_scanString
-												  withValidationStatus:validStatus
-														  parentString:m_scanString
-															  andRange:scannedRange];
-				return [markedLink autorelease];
-			}
+            // if we have a valid URL then save the scanned string, and make a SHMarkedHyperlink out of it.
+            // this way, we can preserve things like the matched string (to be converted to a NSURL),
+            // parent string, its validation status (valid, file, degenerate, etc), and its range in the parent string
+            AH_URI_VERIFICATION_STATUS	 validStatus;
+            NSString					*_scanString = nil;
+            unsigned long				 schemeLength = 0;
+            if(4 < scannedRange.length) _scanString = [m_scanString substringWithRange:scannedRange];
+            if((4 < scannedRange.length) && [[self class] isStringValidURI:_scanString usingStrict:m_strictChecking fromIndex:&m_scanLocation withStatus:&validStatus schemeLength:&schemeLength]){
+                AHMarkedHyperlink	*markedLink;
+                BOOL				 makeLink = TRUE;
+                //insert typical specifiers if the URL is degenerate
+                switch(validStatus){
+                    case AH_URL_DEGENERATE:
+                    {
+                        NSString *scheme = DEFAULT_URL_SCHEME;
+                        unsigned long i = 0;
+                        
+                        NSRange  firstComponent;
+                        [self		  _scanString:_scanString
+                       upToCharactersFromSet:hostnameComponentSeparatorSet
+                                   intoRange:&firstComponent
+                                   fromIndex:&i];
+                        
+                        if(NSNotFound != firstComponent.location) {
+                            NSString *hostnameScheme = [m_urlSchemes objectForKey:[_scanString substringWithRange:firstComponent]];
+                            if(hostnameScheme) scheme = hostnameScheme;
+                        }
+                        
+                        _scanString = [scheme stringByAppendingString:_scanString];
+                        
+                        break;
+                    }
+                        
+                    case AH_MAILTO_DEGENERATE:
+                        _scanString = [@"mailto:" stringByAppendingString:_scanString];
+                        break;
+                    case AH_URL_TENTATIVE:
+                    {
+                        NSString *scheme = [_scanString substringToIndex:schemeLength];
+                        NSArray *apps = (NSArray *)LSCopyAllHandlersForURLScheme((CFStringRef)scheme);
+
+                        if(!apps.count)
+                            makeLink = FALSE;
+                        [apps release];
+                        break;
+                    }
+                    default:
+                        break;
+                }
+                
+                if(makeLink){
+                    //make a marked link
+                    markedLink = [[AHMarkedHyperlink alloc] initWithString:_scanString
+                                                      withValidationStatus:validStatus
+                                                              parentString:m_scanString
+                                                                  andRange:scannedRange];
+                    return [markedLink autorelease];
+                }
+            }
+            
+            //step location after scanning a string
+            NSRange startRange = [m_scanString rangeOfCharacterFromSet:puncSet options:NSLiteralSearch range:scannedRange];
+            if (startRange.location != NSNotFound)
+                m_scanLocation = startRange.location + startRange.length;
+            else
+                m_scanLocation += scannedRange.length;
+            
+            scannedLocation = m_scanLocation;
         }
-		
-		//step location after scanning a string
-		NSRange startRange = [m_scanString rangeOfCharacterFromSet:puncSet options:NSLiteralSearch range:scannedRange];
-		if (startRange.location != NSNotFound)
-			m_scanLocation = startRange.location + startRange.length;
-		else
-			m_scanLocation += scannedRange.length;
-		
-		scannedLocation = m_scanLocation;
     }
 	
     // if we're here, then NSScanner hit the end of the string