PostScript的readstring:有没有更有效的方法来解析行?

时间:2019-06-05 00:05:03

标签: algorithm performance postscript

我的解析输入行的非常老的PostScript代码如下:

/buff 1 string def      % check for line wrap every character
/EOLchar (\n) def       % line feed

{ %loop
        currentfile buff readstring exch
        dup EOLchar eq
    { %ifelse
        %...
    }
    { %else
        %...
    } ifelse
    not { %if readstring found EOF
        exit    % end of file
    } if
} loop

是否有更有效的方式读取整行以进行进一步处理?

我想将数据嵌入到文件中,并且每一行都将描述一个线段。 每行包含多个数字字段,并用TAB字符分隔(因此,我将这些行拆分为多个字段,并将字段字符串转换为intreal,以方便处理)。

我当然可以在外部转换绘图数据(例如,转换为PostSript数组),但是我的想法是向每个数据文件添加一些固定的魔术PostScript标头,然后将其呈现给数据...

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样? readline为您处理行(不超过解释器定义的字符串容量)。 token处理跳过空格,并视情况将数字转换为实数或整数。

还要注意,文件读取运算符的布尔结果可用于尽早退出循环,这似乎很有效。

/buf 65535 string def
/f currentfile def
{
  f buf readline not {exit} if
  { token {exch}{exit} ifelse } loop
  {lineto} stopped {moveto} if
} loop
100 200
300 400
500 600