2 Copyright (C) 2004 OpenGroupware.org
4 This file is part of versitSaxDriver, written for the OpenGroupware.org
7 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "VSStringFormatter.h"
28 @implementation VSStringFormatter
30 static NSCharacterSet *escSet = nil;
33 static BOOL didInit = NO;
39 escSet = [[NSCharacterSet characterSetWithCharactersInString:@"\\"] retain];
42 + (id)sharedFormatter {
43 static id sharedInstance = nil;
45 sharedInstance = [[self alloc] init];
47 return sharedInstance;
50 - (NSString *)stringByUnescapingRFC2445Text:(NSString *)_s {
51 NSMutableString *safeString;
53 NSRange prevRange, escRange;
58 prevRange = NSMakeRange(0, length);
59 escRange = [_s rangeOfCharacterFromSet:escSet options:0 range:prevRange];
61 needsEscaping = escRange.length > 0 ? YES : NO;
63 return _s; /* cheap */
65 safeString = [NSMutableString stringWithCapacity:length];
68 prevRange.length = escRange.location - prevRange.location;
69 if (prevRange.length > 0)
70 [safeString appendString:[_s substringWithRange:prevRange]];
73 if(NSMaxRange(escRange) < length) {
74 unichar c = [_s characterAtIndex:escRange.location + 1];
75 if(c == 'n' || c == 'N') {
76 [safeString appendString:@"\n"];
77 escRange.length += 1; /* skip the 'n' */
81 prevRange.location = NSMaxRange(escRange);
82 todoRange.location = prevRange.location;
83 todoRange.length = length - prevRange.location;
84 escRange = [_s rangeOfCharacterFromSet:escSet
88 while(escRange.length > 0);
90 if (todoRange.length > 0)
91 [safeString appendString:[_s substringWithRange:todoRange]];