// // ICeCoffEEScanner.m // ICeCoffEE // // Created by Nicholas Riley on Fri Feb 08 2002. // Copyright (c) 2002 Nicholas Riley. All rights reserved. // #import "ICeCoffEEScanner.h" @implementation ICeCoffEEScanner - (id)initWithSource:(NSString *)aSource destination:(NSMutableString *)aDest; { if ( (self = [super init]) != nil) { source = [aSource retain]; length = [source length]; dest = [aDest retain]; } return self; } - (void)dealloc; { [source release]; [dest release]; [super dealloc]; } - (NSRange)_nextMatchRangeForString:(NSString *)string; { return [source rangeOfString: string options: NSLiteralSearch range: NSMakeRange(loc, length - loc)]; } - (void)_breakWithString:(NSString *)breakString atWidth:(unsigned)width forChars:(unsigned)charsToCopy; { while (charsToCopy > width) { [dest appendString: [source substringWithRange: NSMakeRange(loc, width)]]; charsToCopy -= width; loc += width; [dest appendString: breakString]; } } - (void)breakWithString:(NSString *)breakString atWidth:(unsigned)width; { NSRange matchRange = {1, 1}; while ( (matchRange = [self _nextMatchRangeForString: breakString]).location != NSNotFound) { [self _breakWithString: breakString atWidth: width forChars: matchRange.location - loc]; [dest appendString: [source substringWithRange: NSMakeRange(loc, matchRange.location + matchRange.length - loc)]]; loc = matchRange.location + matchRange.length; } if (![self isAtEnd]) { [self _breakWithString: breakString atWidth: width forChars: [self remainingLength]]; [self appendUpToEnd]; [dest appendString: breakString]; } } - (BOOL)appendUpToString:(NSString *)string; { NSRange matchRange = [self _nextMatchRangeForString: string]; if (matchRange.location == NSNotFound) return NO; [dest appendString: [source substringWithRange: NSMakeRange(loc, matchRange.location - loc)]]; loc = matchRange.location + matchRange.length; return YES; } - (void)appendUpToEnd; { [dest appendString: [source substringFromIndex: loc]]; loc = length; } - (BOOL)isAtEnd; { return (loc >= length); } - (unsigned)scanLocation; { return loc; } - (unsigned)remainingLength; { return (length - loc); } @end