Bug report in {Function} Replace (advanced.grg)

A forum dedicated to George scripting questions
Post Reply
User avatar
aAntin
Posts: 29
Joined: 29 Aug 2007, 09:40
Location: France
Contact:

Bug report in {Function} Replace (advanced.grg)

Post by aAntin »

Hi there,

Just wanted to report a bug in the function Replace() (in advanced.grg)

Because the function is missing a length test at line 9, the following line will throw an error:
result = Replace("aaa","aaa","")

Here is my suggestion for fixing the bug:

Code: Select all

FUNCTION Replace(string,search,repl)
    LOCAL pos workstr str1 str2 size
    pos = 1
    workstr = string
    size = LEN(search)
    
    WHILE ((pos = FindString(workstr,search,pos)) > 0)
        IF (pos == 1)
            IF (size < LEN(workstr))
                str1 = CUT(workstr,size+1,LEN(workstr))
                workstr = repl""str1
            ELSE
                workstr = repl
            END
            pos = 0
        ELSE
            str1 = CUT(workstr,1,pos-1)
            IF ((pos+size) < LEN(workstr))
                str2 = CUT(workstr,pos+size,LEN(workstr))
                workstr = str1""repl""str2
            ELSE
                workstr = str1""repl
            END
        END
        pos = pos+LEN(repl)
    END
    RETURN workstr
END
Cheers! :wink:
Post Reply