Skip to content

Commit 2bfd2cd

Browse files
sep2 can be an empty string. Closes #5414
1 parent b186024 commit 2bfd2cd

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

R/fwrite.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fwrite = function(x, file="", append=FALSE, quote="auto",
4646
is.list(x),
4747
identical(quote,"auto") || isTRUEorFALSE(quote),
4848
is.character(sep) && length(sep)==1L && (nchar(sep) == 1L || identical(sep, "")),
49-
is.character(sep2) && length(sep2)==3L && nchar(sep2[2L])==1L,
49+
is.character(sep2) && length(sep2)==3L && nchar(sep2[2L])<=1L,
5050
is.character(dec) && length(dec)==1L && nchar(dec) == 1L,
5151
dec != sep, # sep2!=dec and sep2!=sep checked at C level when we know if list columns are present
5252
is.character(eol) && length(eol)==1L,

inst/tests/tests.Rraw

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10964,6 +10964,8 @@ test(1736.08, capture.output(fwrite(DT)), c("A","foo","ba|r","baz"))
1096410964
DT = data.table(A=c("foo","ba|r","baz"), B=list(1:3,1:4,c("fo|o","ba,r","baz"))) # now list column and need to quote
1096510965
test(1736.09, capture.output(fwrite(DT)), c("A,B", "foo,1|2|3", "\"ba|r\",1|2|3|4", "baz,\"fo|o\"|\"ba,r\"|baz"))
1096610966
test(1736.10, capture.output(fwrite(DT,quote=TRUE)), c("\"A\",\"B\"", "\"foo\",1|2|3", "\"ba|r\",1|2|3|4", "\"baz\",\"fo|o\"|\"ba,r\"|\"baz\""))
10967+
test(1736.11, capture.output(fwrite(DT[c(1,3,4)], col.names = FALSE, sep = " ", sep2 = c("","",""))),
10968+
c("1 12345678910 stuvw", "3 7 foobar", "4 910 TRUETRUEFALSE"))
1096710969

1096810970
# any list of same length vector input
1096910971
test(1737.1, fwrite(list()), NULL, warning="fwrite was passed an empty list of no columns")

src/fwriteR.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ void writeList(const void *col, int64_t row, char **pch) {
8585
writer_fun_t *fun = funs[wf];
8686
for (int j=0; j<LENGTH(v); j++) {
8787
(*fun)(data, j, &ch);
88-
*ch++ = sep2;
88+
if (sep2)
89+
*ch++ = sep2;
8990
}
90-
if (LENGTH(v)) ch--; // backup over the last sep2 after the last item
91+
if (LENGTH(v) && sep2) ch--; // backup over the last sep2 after the last item
9192
write_chars(sep2end, &ch);
9293
*pch = ch;
9394
}
@@ -280,7 +281,7 @@ SEXP fwriteR(
280281
args.sep2 = sep2 = *CHAR(STRING_ELT(sep2_Arg, 1));
281282
args.dec = *CHAR(STRING_ELT(dec_Arg,0));
282283

283-
if (!firstListColumn) {
284+
if (!firstListColumn && sep2) {
284285
if (args.verbose) Rprintf(_("No list columns are present. Setting sep2='' otherwise quote='auto' would quote fields containing sep2.\n"));
285286
args.sep2 = sep2 = '\0';
286287
} else {

0 commit comments

Comments
 (0)