Skip to content

Commit 14dd4f9

Browse files
committed
Update newline handling
1 parent 5b52c60 commit 14dd4f9

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎src/buffer.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,35 @@ static int recvall(p_buffer buf, luaL_Buffer *b) {
230230
}
231231

232232
/*-------------------------------------------------------------------------*\
233-
* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF
234-
* are not returned by the function and are discarded from the buffer
233+
* Reads a line terminated by a CR LF pair or just by a LF. If chop, then
234+
* the line ending is discarded.
235235
\*-------------------------------------------------------------------------*/
236236
static int recvline(p_buffer buf, luaL_Buffer *b, int chop) {
237237
int err = IO_DONE;
238+
char hasprevch = 0;
239+
char prevch;
238240
while (err == IO_DONE) {
239241
size_t count, pos; const char *data;
240242
err = buffer_get(buf, &data, &count);
241243
pos = 0;
242244
while (pos < count && data[pos] != '\n') {
243-
/* we ignore all \r's */
244-
if (data[pos] != '\r') luaL_addchar(b, data[pos]);
245+
/* we delay all the characters an iteration so that \r can be skipped */
246+
if (hasprevch)
247+
luaL_addchar(b, prevch);
248+
prevch = data[pos];
249+
hasprevch = 1;
245250
pos++;
246251
}
252+
if (hasprevch && prevch != '\r') {
253+
hasprevch = 0;
254+
luaL_addchar(b, prevch);
255+
}
247256
if (pos < count) { /* found '\n' */
248-
if (!chop)
257+
if (!chop) {
258+
if (hasprevch) /* prevch == '\r' */
259+
luaL_addchar(b, prevch);
249260
luaL_addchar(b, '\n');
261+
}
250262
buffer_skip(buf, pos+1); /* skip '\n' too */
251263
break; /* we are done */
252264
} else /* reached the end of the buffer */

0 commit comments

Comments
 (0)