1 /* NSString+Utilities.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse group conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCharacterSet.h>
25 #import <Foundation/NSEnumerator.h>
27 #import "NSArray+Utilities.h"
28 #import "NSDictionary+URL.h"
30 #import "NSString+Utilities.h"
32 static NSMutableCharacterSet *urlNonEndingChars = nil;
33 static NSMutableCharacterSet *urlAfterEndingChars = nil;
35 @implementation NSString (SOGoURLExtension)
37 - (NSString *) composeURLWithAction: (NSString *) action
38 parameters: (NSDictionary *) urlParameters
39 andHash: (BOOL) useHash
41 NSMutableString *completeURL;
43 completeURL = [NSMutableString new];
44 [completeURL autorelease];
46 [completeURL appendString: [self urlWithoutParameters]];
47 if (![completeURL hasSuffix: @"/"])
48 [completeURL appendString: @"/"];
49 [completeURL appendString: action];
50 [completeURL appendString: [urlParameters asURLParameters]];
52 [completeURL appendString: @"#"];
57 - (NSString *) hostlessURL
60 NSRange hostR, locationR;
62 if ([self hasPrefix: @"/"])
69 hostR = [self rangeOfString: @"://"];
70 locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
72 newURL = [self substringFromIndex: (hostR.location + hostR.length
73 + locationR.location)];
79 - (NSString *) urlWithoutParameters;
84 r = [self rangeOfString:@"?" options: NSBackwardsSearch];
86 newUrl = [self substringToIndex: NSMaxRange(r) - 1];
93 - (NSString *) davMethodToObjC
95 NSMutableString *newName;
96 NSEnumerator *components;
99 newName = [NSMutableString stringWithString: @"dav"];
100 components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
101 component = [components nextObject];
104 [newName appendString: [component capitalizedString]];
105 component = [components nextObject];
111 - (NSRange) _rangeOfURLInRange: (NSRange) refRange
116 if (!urlNonEndingChars)
118 urlNonEndingChars = [NSMutableCharacterSet new];
119 [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
121 if (!urlAfterEndingChars)
123 urlAfterEndingChars = [NSMutableCharacterSet new];
124 [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
127 start = refRange.location;
129 && ![urlAfterEndingChars characterIsMember:
130 [self characterAtIndex: start]])
133 length = [self length] - start;
134 workRange = NSMakeRange (start, length);
135 workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
136 options: NSLiteralSearch range: workRange];
137 if (workRange.location != NSNotFound)
138 length = workRange.location - start;
141 && [urlNonEndingChars characterIsMember:
142 [self characterAtIndex: (start + length - 1)]])
145 return NSMakeRange (start, length);
148 - (void) _handleURLs: (NSMutableString *) selfCopy
149 textToMatch: (NSString *) match
150 prefix: (NSString *) prefix
151 inRanges: (NSMutableArray *) ranges
153 NSRange httpRange, currentURL, rest;
154 NSString *urlText, *newUrlText;
155 unsigned int length, matchLength;
157 matchLength = [match length];
158 httpRange = [selfCopy rangeOfString: match];
159 while (httpRange.location != NSNotFound)
161 if ([ranges hasRangeIntersection: httpRange])
162 rest.location = NSMaxRange (httpRange);
165 currentURL = [selfCopy _rangeOfURLInRange: httpRange];
166 urlText = [selfCopy substringFromRange: currentURL];
167 if ([urlText length] > matchLength)
169 newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
170 prefix, urlText, urlText];
171 [selfCopy replaceCharactersInRange: currentURL
172 withString: newUrlText];
174 = NSMakeRange (currentURL.location, [newUrlText length]);
175 [ranges addRange: currentURL];
177 rest.location = NSMaxRange (currentURL);
179 length = [selfCopy length];
180 rest.length = length - rest.location;
181 httpRange = [selfCopy rangeOfString: match
182 options: 0 range: rest];
186 - (NSString *) stringByDetectingURLs
188 NSMutableString *selfCopy;
189 NSMutableArray *ranges;
191 ranges = [NSMutableArray new];
192 selfCopy = [NSMutableString stringWithString: self];
193 [self _handleURLs: selfCopy
197 [self _handleURLs: selfCopy
206 - (NSString *) jsonRepresentation
208 NSMutableString *representation;
210 representation = [NSMutableString stringWithString: self];
211 [representation replaceString: @"\\" withString: @"\\\\"];
212 [representation replaceString: @"\"" withString: @"\\\""];
213 [representation replaceString: @"/" withString: @"\\/"];
214 [representation replaceString: @"\b" withString: @"\\b"];
215 [representation replaceString: @"\f" withString: @"\\f"];
216 [representation replaceString: @"\n" withString: @"\\n"];
217 [representation replaceString: @"\r" withString: @"\\r"];
218 [representation replaceString: @"\t" withString: @"\\t"];
220 return [NSString stringWithFormat: @"\"%@\"", representation];
223 #if LIB_FOUNDATION_LIBRARY
226 return !([self isEqualToString: @"0"]
227 || [self isEqualToString: @"NO"]);