如何在Scheme中创建带有可扩展关键字的字符串?
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有标准的计划程序可以做到这一点。我编写了以下低效但可移植的过程:
示例用法:
如果您的Scheme 实现提供了就地修改字符串的过程,请使用它而不是
string-append
。There is no standard Scheme procedure to do this. I wrote the following in-efficient but portable procedure:
Sample usage:
If your Scheme implementation provides a procedure to modify a string in-place, use that instead of
string-append
.重要警告:我知道很多Scheme,但不知道很多GIMP 方案——显然是TinyScheme。例如,在 Racket 中,您可以编写
...假设 layername 是一个字符串(从您的问题中我不清楚是否是这种情况)。
我猜你在TinyScheme中没有racket的“格式”,但你几乎肯定有“字符串附加”:
如果layername是一个字符串,你可能还想使用number->string函数,像这样:
BIG CAVEAT: I know lots of Scheme, but not lots of GIMP scheme--apparently TinyScheme. In Racket, for instance, you would write
... 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":
If layername is a string, you'll probably also want to use the number->string function, like this: