From: znek Date: Thu, 14 Oct 2004 20:11:56 +0000 (+0000) Subject: generic escaping, minor refactoring X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13317e77452e2da1fdd8a7675b63fc12316a92e9;p=sope generic escaping, minor refactoring git-svn-id: http://svn.opengroupware.org/SOPE/trunk@256 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- diff --git a/sope-core/NGExtensions/ChangeLog b/sope-core/NGExtensions/ChangeLog index 57edda27..751d7f5b 100644 --- a/sope-core/NGExtensions/ChangeLog +++ b/sope-core/NGExtensions/ChangeLog @@ -1,3 +1,14 @@ +2004-10-14 Marcus Mueller + + * v4.3.123 + + * FdExt.subproj/NSString+Escaping.m, NGExtensions/NSString+Escaping.h: + new category and protocol to do generic escaping. The category is + Unicode safe and optimized for performance. + + * FdExt.subproj/NSString+misc.m, NGExtensions/NSString+misc.h: moved + -stringByApplyingCEscaping to new NSString+Escaping. + 2004-10-11 Matthew Joyce * FdExt.subproj/NSCalendarDate+misc.m: fixed -isAfternoon (all dates diff --git a/sope-core/NGExtensions/FdExt.subproj/GNUmakefile b/sope-core/NGExtensions/FdExt.subproj/GNUmakefile index b3930a15..bfc4e162 100644 --- a/sope-core/NGExtensions/FdExt.subproj/GNUmakefile +++ b/sope-core/NGExtensions/FdExt.subproj/GNUmakefile @@ -24,6 +24,7 @@ FdExt_OBJC_FILES = \ NSSet+enumerator.m \ NSString+Ext.m \ NSString+Encoding.m \ + NSString+Escaping.m \ NSString+Formatting.m \ NSString+misc.m \ NSString+HTMLEscaping.m \ diff --git a/sope-core/NGExtensions/FdExt.subproj/NSString+Escaping.m b/sope-core/NGExtensions/FdExt.subproj/NSString+Escaping.m new file mode 100644 index 00000000..0ef980c0 --- /dev/null +++ b/sope-core/NGExtensions/FdExt.subproj/NSString+Escaping.m @@ -0,0 +1,122 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + OGo is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ +// $Id$ + + +#include +#include "common.h" + +@implementation NSString (Escaping) + +- (NSString *)stringByApplyingCEscaping { + // Unicode! + const char *cstr; + char *buffer; + register int pos = 0; + + cstr = [self cString]; + buffer = malloc([self cStringLength] * 2 + 1); + + while (*cstr) { + switch (*cstr) { + case '\n': + buffer[pos] = '\\'; pos++; + buffer[pos] = 'n'; + break; + case '\r': + buffer[pos] = '\\'; pos++; + buffer[pos] = 'r'; + break; + case '\t': + buffer[pos] = '\\'; pos++; + buffer[pos] = 't'; + break; + + default: + buffer[pos] = *cstr; + break; + } + cstr++; + pos++; + } + buffer[pos] = '\0'; + +#if NeXT_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY + { + NSString *s; + + s = buffer ? [NSString stringWithCString:buffer] : nil; + if (buffer) free(buffer); + return s; + } +#else + return [NSString stringWithCStringNoCopy:buffer freeWhenDone:YES]; +#endif +} + +- (NSString *)stringByEscapingCharactersFromSet:(NSCharacterSet *)_escSet + usingStringEscaping:()_esc +{ + NSMutableString *safeString; + unsigned length; + NSRange prevRange, escRange; + BOOL needsEscaping; + + length = [self length]; + prevRange = NSMakeRange(0, length); + escRange = [self rangeOfCharacterFromSet:_escSet options:0 range:prevRange]; + + needsEscaping = escRange.length > 0 ? YES : NO; + if (!needsEscaping) { + return self; /* cheap */ + } + + safeString = [NSMutableString stringWithCapacity:length]; + if (needsEscaping) { + NSRange todoRange; + + length = prevRange.length; + do { + NSString *s; + + prevRange.length = escRange.location - prevRange.location; + if (prevRange.length > 0) + [safeString appendString:[self substringWithRange:prevRange]]; + s = [_esc stringByEscapingString:[self substringWithRange:escRange]]; + if (s) + [safeString appendString:s]; + + prevRange.location = NSMaxRange(escRange); + todoRange.location = prevRange.location; + todoRange.length = length - prevRange.location; + escRange = [self rangeOfCharacterFromSet:_escSet + options:0 + range:todoRange]; + } + while(escRange.length > 0); + if(todoRange.length > 0) { + [safeString appendString:[self substringWithRange:todoRange]]; + } + } + return safeString; +} + +@end diff --git a/sope-core/NGExtensions/FdExt.subproj/NSString+misc.m b/sope-core/NGExtensions/FdExt.subproj/NSString+misc.m index 0954939d..190de2d1 100644 --- a/sope-core/NGExtensions/FdExt.subproj/NSString+misc.m +++ b/sope-core/NGExtensions/FdExt.subproj/NSString+misc.m @@ -49,51 +49,6 @@ @implementation NSString(misc) -- (NSString *)stringByApplyingCEscaping { - // Unicode! - const char *cstr; - char *buffer; - register int pos = 0; - - cstr = [self cString]; - buffer = malloc([self cStringLength] * 2 + 1); - - while (*cstr) { - switch (*cstr) { - case '\n': - buffer[pos] = '\\'; pos++; - buffer[pos] = 'n'; - break; - case '\r': - buffer[pos] = '\\'; pos++; - buffer[pos] = 'r'; - break; - case '\t': - buffer[pos] = '\\'; pos++; - buffer[pos] = 't'; - break; - - default: - buffer[pos] = *cstr; - break; - } - cstr++; - pos++; - } - buffer[pos] = '\0'; - -#if NeXT_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY - { - NSString *s; - - s = buffer ? [NSString stringWithCString:buffer] : nil; - if (buffer) free(buffer); - return s; - } -#else - return [NSString stringWithCStringNoCopy:buffer freeWhenDone:YES]; -#endif -} - (NSSet *)bindingVariables { unsigned len, pos = 0; diff --git a/sope-core/NGExtensions/GNUmakefile b/sope-core/NGExtensions/GNUmakefile index ace56416..e1c476bc 100644 --- a/sope-core/NGExtensions/GNUmakefile +++ b/sope-core/NGExtensions/GNUmakefile @@ -103,6 +103,7 @@ FdExt_HEADER_FILES = \ NSString+German.h \ NSString+Formatting.h \ NSString+Encoding.h \ + NSString+Escaping.h \ NSString+misc.h \ NSURL+misc.h \ NGPropertyListParser.h \ diff --git a/sope-core/NGExtensions/NGExtensions.xcode/project.pbxproj b/sope-core/NGExtensions/NGExtensions.xcode/project.pbxproj index e4d41e12..ed18981b 100644 --- a/sope-core/NGExtensions/NGExtensions.xcode/project.pbxproj +++ b/sope-core/NGExtensions/NGExtensions.xcode/project.pbxproj @@ -38,6 +38,40 @@ settings = { }; }; + AD665E37071F00AF00EC5911 = { + fileEncoding = 5; + indentWidth = 2; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.h; + path = "NSString+Escaping.h"; + refType = 4; + sourceTree = ""; + }; + AD665E38071F00AF00EC5911 = { + fileEncoding = 5; + indentWidth = 2; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.objc; + path = "NSString+Escaping.m"; + refType = 4; + sourceTree = ""; + }; + AD665E39071F00AF00EC5911 = { + fileRef = AD665E37071F00AF00EC5911; + isa = PBXBuildFile; + settings = { + ATTRIBUTES = ( + Public, + ); + }; + }; + AD665E3A071F00AF00EC5911 = { + fileRef = AD665E38071F00AF00EC5911; + isa = PBXBuildFile; + settings = { + COMPILER_FLAGS = "-I.."; + }; + }; ADD45B5E06FEF017004BBD65 = { fileEncoding = 5; indentWidth = 2; @@ -736,6 +770,7 @@ ADD65C6506DA3394007161CA = { children = ( ADD65C4B06DA336E007161CA, + AD665E38071F00AF00EC5911, ADD65C4C06DA336E007161CA, ADD65C4D06DA336E007161CA, ADD65C4E06DA336E007161CA, @@ -1133,6 +1168,7 @@ ADD65DE206DA3830007161CA, ADD65DE306DA3830007161CA, ADD65DE406DA3830007161CA, + AD665E39071F00AF00EC5911, ADD65DE506DA3830007161CA, ADD65DE606DA3830007161CA, ADD65DE706DA3830007161CA, @@ -1228,6 +1264,7 @@ ADD65F3306DA397E007161CA, ADD45B6106FEF017004BBD65, AD4BF6EC070314EE006FB665, + AD665E3A071F00AF00EC5911, ); isa = PBXSourcesBuildPhase; runOnlyForDeploymentPostprocessing = 0; @@ -1256,7 +1293,7 @@ ); buildSettings = { DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 4.3.120; + DYLIB_CURRENT_VERSION = 4.3.123; FRAMEWORK_SEARCH_PATHS = "$(LOCAL_LIBRARY_DIR)/Frameworks"; FRAMEWORK_VERSION = A; GCC_PRECOMPILE_PREFIX_HEADER = YES; @@ -1337,6 +1374,8 @@ }; ADD65D6106DA382F007161CA = { children = ( + ADD65D7D06DA382F007161CA, + ADD65D7E06DA382F007161CA, ADD65DF506DA38CA007161CA, ADD65DEF06DA3877007161CA, ADD65DEC06DA3848007161CA, @@ -1352,8 +1391,6 @@ ADD65D7906DA382F007161CA, ADD65D7B06DA382F007161CA, ADD65D7C06DA382F007161CA, - ADD65D7D06DA382F007161CA, - ADD65D7E06DA382F007161CA, ADD65D7F06DA382F007161CA, ADD65D8006DA382F007161CA, ADD65D8106DA382F007161CA, @@ -2645,6 +2682,7 @@ ADD65D9E06DA3830007161CA, ADD65D9F06DA3830007161CA, ADD65DA006DA3830007161CA, + AD665E37071F00AF00EC5911, ADD65DA106DA3830007161CA, ADD65DA206DA3830007161CA, ADD65DA306DA3830007161CA, diff --git a/sope-core/NGExtensions/NGExtensions/NGExtensions.h b/sope-core/NGExtensions/NGExtensions/NGExtensions.h index a52c195d..79ebc183 100644 --- a/sope-core/NGExtensions/NGExtensions/NGExtensions.h +++ b/sope-core/NGExtensions/NGExtensions/NGExtensions.h @@ -62,6 +62,7 @@ #include #include #include +#include #include #include diff --git a/sope-core/NGExtensions/NGExtensions/NSString+Escaping.h b/sope-core/NGExtensions/NGExtensions/NSString+Escaping.h new file mode 100644 index 00000000..d0a95c21 --- /dev/null +++ b/sope-core/NGExtensions/NGExtensions/NSString+Escaping.h @@ -0,0 +1,41 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + OGo is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ +// $Id$ + + +#ifndef __NGExtensions_NSString_Escaping_H__ +#define __NGExtensions_NSString_Escaping_H__ + +#import + +@protocol NGStringEscaping < NSObject > +- (NSString *)stringByEscapingString:(NSString *)_s; +@end + +@interface NSString (Escaping) + +- (NSString *)stringByApplyingCEscaping; + +- (NSString *)stringByEscapingCharactersFromSet:(NSCharacterSet *)_set + usingStringEscaping:()_esc; +@end + +#endif /* __NGExtensions_NSString_Escaping_H__ */ diff --git a/sope-core/NGExtensions/NGExtensions/NSString+misc.h b/sope-core/NGExtensions/NGExtensions/NSString+misc.h index 0515ec7e..6ff0a8b2 100644 --- a/sope-core/NGExtensions/NGExtensions/NSString+misc.h +++ b/sope-core/NGExtensions/NGExtensions/NSString+misc.h @@ -33,8 +33,6 @@ @interface NSString(misc) -- (NSString *)stringByApplyingCEscaping; - /* Replaces keys, which enclosed in '$', with values from _bindings. The values are retrieved using the '-valueForStringBinding:' method which per default diff --git a/sope-core/NGExtensions/Version b/sope-core/NGExtensions/Version index 754354e9..902b0e2d 100644 --- a/sope-core/NGExtensions/Version +++ b/sope-core/NGExtensions/Version @@ -1,6 +1,6 @@ # version -SUBMINOR_VERSION:=122 +SUBMINOR_VERSION:=123 # v4.3.115 requires libFoundation v1.0.59 # v4.2.72 requires libEOControl v4.2.39