2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <NGExtensions/NSString+Escaping.h>
25 @implementation NSString(Escaping)
27 - (NSString *)stringByApplyingCEscaping {
33 cstr = [self cString];
34 buffer = malloc([self cStringLength] * 2 + 1);
39 buffer[pos] = '\\'; pos++;
43 buffer[pos] = '\\'; pos++;
47 buffer[pos] = '\\'; pos++;
60 #if NeXT_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY
64 s = buffer ? [NSString stringWithCString:buffer] : nil;
65 if (buffer) free(buffer);
69 return [NSString stringWithCStringNoCopy:buffer freeWhenDone:YES];
73 - (NSString *)stringByEscapingCharactersFromSet:(NSCharacterSet *)_escSet
74 usingStringEscaping:(<NGStringEscaping>)_esc
76 NSMutableString *safeString;
78 NSRange prevRange, escRange;
82 length = [self length];
83 prevRange = NSMakeRange(0, length);
84 escRange = [self rangeOfCharacterFromSet:_escSet options:0 range:prevRange];
86 needsEscaping = escRange.length > 0 ? YES : NO;
88 return self; /* cheap */
90 safeString = [NSMutableString stringWithCapacity:length];
95 prevRange.length = escRange.location - prevRange.location;
96 if (prevRange.length > 0)
97 [safeString appendString:[self substringWithRange:prevRange]];
99 s = [_esc stringByEscapingString:[self substringWithRange:escRange]];
101 [safeString appendString:s];
103 prevRange.location = NSMaxRange(escRange);
104 todoRange.location = prevRange.location;
105 todoRange.length = length - prevRange.location;
106 escRange = [self rangeOfCharacterFromSet:_escSet
110 while(escRange.length > 0);
112 if (todoRange.length > 0)
113 [safeString appendString:[self substringWithRange:todoRange]];
118 @end /* NSString(Escaping) */