]> err.no Git - sope/blob - sope-xml/STXSaxDriver/ExtraSTX/NSString+STX.m
added support for XML-RPC 'nil' type
[sope] / sope-xml / STXSaxDriver / ExtraSTX / NSString+STX.m
1 /*
2   Copyright (C) 2004 eXtrapola Srl
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 "NSString+STX.h"
23 #include "common.h"
24
25 @implementation NSString(STX)
26
27 - (StructuredText *)structuredText {
28   return [[[StructuredText alloc] initWithString:self] autorelease];
29 }
30
31 - (NSString *)unescapedString {
32   NSMutableString *result;
33   NSString        *text;
34   int             i, start, length;
35   NSRange         range;
36   
37   result = [NSMutableString stringWithCapacity:[self length]];
38
39   text = self;
40   length = [text length];
41
42   for (i = start = 0; i < length; i++) {
43     unichar c;
44
45     c = [text characterAtIndex:i];
46
47     if (c == '\\') {
48       if (i - start > 0) {
49         range.location = start;
50         range.length = i - start;
51
52         [result appendString:[text substringWithRange:range]];
53       }
54
55       if (i + 1 < length) {
56         c = [text characterAtIndex:i + 1];
57
58         if (c == '\\') {
59           start = ++i;
60         }
61       }
62     }
63   }
64
65   if (i - start > 0) {
66     range.location = start;
67     range.length = i - start;
68
69     [result appendString:[text substringWithRange:range]];
70   }
71
72   return result;
73 }
74
75 @end /* NSString(STX) */