]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSString+Escaping.m
removed & as a URL safe char
[sope] / sope-core / NGExtensions / FdExt.subproj / NSString+Escaping.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include <NGExtensions/NSString+Escaping.h>
23 #include "common.h"
24
25 @implementation NSString(Escaping)
26
27 - (NSString *)stringByApplyingCEscaping {
28   // Unicode!
29   const char   *cstr;
30   char         *buffer;
31   register int pos = 0;
32   
33   cstr = [self cString];
34   buffer = malloc([self cStringLength] * 2 + 1);
35   
36   while (*cstr) {
37     switch (*cstr) {
38       case '\n':
39         buffer[pos] = '\\'; pos++;
40         buffer[pos] = 'n';
41         break;
42       case '\r':
43         buffer[pos] = '\\'; pos++;
44         buffer[pos] = 'r';
45         break;
46       case '\t':
47         buffer[pos] = '\\'; pos++;
48         buffer[pos] = 't';
49         break;
50         
51       default:
52         buffer[pos] = *cstr;
53         break;
54     }
55     cstr++;
56     pos++;
57   }
58   buffer[pos] = '\0';
59   
60 #if NeXT_Foundation_LIBRARY || GNUSTEP_BASE_LIBRARY
61   {
62     NSString *s;
63     
64     s = buffer ? [NSString stringWithCString:buffer] : nil;
65     if (buffer) free(buffer);
66     return s;
67   }
68 #else
69   return [NSString stringWithCStringNoCopy:buffer freeWhenDone:YES];
70 #endif
71 }
72
73 - (NSString *)stringByEscapingCharactersFromSet:(NSCharacterSet *)_escSet
74   usingStringEscaping:(<NGStringEscaping>)_esc
75 {
76   NSMutableString *safeString;
77   unsigned length;
78   NSRange  prevRange, escRange;
79   NSRange  todoRange;
80   BOOL     needsEscaping;
81   
82   length    = [self length];
83   prevRange = NSMakeRange(0, length);
84   escRange  = [self rangeOfCharacterFromSet:_escSet options:0 range:prevRange];
85
86   needsEscaping = escRange.length > 0 ? YES : NO;
87   if (!needsEscaping)
88     return self; /* cheap */
89   
90   safeString = [NSMutableString stringWithCapacity:length];
91
92   do {
93     NSString *s;
94
95     prevRange.length = escRange.location - prevRange.location;
96     if (prevRange.length > 0)
97       [safeString appendString:[self substringWithRange:prevRange]];
98     
99     s = [_esc stringByEscapingString:[self substringWithRange:escRange]];
100     if (s != nil)
101         [safeString appendString:s];
102
103     prevRange.location = NSMaxRange(escRange);
104     todoRange.location = prevRange.location;
105     todoRange.length   = length - prevRange.location;
106     escRange           = [self rangeOfCharacterFromSet:_escSet
107                                options:0
108                                                  range:todoRange];
109   }
110   while(escRange.length > 0);
111   
112   if (todoRange.length > 0)
113     [safeString appendString:[self substringWithRange:todoRange]];
114   
115   return safeString;
116 }
117
118 @end /* NSString(Escaping) */