2 Copyright (C) 2004-2005 OpenGroupware.org
4 This file is part of versitSaxDriver, written for the OpenGroupware.org
7 SOPE is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with SOPE; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "VSStringFormatter.h"
26 @implementation VSStringFormatter
28 static NSCharacterSet *escSet = nil;
31 static BOOL didInit = NO;
37 escSet = [[NSCharacterSet characterSetWithCharactersInString:@"\\"] retain];
40 + (id)sharedFormatter {
41 static id sharedInstance = nil;
43 sharedInstance = [[self alloc] init];
45 return sharedInstance;
48 - (NSString *)stringByUnescapingRFC2445Text:(NSString *)_s {
49 NSMutableString *safeString;
51 NSRange prevRange, escRange;
56 prevRange = NSMakeRange(0, length);
57 escRange = [_s rangeOfCharacterFromSet:escSet options:0 range:prevRange];
59 needsEscaping = escRange.length > 0 ? YES : NO;
61 return _s; /* cheap */
63 safeString = [NSMutableString stringWithCapacity:length];
66 prevRange.length = escRange.location - prevRange.location;
67 if (prevRange.length > 0)
68 [safeString appendString:[_s substringWithRange:prevRange]];
71 if(NSMaxRange(escRange) < length) {
72 unichar c = [_s characterAtIndex:escRange.location + 1];
73 if(c == 'n' || c == 'N') {
74 [safeString appendString:@"\n"];
75 escRange.length += 1; /* skip the 'n' */
79 prevRange.location = NSMaxRange(escRange);
80 todoRange.location = prevRange.location;
81 todoRange.length = length - prevRange.location;
82 escRange = [_s rangeOfCharacterFromSet:escSet
86 while(escRange.length > 0);
88 if (todoRange.length > 0)
89 [safeString appendString:[_s substringWithRange:todoRange]];
94 @end /* VSStringFormatter */