Skip to content

Commit

Permalink
FIX: WRITE/LINES of STRING! doesn't enforce terminal newline
Browse files Browse the repository at this point in the history
This brings back compatibility of WRITE/LINES with Rebol2 and Red.

Fixes:
metaeducation/rebol-issues#2102
metaeducation/rebol-issues#2328
  • Loading branch information
Oldes committed Dec 18, 2018
1 parent f9a10bf commit 7eb8209
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/core/p-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,31 @@ REBINT Mode_Syms[] = {
***********************************************************************/
{
REBSER *ser;
REBOOL lines = (args & AM_WRITE_LINES) != 0;
REBINT n = 0;

if (IS_BLOCK(data)) {
// Form the values of the block
// !! Could be made more efficient if we broke the FORM
// into 32K chunks for writing.
REB_MOLD mo = {0};
Reset_Mold(&mo);
if (args & AM_WRITE_LINES) {
mo.opts = 1 << MOPT_LINES;
}
if (lines) mo.opts = 1 << MOPT_LINES;
Mold_Value(&mo, data, 0);
Set_String(data, mo.series); // fall into next section
len = SERIES_TAIL(mo.series);
} else if (lines) {
// if there was: WRITE/LINES "string"
// append temporary CRLF on Windows or LF on Posix
// @@ https://github.com/rebol/rebol-issues/issues/2102
#ifdef TO_WINDOWS
Append_Bytes_Len(VAL_SERIES(data), "\r\n", 2);
n = 2;
#else
Append_Byte(VAL_SERIES(data), '\n');
n = 1;
#endif
len += n;
}

// Auto convert string to UTF-8
Expand All @@ -306,6 +318,13 @@ REBINT Mode_Syms[] = {
}
file->length = len;
OS_DO_DEVICE(file, RDC_WRITE);

if(n > 0) {
// remove the temporary added newline from the series
len -= n;
SET_STR_END(VAL_SERIES(data), len);
VAL_SERIES(data)->tail = len;
}
}


Expand Down
7 changes: 7 additions & 0 deletions src/tests/units/port-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ Rebol [
--assert [file 51732] = query/mode file [type size]
--assert [type: file size: 51732] = query/mode file [type: size:]
close file

--test-- "write/lines - issue/2102"
;@@ https://github.com/rebol/rebol-issues/issues/2102
write/lines %tmp.txt {a^/}
--assert ["a" ""] = read/lines %tmp.txt
delete %tmp.txt

===end-group===

===start-group=== "console port"
Expand Down

0 comments on commit 7eb8209

Please sign in to comment.