Skip to content

Commit 7eb8209

Browse files
committed
FIX: WRITE/LINES of STRING! doesn't enforce terminal newline
This brings back compatibility of WRITE/LINES with Rebol2 and Red. Fixes: metaeducation/rebol-issues#2102 metaeducation/rebol-issues#2328
1 parent f9a10bf commit 7eb8209

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/core/p-file.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,31 @@ REBINT Mode_Syms[] = {
280280
***********************************************************************/
281281
{
282282
REBSER *ser;
283+
REBOOL lines = (args & AM_WRITE_LINES) != 0;
284+
REBINT n = 0;
283285

284286
if (IS_BLOCK(data)) {
285287
// Form the values of the block
286288
// !! Could be made more efficient if we broke the FORM
287289
// into 32K chunks for writing.
288290
REB_MOLD mo = {0};
289291
Reset_Mold(&mo);
290-
if (args & AM_WRITE_LINES) {
291-
mo.opts = 1 << MOPT_LINES;
292-
}
292+
if (lines) mo.opts = 1 << MOPT_LINES;
293293
Mold_Value(&mo, data, 0);
294294
Set_String(data, mo.series); // fall into next section
295295
len = SERIES_TAIL(mo.series);
296+
} else if (lines) {
297+
// if there was: WRITE/LINES "string"
298+
// append temporary CRLF on Windows or LF on Posix
299+
// @@ https://github.com/rebol/rebol-issues/issues/2102
300+
#ifdef TO_WINDOWS
301+
Append_Bytes_Len(VAL_SERIES(data), "\r\n", 2);
302+
n = 2;
303+
#else
304+
Append_Byte(VAL_SERIES(data), '\n');
305+
n = 1;
306+
#endif
307+
len += n;
296308
}
297309

298310
// Auto convert string to UTF-8
@@ -306,6 +318,13 @@ REBINT Mode_Syms[] = {
306318
}
307319
file->length = len;
308320
OS_DO_DEVICE(file, RDC_WRITE);
321+
322+
if(n > 0) {
323+
// remove the temporary added newline from the series
324+
len -= n;
325+
SET_STR_END(VAL_SERIES(data), len);
326+
VAL_SERIES(data)->tail = len;
327+
}
309328
}
310329

311330

src/tests/units/port-test.r3

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ Rebol [
4444
--assert [file 51732] = query/mode file [type size]
4545
--assert [type: file size: 51732] = query/mode file [type: size:]
4646
close file
47+
48+
--test-- "write/lines - issue/2102"
49+
;@@ https://github.com/rebol/rebol-issues/issues/2102
50+
write/lines %tmp.txt {a^/}
51+
--assert ["a" ""] = read/lines %tmp.txt
52+
delete %tmp.txt
53+
4754
===end-group===
4855

4956
===start-group=== "console port"

0 commit comments

Comments
 (0)