1 | //
|
---|
2 | // ICeCoffEEScanner.m
|
---|
3 | // ICeCoffEE
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Fri Feb 08 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "ICeCoffEEScanner.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | @implementation ICeCoffEEScanner
|
---|
13 |
|
---|
14 | - (id)initWithSource:(NSString *)aSource destination:(NSMutableString *)aDest;
|
---|
15 | {
|
---|
16 | if ( (self = [super init]) != nil) {
|
---|
17 | source = [aSource retain];
|
---|
18 | length = [source length];
|
---|
19 | dest = [aDest retain];
|
---|
20 | }
|
---|
21 | return self;
|
---|
22 | }
|
---|
23 |
|
---|
24 | - (void)dealloc;
|
---|
25 | {
|
---|
26 | [source release];
|
---|
27 | [dest release];
|
---|
28 | [super dealloc];
|
---|
29 | }
|
---|
30 |
|
---|
31 | - (NSRange)_nextMatchRangeForString:(NSString *)string;
|
---|
32 | {
|
---|
33 | return [source rangeOfString: string options: NSLiteralSearch range: NSMakeRange(loc, length - loc)];
|
---|
34 | }
|
---|
35 |
|
---|
36 | - (void)_breakWithString:(NSString *)breakString atWidth:(unsigned)width forChars:(unsigned)charsToCopy;
|
---|
37 | {
|
---|
38 | while (charsToCopy > width) {
|
---|
39 | [dest appendString: [source substringWithRange: NSMakeRange(loc, width)]];
|
---|
40 | charsToCopy -= width;
|
---|
41 | loc += width;
|
---|
42 | [dest appendString: breakString];
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | - (void)breakWithString:(NSString *)breakString atWidth:(unsigned)width;
|
---|
47 | {
|
---|
48 | NSRange matchRange = {1, 1};
|
---|
49 |
|
---|
50 | while ( (matchRange = [self _nextMatchRangeForString: breakString]).location != NSNotFound) {
|
---|
51 | [self _breakWithString: breakString atWidth: width forChars: matchRange.location - loc];
|
---|
52 | [dest appendString: [source substringWithRange: NSMakeRange(loc, matchRange.location + matchRange.length - loc)]];
|
---|
53 | loc = matchRange.location + matchRange.length;
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (![self isAtEnd]) {
|
---|
57 | [self _breakWithString: breakString atWidth: width forChars: [self remainingLength]];
|
---|
58 | [self appendUpToEnd];
|
---|
59 | [dest appendString: breakString];
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | - (BOOL)appendUpToString:(NSString *)string;
|
---|
65 | {
|
---|
66 | NSRange matchRange = [self _nextMatchRangeForString: string];
|
---|
67 |
|
---|
68 | if (matchRange.location == NSNotFound) return NO;
|
---|
69 |
|
---|
70 | [dest appendString: [source substringWithRange: NSMakeRange(loc, matchRange.location - loc)]];
|
---|
71 |
|
---|
72 | loc = matchRange.location + matchRange.length;
|
---|
73 |
|
---|
74 | return YES;
|
---|
75 | }
|
---|
76 |
|
---|
77 | - (void)appendUpToEnd;
|
---|
78 | {
|
---|
79 | [dest appendString: [source substringFromIndex: loc]];
|
---|
80 | loc = length;
|
---|
81 | }
|
---|
82 |
|
---|
83 | - (BOOL)isAtEnd;
|
---|
84 | {
|
---|
85 | return (loc >= length);
|
---|
86 | }
|
---|
87 |
|
---|
88 | - (unsigned)scanLocation;
|
---|
89 | {
|
---|
90 | return loc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | - (unsigned)remainingLength;
|
---|
94 | {
|
---|
95 | return (length - loc);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @end
|
---|