Restored from previous forum. Originally posted by Pupil. Nice Rings!
I've made a routine much like yours, don't know if mine is faster, smaller or whatever but i works atleast. Only problem is that it's not so good commented as yours, my bad

Included in the code listing is a small example of how to call the procedure..
With some more work it will be possible to handle different sizes of search-string and replace-string. One solution could be to go thru the whole search procedure and put markers in a dynamic list where a replacement should be. Then save the fragment between markers and put the replacement in between that. Hope you got that and that my explannation wasn't too bad..
Code:
Structure CharType
Char.b
EndStructure
Procedure FindAndReplace_Mem(*buffer, memlength.l, search.s, replace.s)
length.l=Len(search)
If length.lLen(replace.s)
ProcedureReturn #FALSE
EndIf
*mem.CharType = *buffer
*string_s.CharType = @search.s : stringpos.l=1
*string_r.CharType = @replace.s
memmax.l=*mem+memlength
stringbase_s.l=*string_s
stringbase_r.l=*string_r
stringmax_s.l=*string_s+length-1
stringmax_r.l=*string_r+length-1
Repeat
If *mem\Char = *string_s\Char
*mem+1 : *string_s+1
memold.l=*mem
loop=#TRUE
While *mem=memmax
ProcedureReturn #TRUE
EndProcedure
If ReadFile(0,"data.txt")
l.l=Lof()
*data=AllocateMemory(0,l,0)
ReadData(*data,l) : CloseFile(0)
If FindAndReplace_Mem(*data, l, "n", "_")
If CreateFile(0,"data1.txt")
WriteData(*data,l) : CloseFile(0)
EndIf
EndIf
Endif
Have edited and updated the code some! Possibly faster now..
Edited by - Pupil on 16 January 2002 18:16:30