REM
REM Contributed by James C. Fuller - April 2009.
REM
REM bacon replace

FUNCTION Replace(STRING Main$,STRING Match$,STRING Repl$)
    LOCAL mlen,rlen,mainlen,i,j TYPE int
    LOCAL S2$ TYPE STRING
    LOCAL sRet$ TYPE STRING
    mlen = LEN(Match$)
    rlen = LEN(Repl$)
    mainlen = LEN(Main$)
    S2$ = ""
    sRet$ = ""
    IF (mlen EQ 0) OR (rlen EQ 0) OR (mainlen EQ 0) THEN
        RETURN sRet$
    END IF      
    i = 0
    j = 1
    WHILE 1 DO
        i = INSTR(Main$,Match$,j)
        IF i THEN
            S2$ = CONCAT$(S2$,MID$(Main$,j,(i-j)),Repl$)
        ELSE
            BREAK
        END IF    
        IF j + mlen >= LEN(Main$) THEN
            BREAK
        ELSE
            j = i + mlen
        END IF
    WEND
    IF j > 1 THEN
        sRet$ = CONCAT$(S2$,RIGHT$(Main$,(mainlen-j)+1))
        RETURN sRet$
    ENDIF
    RETURN Main$
ENDFUNCTION