32 :(unsigned)inIndentation errorDescription:(NSString **)outErrorDescription
33{
34 NSCharacterSet *charSet;
35 NSRange foundRange, searchRange;
36 NSString *foundString;
37 NSMutableString *newString;
38 NSUInteger length;
39
40 length = [self length];
41 if (0 != length
42 && [self rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location == NSNotFound
43 && ![[NSCharacterSet decimalDigitCharacterSet] longCharacterIsMember:[self characterAtIndex:0]])
44 {
45
46 return [[self copy] autorelease];
47 }
48 else
49 {
50 charSet = [NSCharacterSet characterSetWithCharactersInString:@"\"\r\n\\"];
51 foundRange = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch];
52 if (NSNotFound == foundRange.location)
53 {
54 newString = (NSMutableString *)self;
55 }
56 else
57 {
58
59 newString = [[[self substringToIndex:foundRange.location] mutableCopy] autorelease];
60
61 for (;;)
62 {
63
64 foundString = [self substringWithRange:foundRange];
65 if ([foundString isEqual:@"\""]) [newString appendString:@"\\\""];
66 else if ([foundString isEqual:@"\n"]) [newString appendString:@"\\\n"];
67 else if ([foundString isEqual:@"\r"]) [newString appendString:@"\\\r"];
68 else if ([foundString isEqual:@"\\"]) [newString appendString:@"\\\\"];
69 else
70 {
71 [NSException raise:NSInternalInconsistencyException format:@"%s: expected \" or newline, found %@", __PRETTY_FUNCTION__, foundString];
72 }
73
74
75 searchRange.location = foundRange.location + foundRange.length;
76 searchRange.length = length - searchRange.location;
77
78
79 foundRange = [self rangeOfCharacterFromSet:charSet options:NSLiteralSearch range:searchRange];
80 if (NSNotFound == foundRange.location)
81 {
82 [newString appendString:[self substringWithRange:searchRange]];
83 break;
84 }
85 }
86 }
87
88 return [NSString stringWithFormat:@"\"%@\"", newString];
89 }
90}