如何在Postscript中连接两个字符串?
(foo) (bar) ??? -> (foobar)
Run Code Online (Sandbox Code Playgroud)
PostScript没有内置的字符串连接运算符.你需要为此编写一些代码.例如,请参见http://en.wikibooks.org/wiki/PostScript_FAQ#How_to_concatenate_strings.3F.
(由mh更新:复制到这里)
/concatstrings % (a) (b) -> (ab)
{ exch dup length
2 index length add string
dup dup 4 2 roll copy length
4 -1 roll putinterval
} bind def
Run Code Online (Sandbox Code Playgroud)
同样的想法推广到任意数量的字符串。早期的修订版使用一个辅助函数acat,它接受一个字符串数组(以便于计数和迭代)。此版本使用更高级的循环和堆栈操作来避免分配数组。此版本还将通过将string运算符更改为来连接数组array。
% (s1) (s2) (s3) ... (sN) n ncat (s1s2s3...sN)
/ncat { % s1 s2 s3 .. sN n % first sum the lengths
dup 1 add % s1 s2 s3 .. sN n n+1
copy % s1 s2 s3 .. sN n s1 s2 s3 .. sN n
0 exch % s1 s2 s3 .. sN n s1 s2 s3 .. sN 0 n
{
exch length add
} repeat % s1 s2 s3 .. sN n len % then allocate string
string exch % s1 s2 s3 .. sN str n
0 exch % s1 s2 s3 .. sN str off n
-1 1 { % s1 s2 s3 .. sN str off n % copy each string
2 add -1 roll % s2 s3 .. sN str off s1 % bottom to top
3 copy putinterval % s2 s3 .. sN str' off s1
length add % s2 s3 .. sN str' off+len(s1)
% s2 s3 .. sN str' off'
} for % str' off'
pop % str'
} def
(abc) (def) (ghi) (jkl) 4 ncat == %(abcdefghijkl)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2843 次 |
| 最近记录: |