
/*
	General search and replace, replacing strings found between starting and ending delimeters.
	Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSString/General_search_and_.m>
	(See copyright notice at <http://cocoa.karelia.com>)
*/

/*"	General search-and-replace mechanism to convert text between the given delimeters.  Pass in a dictionary with the keys of "from" strings, and the values of what to convert them to.  If not found in the dictionary,  the text will just be removed.  If the dictionary passed in is nil, then the string between the delimeters will put in the place of the whole range; this could be used to just strip out the delimeters.

Requires -[NSString rangeFromString:toString:options:range:].
"*/

- (NSString *) replaceAllTextBetweenString:(NSString *)inString1 andString:(NSString *)inString2
	fromDictionary:(NSDictionary *)inDict
	options:(unsigned)inMask range:(NSRange)inSearchRange
{
	NSRange range = inSearchRange;	// We'll increment this
	int startLength = [inString1 length];
	int delimLength = startLength + [inString2 length];
	NSMutableString *buf = [NSMutableString string];

	NSRange beforeSearchRange = NSMakeRange(0,inSearchRange.location);
	[buf appendString:[self substringWithRange:beforeSearchRange]];

	// Now loop through; looking.
	while (range.length != 0)
	{
		NSRange foundRange = [self rangeFromString:inString1 toString:inString2 options:inMask range:range];
		if (foundRange.location != NSNotFound)
		{
			// First, append what was the search range and the found range -- before match -- to output
			{
				NSRange beforeRange = NSMakeRange(range.location, foundRange.location - range.location);
				NSString *before = [self substringWithRange:beforeRange];
				[buf appendString:before];
			}
			// Now, figure out what was between those two strings
			{
				NSRange betweenRange = NSMakeRange(foundRange.location + startLength, foundRange.length - delimLength);
				NSString *between = [self substringWithRange:betweenRange];
				if (nil != inDict)
				{
					between = [inDict objectForKey:between];	// replace string
				}
				// Now append the between value if not nil
				if (nil != between)
				{
					[buf appendString:[between description]];
				}
			}
			// Now, update things and move on.
			range.length = NSMaxRange(range) - NSMaxRange(foundRange);
			range.location = NSMaxRange(foundRange);
		}
		else
		{
			NSString *after = [self substringWithRange:range];
			[buf appendString:after];
			// Now, update to be past the range, to finish up.
			range.location = NSMaxRange(range);
			range.length = 0;
		}
	}
	// Finally, append stuff after the search range
	{
		NSRange afterSearchRange = NSMakeRange(range.location, [self length] - range.location);
		[buf appendString:[self substringWithRange:afterSearchRange]];
	}
	return [NSString stringWithString:buf];
}


/*"	Replace between the two given strings with the given options inMask; the delimeter strings are not included in the result.  The inMask parameter is the same as is passed to [NSString rangeOfString:options:range:].
"*/

- (NSString *) replaceAllTextBetweenString:(NSString *)inString1 andString:(NSString *)inString2
						 fromDictionary:(NSDictionary *)inDict
							options:(unsigned)inMask

{
	return [self replaceAllTextBetweenString:inString1 andString:inString2
								fromDictionary:inDict 
							  	options:inMask
								range:NSMakeRange(0,[self length])];
}

/*"	Replace between the two given strings with the default options; the delimeter strings are not included in the result.
"*/

- (NSString *) replaceAllTextBetweenString:(NSString *)inString1 andString:(NSString *)inString2
										fromDictionary:(NSDictionary *)inDict
{
	return [self replaceAllTextBetweenString:inString1 andString:inString2 fromDictionary:inDict options:0];
}

