diff --git a/flang/runtime/connection.cpp b/flang/runtime/connection.cpp index 4172a8361857c..0abacd7995b47 100644 --- a/flang/runtime/connection.cpp +++ b/flang/runtime/connection.cpp @@ -14,8 +14,8 @@ namespace Fortran::runtime::io { std::size_t ConnectionState::RemainingSpaceInRecord() const { - auto recl{recordLength.value_or( - executionEnvironment.listDirectedOutputLineLengthLimit)}; + auto recl{recordLength.value_or(openRecl.value_or( + executionEnvironment.listDirectedOutputLineLengthLimit))}; return positionInRecord >= recl ? 0 : recl - positionInRecord; } diff --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp index 079d8aefefad2..aa5ef489d22e7 100644 --- a/flang/runtime/edit-output.cpp +++ b/flang/runtime/edit-output.cpp @@ -477,13 +477,13 @@ bool ListDirectedDefaultCharacterOutput(IoStatementState &io, ok = ok && list.EmitLeadingSpaceOrAdvance(io); // Value is delimited with ' or " marks, and interior // instances of that character are doubled. - ok = ok && io.Emit(&modes.delim, 1); auto EmitOne{[&](char ch) { if (connection.NeedAdvance(1)) { ok = ok && io.AdvanceRecord(); } ok = ok && io.Emit(&ch, 1); }}; + EmitOne(modes.delim); for (std::size_t j{0}; j < length; ++j) { // Doubled delimiters must be put on the same record // in order to be acceptable as list-directed or NAMELIST @@ -502,9 +502,7 @@ bool ListDirectedDefaultCharacterOutput(IoStatementState &io, EmitOne(modes.delim); } else { // Undelimited list-directed output - ok = ok && - list.EmitLeadingSpaceOrAdvance( - io, length > 0 && !list.lastWasUndelimitedCharacter()); + ok = ok && list.EmitLeadingSpaceOrAdvance(io, length > 0 ? 1 : 0, true); std::size_t put{0}; while (ok && put < length) { auto chunk{std::min(length - put, connection.RemainingSpaceInRecord())}; diff --git a/flang/runtime/namelist.cpp b/flang/runtime/namelist.cpp index 96bffa77b4d5a..762b885b56b3b 100644 --- a/flang/runtime/namelist.cpp +++ b/flang/runtime/namelist.cpp @@ -54,9 +54,13 @@ bool IONAME(OutputNamelist)(Cookie cookie, const NamelistGroup &group) { if (!(EmitWithAdvance('&') && EmitUpperCase(group.groupName))) { return false; } + auto *listOutput{io.get_if>()}; for (std::size_t j{0}; j < group.items; ++j) { // [,]ITEM=... const NamelistGroup::Item &item{group.item[j]}; + if (listOutput) { + listOutput->set_lastWasUndelimitedCharacter(false); + } if (!(EmitWithAdvance(j == 0 ? ' ' : comma) && EmitUpperCase(item.name) && EmitWithAdvance('=') && descr::DescriptorIO(io, item.descriptor))) {