From 7c0c9e0ef9fe5991d88c88fd1c0475449fb4cf75 Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Tue, 15 May 2007 20:52:01 +0000 Subject: [PATCH] Monotone-Parent: b5675f4c42d29fc479dac10dbd4e9d6269085d3d Monotone-Revision: 0e5da06a5c5166a41629d40ff43cf8d9f3b4335c Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2007-05-15T20:52:01 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 6 +++ SoObjects/SOGo/NSString+Utilities.h | 2 + SoObjects/SOGo/NSString+Utilities.m | 70 ++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 9c1229d3a..ed1b6bd60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-05-15 Wolfgang Sourdeau + + * SoObjects/SOGo/NSString+Utilities.m ([NSString + -stringByDetectingURLs]): this new method replaces passive URLS + with active ones for HTML resolution of the text. + 2007-05-14 Wolfgang Sourdeau * SoObjects/Mailer/SOGoDraftObject.m ([NSString diff --git a/SoObjects/SOGo/NSString+Utilities.h b/SoObjects/SOGo/NSString+Utilities.h index ff120c2f7..01728e9e3 100644 --- a/SoObjects/SOGo/NSString+Utilities.h +++ b/SoObjects/SOGo/NSString+Utilities.h @@ -38,6 +38,8 @@ - (NSString *) davMethodToObjC; +- (NSString *) stringByDetectingURLs; + #ifndef GNUSTEP_BASE_LIBRARY - (BOOL) boolValue; #endif diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m index 66ddda632..4d8acf863 100644 --- a/SoObjects/SOGo/NSString+Utilities.m +++ b/SoObjects/SOGo/NSString+Utilities.m @@ -21,11 +21,15 @@ */ #import +#import #import #import "NSString+Utilities.h" #import "NSDictionary+URL.h" +static NSMutableCharacterSet *urlNonEndingChars = nil; +static NSMutableCharacterSet *urlAfterEndingChars = nil; + @implementation NSString (SOGoURLExtension) - (NSString *) composeURLWithAction: (NSString *) action @@ -101,7 +105,71 @@ return newName; } -#ifndef GNUSTEP_BASE_LIBRARY +- (NSRange) _rangeOfURLInRange: (NSRange) refRange +{ + int start, length; + NSRange endRange; + + if (!urlNonEndingChars) + { + urlNonEndingChars = [NSMutableCharacterSet new]; + [urlNonEndingChars addCharactersInString: @",.:;\t \r\n"]; + } + if (!urlAfterEndingChars) + { + urlAfterEndingChars = [NSMutableCharacterSet new]; + [urlAfterEndingChars addCharactersInString: @"\t \r\n"]; + } + + start = refRange.location; + while (start > -1 + && [self characterAtIndex: start] != ' ') + start--; + start++; + length = [self length] - start; + endRange = NSMakeRange (start, length); + endRange = [self rangeOfCharacterFromSet: urlAfterEndingChars + options: NSLiteralSearch range: endRange]; + if (endRange.location != NSNotFound) + length = endRange.location; + length -= start; + while + ([urlNonEndingChars characterIsMember: + [self characterAtIndex: (start + length - 1)]]) + length--; + + return NSMakeRange (start, length); +} + +- (NSString *) stringByDetectingURLs +{ + NSMutableString *selfCopy; + NSRange httpRange, currentURL, rest; + NSString *urlText, *newUrlText; + unsigned int length; + + selfCopy = [NSMutableString stringWithString: self]; + + httpRange = [selfCopy rangeOfString: @"://"]; + while (httpRange.location != NSNotFound) + { + currentURL = [selfCopy _rangeOfURLInRange: httpRange]; + urlText = [selfCopy substringFromRange: currentURL]; + newUrlText = [NSString stringWithFormat: @"%@", + urlText, urlText]; + [selfCopy replaceCharactersInRange: currentURL + withString: newUrlText]; + length = [selfCopy length]; + rest.location = currentURL.location + [newUrlText length]; + rest.length = length - rest.location; + httpRange = [selfCopy rangeOfString: @"://" + options: 0 range: rest]; + } + + return selfCopy; +} + +#if LIB_FOUNDATION_LIBRARY - (BOOL) boolValue { return !([self isEqualToString: @"0"]