PostScript:`cvs`是否终止字符串缓冲区? [英] PostScript: Does `cvs` terminate the string buffer?

查看:107
本文介绍了PostScript:`cvs`是否终止字符串缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 cvs 时的一个想法是通过重新使用字符串缓冲区来帮助垃圾收集器,例如

One idea when using cvs is to help the garbage collector by re-using a string buffer, like

/s 5 string def
s 2 cvs %...
s 66 cvs %...

但是在循环中执行此操作时,实际上在缓冲区中找到的字符串是:

However when doing such a thing in a loop, the strings actually found in the buffer were:

(-40.0)
(-30.0)
(-20.0)
(-10.0)
(0.0.0)
(10.00)
(20.00)
(30.00)
(40.00)

因此,当转换结束时,字符串似乎并未终止(在GhostScript 9.26中找到)。不幸的是,GhostScript参考手册对字符串是否会终止没有保持沉默。

So it seems the strings are not terminated when conversion ended (Found in GhostScript 9.26). Unfortunately the GhostScript reference manual is somewhat silent about whether the string will be terminated or not.

所以问题是:预期的行为是什么?

尝试使用 val str dup 2 0之类的东西放置cvs 时,结果是(如 == ):

When trying something like val str dup 2 0 put cvs the result was (as shown by ==):

(-40.0)
(-30.0)
(-20.0)
(-10.0)
(0.0\0000)
(10.00)
(20.00)
(30.00)
(40.00)

那么重新使用字符串缓冲区到底是个坏主意吗?

So is re-using a string buffer a bad idea after all?

更新1:

我发现放入堆栈的值是正确的,而缓冲区中的值不是:

I found that the value put on the stack is correct, while the value in the buffer is not:

val s1 cvs pop            % leaves the wrong value in s1
val s1 cvs /s2 exch def   % leaves the correct value in s2


推荐答案

后记字符串不是C字符串。特别是,它们不是NUL终止的;相反,它们具有明确的长度,就像Postscript数组一样。一旦创建了字符串(或数组),就无法修改其长度,但是可以创建子字符串(或子数组)的视图(在Postscript术语中为 interval)。间隔不是副本。它与基础字符串(数组)共享存储。

Postscript strings are not C strings. In particular, they are not NUL-terminated; instead, they have explicit lengths, just like Postscript arrays. It is not possible to modify the length of a string (or array) once it is created, but it is possible to create a view ("interval" in Postscript terms) of a substring (or subarray). An interval is not a copy; it shares storage with the underlying string (array).

cvs 接受一个字符串(用作缓冲区) )作为参数,将value参数转换为字符串覆盖缓冲区的初始段,然后将该段作为间隔返回。 (请参阅PLRM的第568页;我懒得重新输入它。)如果您不希望在转换后的值的末尾使用垃圾,则需要使用该返回值。 (但返回值只有在您不将基础缓冲区用于其他用途时才有效。)

cvs takes a string (to be used as a buffer) as an argument, overwrites the initial segment of the buffer with the value argument converted to a string, and returns that segment as an interval. (See page 568 of the PLRM; I'm too lazy to retype it.) You'll want to use that return value if you don't want garbage at the end of your converted value. (But the returned value will only be valid as long as you don't use the underlying buffer for something else.)

这篇关于PostScript:`cvs`是否终止字符串缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆