Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Replace in Gimp Script Fu

I have a Gimp plugin that renames files and I needed a replace function. Unfortunately TinyScheme that Gimp uses does not have a replace function for strings. I searched a lot but couldn't find one that worked as a true string-replace. Answer to follow...

like image 299
Isaac Avatar asked Jul 16 '12 17:07

Isaac


1 Answers

Here is the implementation that I created. Please feel free to let me know if there is a better solution.

(define (string-replace strIn strReplace strReplaceWith)
    (let*
        (
            (curIndex 0)
            (replaceLen (string-length strReplace))
            (replaceWithLen (string-length strReplaceWith))
            (inLen (string-length strIn))
            (result strIn)
        )
        ;loop through the main string searching for the substring
        (while (<= (+ curIndex replaceLen) inLen)
            ;check to see if the substring is a match
            (if (substring-equal? strReplace result curIndex (+ curIndex replaceLen))
                (begin
                    ;create the result string
                    (set! result (string-append (substring result 0 curIndex) strReplaceWith (substring result (+ curIndex replaceLen) inLen)))
                    ;now set the current index to the end of the replacement. it will get incremented below so take 1 away so we don't miss anything
                    (set! curIndex (-(+ curIndex replaceWithLen) 1))
                    ;set new length for inLen so we can accurately grab what we need
                    (set! inLen (string-length result))
                )
            )
            (set! curIndex (+ curIndex 1))
        )
       (string-append result "")
    )
)
like image 94
Isaac Avatar answered Oct 09 '22 05:10

Isaac