aboutsummaryrefslogtreecommitdiffstats
path: root/NSString+LispExtensions.m
blob: 4d36e5206930092638b8e06a2930d0576e3211bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
//  NSString+LispExtensions.m
//  Moxie
//
//  Created by Brian Cully on Sat Sep 04 2004.
//  Copyright (c) 2004 Brian Cully. All rights reserved.
//

#import "NSString+LispExtensions.h"

@implementation NSString (LispExtensions)
- (NSString *)lispForm
{
    NSMutableString *result;
    NSRange subrange;
    unsigned int len, i;
    
    result = [NSMutableString stringWithString: @"\""];
    len = [self length];
    subrange.location = 0;
    for (i = 0; i < len; i++) {
        switch ([self characterAtIndex: i]) {
            case '\\':
            case '\"':
                subrange.length = i - subrange.location;
                [result appendString: [self substringWithRange: subrange]];
                [result appendString: @"\\"];
                subrange.location = i;
                break;
        }
    }
    if (subrange.location < len) {
        subrange.length = len - subrange.location;
        [result appendString: [self substringWithRange: subrange]];
    }
    [result appendString: @"\""];
                
    return result;
}
@end