Munger
Munger Search and replace text (or any byte array)
#include <ToolUtils.h> Toolbox Utilities
long Munger(destStrH, offset, findStr,findLen, rplcStr, rplcLen );
Handle destStrH ; handle leading to byte-array to access
long offset ; offset in destStrH to start search/modify
Ptr findStr ; these refer to search . . .
long findLen ; . . . and replace string . . .
Ptr rplcStr ; . . . addresses and lengths . . .
long rplcLen ; . . . meanings vary depending upon options
returns offset at which data was found, replaced, etc.
(negative values mean not found)
This is a general search and replace tool. Variations include searching,
deleting and inserting bytes into an array.
destStrH is a handle leading to any arbitrary array of bytes; typically text.
offset specifies where in the destStrH data you want the operation to
begin. Be careful that this value is less than the size of the data
addressed by destStrH.
findStr is the address of some bytes to search for. If found, these bytes are
deleted and replaced by the data at rplcStr. If findStr=NIL, findLen
bytes starting at offset are replaced by the rplcLen bytes of rplcStr.
findLen is the size, in bytes, of findStr. Exceptions: If findStr=NIL, it
specifies how may bytes of destStrH to replace. A negative value
specifies to replace to the end of destStrH .
rplcStr is the address of some bytes to be used to replace data in destStrH .
If rplcStr=NIL, destStrH is searched, starting at offset , for the
first occurrence of findStr .
rplcLen is the size, in bytes, of rplcStr . Use 0 (with a non-NIL rplcStr )
to delete data from destStrH .
Returns: a signed long integer. It is the offset from the beginning of the
destStrH data at which something occurred. For replace operations,
it is the offset of the byte following the replaced data. For deletions,
it is where the deletion occurred. A negative value means that the
search string was not found.

Notes: This function does NOT use Pascal-style strings. It determines the size of
destStrH via GetHandleSize and manipulates its length via
Here's a summary of operations assuming the following:
destStrH is a handle leading to some text.
offset is set to some starting point within that text.
Searching
Set findStr and findLen to the text you want to find and its length.
Set rplcStr to NIL.
Set rplcLen to 0.
The return value is negative if the string is not found; otherwise, it is
the offset in destStrH of the start of the string.
Searching and Replacing the Found Text
Set findStr and findLen to the text you want to find and its length.
Set rplcStr and rplcLen to the replacement text and its length.
The return value is negative if the search string is not found; otherwise,
it is the offset in destStrH of the character just after the replacement text
(i.e., ready for the next search).
In the event that the first part (a subset) of findStr matches the text at
the very end of destStrH , that text is replaced with all of rplcStr .
Inserting
Set findLen to 0 (findStr is ignored).
Set rplcStr and rplcLen to the text to insert and its length.
The return value is the offset in destStrH of the character just after the
inserted text.
Deleting
Set findStr to NIL.
Set findLen to the number of characters to delete.
Set rplcStr to any non-NIL address.
Set rplcLen to 0.
The return value is the same as offset .
Deleting to the End of the Destination String
Set findStr to NIL.
Set findLen to -1.
Set rplcStr to any non-NIL address.
Set rplcLen to 0.
The return value is the same as offset .
Searching and Deleting the Found Text
Set findStr and findLen to the text you want to find and its length.
Set rplcStr to a non-NIL address and set rplcLen to 0.
The return value is negative if the search string is not found; otherwise,
it is the same as offset .
Although Munger is used almost exclusively for manipulating text, it
could find a home in an image-data compression and decompression
algorithm. However, you're probably better off using an optimized routine
such as PackBits.
Note: The word "Munger" rhymes with "plunger".