如何在Scheme中创建带有可扩展关键字的字符串?

发布于 2024-11-17 05:08:12 字数 512 浏览 3 评论 0原文

我正在编写一个 GIMP 脚本来将图层导出到文件。我想提供一个用户指定的字段,他们可以在其中提供要导出的文件名的格式,以及每个元素的波形符字符组合(即名为“foo”的文件,其中包含层“bar”和“baz”,以及输出文件名格式为“~f-~l.png”,将输出“foo-bar.png”和“foo-baz.png”)。我知道如何在 Lua 中执行此操作:

local nameformat = "~f-~l.png"
local function layer_export_name(filename, layername)
  return string.gsub(nameformat, '~.', {f=filename, l=layername})
end

How can I do this in GIMP's Scheme?

重申一下:我需要替换字符串中的关键字。我不需要创建我已经定义的字符串的函数。

I'm writing a GIMP script to export layers to files. I want to provide a user-specifiable field where they can provide the format for the filenames to be exported, with tilde-character combinations for each element (ie. a file named "foo" with layers "bar" and "baz", with the output filename format being "~f-~l.png", would output "foo-bar.png" and "foo-baz.png"). I know how I would do this in Lua:

local nameformat = "~f-~l.png"
local function layer_export_name(filename, layername)
  return string.gsub(nameformat, '~.', {f=filename, l=layername})
end

How can I do this in GIMP's Scheme?

To reiterate: I need to replace keywords in a string. I don't need a function that creates a string I've already defined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-11-24 05:08:12

没有标准的计划程序可以做到这一点。我编写了以下低效但可移植的过程:

(define (expand-keywords string tokens)
  (let loop ((slist (string->list string))
             (in-replace-mode #f)
             (result ""))
    (if (not (null? slist))        
        (let ((c (car slist)))
          (cond (in-replace-mode
                 (let ((token (car (cdr (assoc c tokens)))))
                   (loop (cdr slist) #f (string-append result token))))
                ((char=? c #\~)
                 (loop (cdr slist) #t result))
                (else 
                 (loop (cdr slist) #f (string-append result 
                                                     (make-string 1 c))))))
        result)))

示例用法:

> (expand-keywords "~f-~l.png" '((#\f "vijay") (#\l "9")))
"vijay-9.png"
> (expand-keywords "~f-~l.~e" '((#\f "vijay") (#\l "10") (#\e "jpg")))
"vijay-10.jpg"

如果您的Scheme 实现提供了就地修改字符串的过程,请使用它而不是string-append

There is no standard Scheme procedure to do this. I wrote the following in-efficient but portable procedure:

(define (expand-keywords string tokens)
  (let loop ((slist (string->list string))
             (in-replace-mode #f)
             (result ""))
    (if (not (null? slist))        
        (let ((c (car slist)))
          (cond (in-replace-mode
                 (let ((token (car (cdr (assoc c tokens)))))
                   (loop (cdr slist) #f (string-append result token))))
                ((char=? c #\~)
                 (loop (cdr slist) #t result))
                (else 
                 (loop (cdr slist) #f (string-append result 
                                                     (make-string 1 c))))))
        result)))

Sample usage:

> (expand-keywords "~f-~l.png" '((#\f "vijay") (#\l "9")))
"vijay-9.png"
> (expand-keywords "~f-~l.~e" '((#\f "vijay") (#\l "10") (#\e "jpg")))
"vijay-10.jpg"

If your Scheme implementation provides a procedure to modify a string in-place, use that instead of string-append.

小嗲 2024-11-24 05:08:12

重要警告:我知道很多Scheme,但不知道很多GIMP 方案——显然是TinyScheme。例如,在 Racket 中,您可以编写

(format "~a-~a.png" filename layername)

...假设 layername 是一个字符串(从您的问题中我不清楚是否是这种情况)。

我猜你在TinyScheme中没有racket的“格式”,但你几乎肯定有“字符串附加”:

(string-append filename "-" layername ".png")

如果layername是一个字符串,你可能还想使用number->string函数,像这样:

(string-append filename "-" (number->string layername) ".png")

BIG CAVEAT: I know lots of Scheme, but not lots of GIMP scheme--apparently TinyScheme. In Racket, for instance, you would write

(format "~a-~a.png" filename layername)

... assuming that layername is a string (it's not clear to me from your question whether this is the case).

I'm guessing that you don't have racket's "format" in TinyScheme, but you almost certainly have "string-append":

(string-append filename "-" layername ".png")

If layername is a string, you'll probably also want to use the number->string function, like this:

(string-append filename "-" (number->string layername) ".png")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文