OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #import "CDBalanceFormatter.h" |
| 7 |
| 8 #import "NSString-Extensions.h" |
| 9 |
| 10 static BOOL debug = NO; |
| 11 |
| 12 @implementation CDBalanceFormatter |
| 13 |
| 14 - (id)initWithString:(NSString *)str; |
| 15 { |
| 16 if ([super init] == nil) |
| 17 return nil; |
| 18 |
| 19 scanner = [[NSScanner alloc] initWithString:str]; |
| 20 openCloseSet = [[NSCharacterSet characterSetWithCharactersInString:@"{}<>()"
] retain]; |
| 21 |
| 22 result = [[NSMutableString alloc] init]; |
| 23 |
| 24 return self; |
| 25 } |
| 26 |
| 27 - (void)dealloc; |
| 28 { |
| 29 [scanner release]; |
| 30 [openCloseSet release]; |
| 31 |
| 32 [result release]; |
| 33 |
| 34 [super dealloc]; |
| 35 } |
| 36 |
| 37 - (void)parse:(NSString *)open index:(NSUInteger)openIndex level:(NSUInteger)lev
el; |
| 38 { |
| 39 NSArray *pairs; |
| 40 NSString *pre; |
| 41 NSString *opens[] = { @"{", @"<", @"(", nil}; |
| 42 NSString *closes[] = { @"}", @">", @")", nil}; |
| 43 NSUInteger index; |
| 44 BOOL foundOpen = NO; |
| 45 BOOL foundClose = NO; |
| 46 |
| 47 pairs = [[NSArray alloc] initWithObjects:@"{}", @"<>", @"()", nil]; |
| 48 |
| 49 while ([scanner isAtEnd] == NO) { |
| 50 if ([scanner scanUpToCharactersFromSet:openCloseSet intoString:&pre]) { |
| 51 if (debug) NSLog(@"pre = '%@'", pre); |
| 52 [result appendFormat:@"%@%@\n", [NSString spacesIndentedToLevel:leve
l], pre]; |
| 53 } |
| 54 if (debug) NSLog(@"remaining: '%@'", [[scanner string] substringFromInde
x:[scanner scanLocation]]); |
| 55 |
| 56 foundOpen = foundClose = NO; |
| 57 for (index = 0; index < 3; index++) { |
| 58 if (debug) NSLog(@"Checking open %u: '%@'", index, opens[index]); |
| 59 if ([scanner scanString:opens[index] intoString:NULL]) { |
| 60 if (debug) NSLog(@"Start %@", opens[index]); |
| 61 [result appendSpacesIndentedToLevel:level]; |
| 62 [result appendString:opens[index]]; |
| 63 [result appendString:@"\n"]; |
| 64 |
| 65 [self parse:opens[index] index:[scanner scanLocation] - 1 level:
level + 1]; |
| 66 |
| 67 [result appendSpacesIndentedToLevel:level]; |
| 68 [result appendString:closes[index]]; |
| 69 [result appendString:@"\n"]; |
| 70 foundOpen = YES; |
| 71 break; |
| 72 } |
| 73 |
| 74 if (debug) NSLog(@"Checking close %u: '%@'", index, closes[index]); |
| 75 if ([scanner scanString:closes[index] intoString:NULL]) { |
| 76 if ([open isEqualToString:opens[index]]) { |
| 77 if (debug) NSLog(@"End %@", closes[index]); |
| 78 } else { |
| 79 NSLog(@"ERROR: Unmatched end %@", closes[index]); |
| 80 } |
| 81 foundClose = YES; |
| 82 break; |
| 83 } |
| 84 } |
| 85 |
| 86 if (foundOpen == NO && foundClose == NO) { |
| 87 if (debug) NSLog(@"Unknown @ %u: %@", [scanner scanLocation], [[scan
ner string] substringFromIndex:[scanner scanLocation]]); |
| 88 break; |
| 89 } |
| 90 |
| 91 if (foundClose) |
| 92 break; |
| 93 } |
| 94 |
| 95 [pairs release]; |
| 96 } |
| 97 |
| 98 - (NSString *)format; |
| 99 { |
| 100 [self parse:nil index:0 level:0]; |
| 101 |
| 102 if (debug) NSLog(@"result:\n%@", result); |
| 103 |
| 104 return [NSString stringWithString:result]; |
| 105 } |
| 106 |
| 107 @end |
OLD | NEW |