2012-07-16 2 views
3

파일 이름을 바꾸는 Gimp 플러그인이 있는데 바꿔 치기 기능이 필요했습니다. 불행히도 Gimp가 사용하는 TinyScheme에는 문자열에 대한 대체 기능이 없습니다. 나는 많은 것을 수색했으나 진정한 문자열 교체로 작동했던 것을 찾을 수 없었다. 수행 할 답변 ...김프 스크립트에서 문자열 바꾸기 Fu

답변

4

다음은 내가 작성한 구현입니다. 더 나은 해결책이 있다면 알려 주시기 바랍니다.

(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 "") 
    ) 
)