Find and Replace Text

Date:    Sun, 27 Nov 1994 21:29:24 -0600
From:    Matthew Caldwell <sexkittn@BURN.DEMON.CO.UK>
Subject: findAndReplaceAll correction
A week or so ago I posted a script containing an untested find & replace handler. On testing, it proved to have a bug which could snuff Director, so here's the corrected version just in case anyone has a use for it... This one should work. It isn't exactly a speed-freak, but.

As for the rest of that stuff (the scriptText altering), I still haven't tried it out. Anyone want to do it for me?

 -- utility to perform search & replace on a given text
 
 on findAndReplaceAll sourceText, findText, replaceText
   set findLength = length(findText) - 1
   set startSearchAt = 1
   repeat while sourceText contains findText
     repeat with charNum = startSearchAt to (length(sourceText) - findLength)
       set testChunk = char charNum to (charNum + findLength) of sourceText
       if testChunk = findText then
         if charNum = 1 then set beforeChunk = ""
         else set beforeChunk = char 1 to (charNum - 1) of sourceText
         -- the -> on the next line should be a continuation character
         set afterChunk = char (findLength+charNum+1) to length(sourceText) ->
           of sourceText
         set sourceText = beforeChunk & replaceText & afterChunk
         set startSearchAt = charNum
         exit repeat
       end if
     end repeat
   end repeat
   return sourceText
 end findAndReplaceAll