2 Copyright (C) 2004 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
24 #include "VSStringFormatter.h"
27 @implementation VSStringFormatter
29 static NSCharacterSet *escSet = nil;
32 static BOOL didInit = NO;
38 escSet = [[NSCharacterSet characterSetWithCharactersInString:@"\\"] retain];
41 + (id)sharedFormatter {
42 static id sharedInstance = nil;
44 sharedInstance = [[self alloc] init];
46 return sharedInstance;
49 - (NSString *)stringByUnescapingRFC2445Text:(NSString *)_s {
50 NSMutableString *safeString;
52 NSRange prevRange, escRange;
57 prevRange = NSMakeRange(0, length);
58 escRange = [_s rangeOfCharacterFromSet:escSet options:0 range:prevRange];
60 needsEscaping = escRange.length > 0 ? YES : NO;
62 return _s; /* cheap */
64 safeString = [NSMutableString stringWithCapacity:length];
67 prevRange.length = escRange.location - prevRange.location;
68 if (prevRange.length > 0)
69 [safeString appendString:[_s substringWithRange:prevRange]];
72 if(NSMaxRange(escRange) < length) {
73 unichar c = [_s characterAtIndex:escRange.location + 1];
74 if(c == 'n' || c == 'N') {
75 [safeString appendString:@"\n"];
76 escRange.length += 1; /* skip the 'n' */
80 prevRange.location = NSMaxRange(escRange);
81 todoRange.location = prevRange.location;
82 todoRange.length = length - prevRange.location;
83 escRange = [_s rangeOfCharacterFromSet:escSet
87 while(escRange.length > 0);
89 if (todoRange.length > 0)
90 [safeString appendString:[_s substringWithRange:todoRange]];