From: znek Date: Thu, 14 Oct 2004 20:13:58 +0000 (+0000) Subject: refactoring X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0edc2231f3ab5dcebf7938aba70cd813b9c8dd2e;p=scalable-opengroupware.org refactoring git-svn-id: http://svn.opengroupware.org/SOGo/trunk@398 d1b88da0-ebda-0310-925b-ed51d893ca5b --- diff --git a/SOGoLogic/ChangeLog b/SOGoLogic/ChangeLog index 815fa5be..bbb5df28 100644 --- a/SOGoLogic/ChangeLog +++ b/SOGoLogic/ChangeLog @@ -1,5 +1,8 @@ 2004-10-14 Marcus Mueller + * NSString+iCal.[hm]: uses new category from NGExtensions to do the + escaping (v0.9.18) + * NSString+iCal.[hm]: new (private) method to strip off any unsafe characters from strings and bugfix for escaping (v0.9.17) diff --git a/SOGoLogic/NSString+iCal.m b/SOGoLogic/NSString+iCal.m index 42fe41a7..c902b110 100644 --- a/SOGoLogic/NSString+iCal.m +++ b/SOGoLogic/NSString+iCal.m @@ -28,6 +28,41 @@ - (NSString *)iCalCleanString; @end +#include "common.h" + +@interface SOGoICalStringEscaper : NSObject +{ +} ++ (id)sharedEscaper; +@end + +@implementation SOGoICalStringEscaper ++ (id)sharedEscaper { + static id sharedInstance = nil; + if(!sharedInstance) { + sharedInstance = [[self alloc] init]; + } + return sharedInstance; +} + +- (NSString *)stringByEscapingString:(NSString *)_s { + unichar c; + + if(!_s || [_s length] == 0) + return nil; + + c = [_s characterAtIndex:0]; + if(c == '\n') { + return @"\\n"; + } + else if(c == '\r') { + return nil; /* effectively remove char */ + } + return [NSString stringWithFormat:@"\\%@", _s]; +} + +@end + @implementation NSString (SOGoiCal) #if 0 @@ -53,50 +88,14 @@ /* strip off any characters from string which are not allowed in iCal */ - (NSString *)iCalCleanString { static NSCharacterSet *replaceSet = nil; - NSRange r, er; - NSMutableString *safeString; - BOOL needsReplacing; - unsigned length; if(replaceSet == nil) { replaceSet = [NSCharacterSet characterSetWithCharactersInString:@"\r"]; [replaceSet retain]; } - length = [self length]; - r = NSMakeRange(0, length); - er = [self rangeOfCharacterFromSet:replaceSet options:0 range:r]; - needsReplacing = er.length > 0 ? YES : NO; - if(!needsReplacing) { - return self; /* cheap */ - } - - safeString = [NSMutableString stringWithCapacity:length]; - if(needsReplacing) { - NSRange todoRange; - /* - r == previous range, upto er.location - er == escape range - todoRange == what we still need to scan - */ - length = r.length; - do { - r.length = er.location - r.location; - if(r.length > 0) - [safeString appendString:[self substringWithRange:r]]; - r.location = NSMaxRange(er); - todoRange.location = r.location; - todoRange.length = length - r.location; - er = [self rangeOfCharacterFromSet:replaceSet - options:0 - range:todoRange]; - } - while(er.length > 0); - if(todoRange.length > 0) { - [safeString appendString:[self substringWithRange:todoRange]]; - } - } - return safeString; + return [self stringByEscapingCharactersFromSet:replaceSet + usingStringEscaping:[SOGoICalStringEscaper sharedEscaper]]; } - (NSString *)iCalDQUOTESafeString { @@ -121,59 +120,11 @@ /* Escape unsafe characters */ - (NSString *)iCalEscapedStringWithEscapeSet:(NSCharacterSet *)_es { - NSString *cleanString; - NSMutableString *safeString; - NSRange r, er; - BOOL needsEscaping; - unsigned length; - - cleanString = [self iCalCleanString]; - length = [cleanString length]; - r = NSMakeRange(0, length); - er = [cleanString rangeOfCharacterFromSet:_es options:0 range:r]; - needsEscaping = er.length > 0 ? YES : NO; - if(!needsEscaping) { - return cleanString; /* cheap */ - } - /* wild guess */ - safeString = [NSMutableString stringWithCapacity:length + 10]; - if(needsEscaping) { - NSRange todoRange; - /* - r == previous range, upto er.location - er == escape range - todoRange == what we still need to scan - */ - length = r.length; - do { - NSString *s; - - r.length = er.location - r.location; - if(r.length > 0) { - s = [cleanString substringWithRange:r]; - [safeString appendString:s]; - } - [safeString appendString:@"\\"]; - if([cleanString characterAtIndex:er.location] == '\n') { - s = @"n"; - } - else { - s = [cleanString substringWithRange:er]; - } - [safeString appendString:s]; - r.location = NSMaxRange(er); - todoRange.location = r.location; - todoRange.length = length - r.location; - er = [cleanString rangeOfCharacterFromSet:_es - options:0 - range:todoRange]; - } - while(er.length > 0); - if(todoRange.length > 0) { - [safeString appendString:[cleanString substringWithRange:todoRange]]; - } - } - return safeString; + NSString *s; + + s = [self iCalCleanString]; + return [s stringByEscapingCharactersFromSet:_es + usingStringEscaping:[SOGoICalStringEscaper sharedEscaper]]; } @end diff --git a/SOGoLogic/Version b/SOGoLogic/Version index f120816a..d8b4b1b1 100644 --- a/SOGoLogic/Version +++ b/SOGoLogic/Version @@ -1,5 +1,6 @@ # $Id$ -SUBMINOR_VERSION:=17 +SUBMINOR_VERSION:=18 +# v0.9.18 requires NGExtensions v4.3.123 # v0.9.13 requires libFoundation v1.0.62