提问者:小点点

从节点中的stdout读取时是否删除多余的行尾?


const process = spawn(...);
process.stdout.setEncoding("utf8");
process.stdout.on("data", (data) => { console.log(data) }

生产

{
  data: 'Result__<?xml version="1.0" encoding="utf-8"?>\r\n' +
    '<?tgml 2.0?>\r\r\n' +
    '\r\r\n' +
    '<Tgml Height="400" Stretch="Uniform" Width="600">\r\r\n' +
    '  <Layer Stroke="None" Fill="#000000" Name="basic-shapes">\r\r\n' +
    '    <Group Id="main">\r\r\n' +
    '      <Line Stroke="#0000CC" StrokeWidth="5" X1="10" Y1="10" X2="100" Y2="200"/>\r\r\n' +
    ...
}

似乎每行都有一个额外的行尾。

什么是一个可靠的,只删除额外的行尾添加的读取,而不是任何行尾存在于原始文档?


共1个答案

匿名用户

摘要注释中的解释:

null

var data = 'Result__<?xml version="1.0" encoding="utf-8"?>\r\n' +
    '<?tgml 2.0?>\r\r\n' +
    '\r\r\n' +
    '<Tgml Height="400" Stretch="Uniform" Width="600">\r\r\n' +
    '  <Layer Stroke="None" Fill="#000000" Name="basic-shapes">\r\r\n' +
    '    <Group Id="main">\r\r\n' +
    '      <Line Stroke="#0000CC" StrokeWidth="5" X1="10" Y1="10" X2="100" Y2="200"/>\r\r\n';

var prevIndex = 0;
var newData = "";
while(prevIndex != -1){ // until no new lines found.
  nextN = data.indexOf('\n',prevIndex); // Find the index of the next new-lien in string
  while(data[nextN]=='\n') nextN++; // Pass over adj. new lines
  newData += data.slice(prevIndex,nextN-1); // copy the from the last index until current location, removing one '\n'
  prevIndex = nextN; // hold current index for the next trim.
}

console.log(newData); // this is the new string.