Skip to content

Commit

Permalink
[Flang] add space between number and character in print
Browse files Browse the repository at this point in the history
```
print *, "123",456,"789"
```
before
```
123 456789
```
after
```
123 456 789
```

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D145768
  • Loading branch information
Shao-Ce SUN committed Mar 10, 2023
1 parent f902ead commit 8cfdc32
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions flang/runtime/edit-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@

namespace Fortran::runtime::io {

// In output statement, add a space between numbers and characters.
static void addSpaceBeforeCharacter(IoStatementState &io) {
if (auto *list{io.get_if<ListDirectedStatementState<Direction::Output>>()}) {
list->set_lastWasUndelimitedCharacter(false);
}
}

// B/O/Z output of arbitrarily sized data emits a binary/octal/hexadecimal
// representation of what is interpreted to be a single unsigned integer value.
// When used with character data, endianness is exposed.
template <int LOG2_BASE>
static bool EditBOZOutput(IoStatementState &io, const DataEdit &edit,
const unsigned char *data0, std::size_t bytes) {
addSpaceBeforeCharacter(io);
int digits{static_cast<int>((bytes * 8) / LOG2_BASE)};
int get{static_cast<int>(bytes * 8) - digits * LOG2_BASE};
if (get > 0) {
Expand Down Expand Up @@ -100,6 +108,7 @@ static bool EditBOZOutput(IoStatementState &io, const DataEdit &edit,
template <int KIND>
bool EditIntegerOutput(IoStatementState &io, const DataEdit &edit,
common::HostSignedIntType<8 * KIND> n) {
addSpaceBeforeCharacter(io);
char buffer[130], *end{&buffer[sizeof buffer]}, *p{end};
bool isNegative{n < 0};
using Unsigned = common::HostUnsignedIntType<8 * KIND>;
Expand Down Expand Up @@ -255,6 +264,7 @@ decimal::ConversionToDecimalResult RealOutputEditing<binaryPrecision>::Convert(
// 13.7.2.3.3 in F'2018
template <int binaryPrecision>
bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
addSpaceBeforeCharacter(io_);
int editDigits{edit.digits.value_or(0)}; // 'd' field
int editWidth{edit.width.value_or(0)}; // 'w' field
int significantDigits{editDigits};
Expand Down Expand Up @@ -381,6 +391,7 @@ bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
// 13.7.2.3.2 in F'2018
template <int binaryPrecision>
bool RealOutputEditing<binaryPrecision>::EditFOutput(const DataEdit &edit) {
addSpaceBeforeCharacter(io_);
int fracDigits{edit.digits.value_or(0)}; // 'd' field
const int editWidth{edit.width.value_or(0)}; // 'w' field
enum decimal::FortranRounding rounding{edit.modes.round};
Expand Down

0 comments on commit 8cfdc32

Please sign in to comment.