2020-02-14 06:41:56 +08:00
|
|
|
//===-- runtime/edit-output.cpp ---------------------------------*- C++ -*-===//
|
2020-01-24 08:59:27 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
#include "edit-output.h"
|
|
|
|
#include "flang/Common/uint128.h"
|
2020-03-13 01:52:29 +08:00
|
|
|
#include <algorithm>
|
2020-01-24 08:59:27 +08:00
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename INT, typename UINT>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool EditIntegerOutput(IoStatementState &io, const DataEdit &edit, INT n) {
|
|
|
|
char buffer[130], *end = &buffer[sizeof buffer], *p = end;
|
|
|
|
bool isNegative{false};
|
|
|
|
if constexpr (std::is_same_v<INT, UINT>) {
|
|
|
|
isNegative = (n >> (8 * sizeof(INT) - 1)) != 0;
|
|
|
|
} else {
|
|
|
|
isNegative = n < 0;
|
|
|
|
}
|
|
|
|
UINT un{static_cast<UINT>(isNegative ? -n : n)};
|
|
|
|
int signChars{0};
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case DataEdit::ListDirected:
|
|
|
|
case 'G':
|
|
|
|
case 'I':
|
|
|
|
if (isNegative || (edit.modes.editingFlags & signPlus)) {
|
2020-03-29 12:00:16 +08:00
|
|
|
signChars = 1; // '-' or '+'
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
while (un > 0) {
|
2020-10-01 03:26:25 +08:00
|
|
|
auto quotient{un / 10u};
|
2020-02-14 06:41:56 +08:00
|
|
|
*--p = '0' + static_cast<int>(un - UINT{10} * quotient);
|
|
|
|
un = quotient;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
for (; un > 0; un >>= 1) {
|
|
|
|
*--p = '0' + (static_cast<int>(un) & 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
for (; un > 0; un >>= 3) {
|
|
|
|
*--p = '0' + (static_cast<int>(un) & 7);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
for (; un > 0; un >>= 4) {
|
|
|
|
int digit = static_cast<int>(un) & 0xf;
|
|
|
|
*--p = digit >= 10 ? 'A' + (digit - 10) : '0' + digit;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().Crash(
|
|
|
|
"Data edit descriptor '%c' may not be used with an INTEGER data item",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
int digits = end - p;
|
|
|
|
int leadingZeroes{0};
|
|
|
|
int editWidth{edit.width.value_or(0)};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (edit.digits && digits <= *edit.digits) { // Iw.m
|
2020-02-14 06:41:56 +08:00
|
|
|
if (*edit.digits == 0 && n == 0) {
|
|
|
|
// Iw.0 with zero value: output field must be blank. For I0.0
|
|
|
|
// and a zero value, emit one blank character.
|
2020-03-29 12:00:16 +08:00
|
|
|
signChars = 0; // in case of SP
|
2020-02-14 06:41:56 +08:00
|
|
|
editWidth = std::max(1, editWidth);
|
|
|
|
} else {
|
|
|
|
leadingZeroes = *edit.digits - digits;
|
|
|
|
}
|
|
|
|
} else if (n == 0) {
|
|
|
|
leadingZeroes = 1;
|
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
int subTotal{signChars + leadingZeroes + digits};
|
|
|
|
int leadingSpaces{std::max(0, editWidth - subTotal)};
|
|
|
|
if (editWidth > 0 && leadingSpaces + subTotal > editWidth) {
|
2020-02-14 06:41:56 +08:00
|
|
|
return io.EmitRepeated('*', editWidth);
|
|
|
|
}
|
|
|
|
if (edit.IsListDirected()) {
|
2021-05-06 02:37:49 +08:00
|
|
|
int total{std::max(leadingSpaces, 1) + subTotal};
|
|
|
|
if (io.GetConnectionState().NeedAdvance(static_cast<std::size_t>(total)) &&
|
2020-02-14 06:41:56 +08:00
|
|
|
!io.AdvanceRecord()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
leadingSpaces = 1;
|
|
|
|
}
|
|
|
|
return io.EmitRepeated(' ', leadingSpaces) &&
|
|
|
|
io.Emit(n < 0 ? "-" : "+", signChars) &&
|
|
|
|
io.EmitRepeated('0', leadingZeroes) && io.Emit(p, digits);
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// Formats the exponent (see table 13.1 for all the cases)
|
|
|
|
const char *RealOutputEditingBase::FormatExponent(
|
|
|
|
int expo, const DataEdit &edit, int &length) {
|
|
|
|
char *eEnd{&exponent_[sizeof exponent_]};
|
|
|
|
char *exponent{eEnd};
|
|
|
|
for (unsigned e{static_cast<unsigned>(std::abs(expo))}; e > 0;) {
|
2020-10-01 03:26:25 +08:00
|
|
|
unsigned quotient{e / 10u};
|
2020-02-14 06:41:56 +08:00
|
|
|
*--exponent = '0' + e - 10 * quotient;
|
|
|
|
e = quotient;
|
|
|
|
}
|
|
|
|
if (edit.expoDigits) {
|
2020-03-29 12:00:16 +08:00
|
|
|
if (int ed{*edit.expoDigits}) { // Ew.dEe with e > 0
|
2020-02-14 06:41:56 +08:00
|
|
|
while (exponent > exponent_ + 2 /*E+*/ && exponent + ed > eEnd) {
|
|
|
|
*--exponent = '0';
|
|
|
|
}
|
|
|
|
} else if (exponent == eEnd) {
|
2020-03-29 12:00:16 +08:00
|
|
|
*--exponent = '0'; // Ew.dE0 with zero-valued exponent
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} else { // ensure at least two exponent digits
|
2020-02-14 06:41:56 +08:00
|
|
|
while (exponent + 2 > eEnd) {
|
|
|
|
*--exponent = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*--exponent = expo < 0 ? '-' : '+';
|
2020-12-16 02:49:01 +08:00
|
|
|
if (edit.expoDigits || edit.IsListDirected() || exponent + 3 == eEnd) {
|
2020-03-29 12:00:16 +08:00
|
|
|
*--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G'
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
length = eEnd - exponent;
|
|
|
|
return exponent;
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
bool RealOutputEditingBase::EmitPrefix(
|
|
|
|
const DataEdit &edit, std::size_t length, std::size_t width) {
|
|
|
|
if (edit.IsListDirected()) {
|
2020-07-02 07:51:44 +08:00
|
|
|
int prefixLength{edit.descriptor == DataEdit::ListDirectedRealPart ? 2
|
|
|
|
: edit.descriptor == DataEdit::ListDirectedImaginaryPart ? 0
|
|
|
|
: 1};
|
2020-02-14 06:41:56 +08:00
|
|
|
int suffixLength{edit.descriptor == DataEdit::ListDirectedRealPart ||
|
|
|
|
edit.descriptor == DataEdit::ListDirectedImaginaryPart
|
|
|
|
? 1
|
|
|
|
: 0};
|
|
|
|
length += prefixLength + suffixLength;
|
|
|
|
ConnectionState &connection{io_.GetConnectionState()};
|
2021-05-06 02:37:49 +08:00
|
|
|
return (!connection.NeedAdvance(length) || io_.AdvanceRecord()) &&
|
2020-02-14 06:41:56 +08:00
|
|
|
io_.Emit(" (", prefixLength);
|
|
|
|
} else if (width > length) {
|
|
|
|
return io_.EmitRepeated(' ', width - length);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
bool RealOutputEditingBase::EmitSuffix(const DataEdit &edit) {
|
|
|
|
if (edit.descriptor == DataEdit::ListDirectedRealPart) {
|
|
|
|
return io_.Emit(edit.modes.editingFlags & decimalComma ? ";" : ",", 1);
|
|
|
|
} else if (edit.descriptor == DataEdit::ListDirectedImaginaryPart) {
|
|
|
|
return io_.Emit(")", 1);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
decimal::ConversionToDecimalResult RealOutputEditing<binaryPrecision>::Convert(
|
|
|
|
int significantDigits, const DataEdit &edit, int flags) {
|
2020-01-24 08:59:27 +08:00
|
|
|
if (edit.modes.editingFlags & signPlus) {
|
|
|
|
flags |= decimal::AlwaysSign;
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
auto converted{decimal::ConvertToDecimal<binaryPrecision>(buffer_,
|
|
|
|
sizeof buffer_, static_cast<enum decimal::DecimalConversionFlags>(flags),
|
|
|
|
significantDigits, edit.modes.round, x_)};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (!converted.str) { // overflow
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.GetIoErrorHandler().Crash(
|
|
|
|
"RealOutputEditing::Convert : buffer size %zd was insufficient",
|
|
|
|
sizeof buffer_);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13.7.2.3.3 in F'2018
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
|
2020-03-29 12:00:16 +08:00
|
|
|
int editDigits{edit.digits.value_or(0)}; // 'd' field
|
|
|
|
int editWidth{edit.width.value_or(0)}; // 'w' field
|
2020-01-24 08:59:27 +08:00
|
|
|
int significantDigits{editDigits};
|
|
|
|
int flags{0};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (editWidth == 0) { // "the processor selects the field width"
|
|
|
|
if (edit.digits.has_value()) { // E0.d
|
|
|
|
editWidth = editDigits + 6; // -.666E+ee
|
|
|
|
} else { // E0
|
2020-01-24 08:59:27 +08:00
|
|
|
flags |= decimal::Minimize;
|
|
|
|
significantDigits =
|
2020-03-29 12:00:16 +08:00
|
|
|
sizeof buffer_ - 5; // sign, NUL, + 3 extra for EN scaling
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool isEN{edit.variation == 'N'};
|
|
|
|
bool isES{edit.variation == 'S'};
|
2020-03-29 12:00:16 +08:00
|
|
|
int scale{isEN || isES ? 1 : edit.modes.scale}; // 'kP' value
|
2020-01-24 08:59:27 +08:00
|
|
|
int zeroesAfterPoint{0};
|
|
|
|
if (scale < 0) {
|
|
|
|
zeroesAfterPoint = -scale;
|
|
|
|
significantDigits = std::max(0, significantDigits - zeroesAfterPoint);
|
|
|
|
} else if (scale > 0) {
|
|
|
|
++significantDigits;
|
|
|
|
scale = std::min(scale, significantDigits + 1);
|
|
|
|
}
|
|
|
|
// In EN editing, multiple attempts may be necessary, so it's in a loop.
|
|
|
|
while (true) {
|
|
|
|
decimal::ConversionToDecimalResult converted{
|
|
|
|
Convert(significantDigits, edit, flags)};
|
2020-06-20 04:32:26 +08:00
|
|
|
if (IsInfOrNaN(converted)) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return EmitPrefix(edit, converted.length, editWidth) &&
|
|
|
|
io_.Emit(converted.str, converted.length) && EmitSuffix(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
if (!IsZero()) {
|
|
|
|
converted.decimalExponent -= scale;
|
|
|
|
}
|
|
|
|
if (isEN && scale < 3 && (converted.decimalExponent % 3) != 0) {
|
|
|
|
// EN mode: boost the scale and significant digits, try again; need
|
|
|
|
// an effective exponent field that's a multiple of three.
|
|
|
|
++scale;
|
|
|
|
++significantDigits;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Format the exponent (see table 13.1 for all the cases)
|
|
|
|
int expoLength{0};
|
|
|
|
const char *exponent{
|
|
|
|
FormatExponent(converted.decimalExponent, edit, expoLength)};
|
|
|
|
int signLength{*converted.str == '-' || *converted.str == '+' ? 1 : 0};
|
|
|
|
int convertedDigits{static_cast<int>(converted.length) - signLength};
|
|
|
|
int zeroesBeforePoint{std::max(0, scale - convertedDigits)};
|
|
|
|
int digitsBeforePoint{std::max(0, scale - zeroesBeforePoint)};
|
|
|
|
int digitsAfterPoint{convertedDigits - digitsBeforePoint};
|
|
|
|
int trailingZeroes{flags & decimal::Minimize
|
|
|
|
? 0
|
|
|
|
: std::max(0,
|
|
|
|
significantDigits - (convertedDigits + zeroesBeforePoint))};
|
|
|
|
int totalLength{signLength + digitsBeforePoint + zeroesBeforePoint +
|
|
|
|
1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes +
|
|
|
|
expoLength};
|
|
|
|
int width{editWidth > 0 ? editWidth : totalLength};
|
|
|
|
if (totalLength > width) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return io_.EmitRepeated('*', width);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
if (totalLength < width && digitsBeforePoint == 0 &&
|
|
|
|
zeroesBeforePoint == 0) {
|
|
|
|
zeroesBeforePoint = 1;
|
|
|
|
++totalLength;
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
return EmitPrefix(edit, totalLength, width) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(converted.str, signLength + digitsBeforePoint) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', zeroesBeforePoint) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(edit.modes.editingFlags & decimalComma ? "," : ".", 1) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', zeroesAfterPoint) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(
|
|
|
|
converted.str + signLength + digitsBeforePoint, digitsAfterPoint) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', trailingZeroes) &&
|
|
|
|
io_.Emit(exponent, expoLength) && EmitSuffix(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13.7.2.3.2 in F'2018
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RealOutputEditing<binaryPrecision>::EditFOutput(const DataEdit &edit) {
|
2020-03-29 12:00:16 +08:00
|
|
|
int fracDigits{edit.digits.value_or(0)}; // 'd' field
|
2020-06-20 04:32:26 +08:00
|
|
|
const int editWidth{edit.width.value_or(0)}; // 'w' field
|
2020-01-24 08:59:27 +08:00
|
|
|
int flags{0};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (editWidth == 0) { // "the processor selects the field width"
|
|
|
|
if (!edit.digits.has_value()) { // F0
|
2020-01-24 08:59:27 +08:00
|
|
|
flags |= decimal::Minimize;
|
2020-03-29 12:00:16 +08:00
|
|
|
fracDigits = sizeof buffer_ - 2; // sign & NUL
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Multiple conversions may be needed to get the right number of
|
|
|
|
// effective rounded fractional digits.
|
2020-06-20 04:32:26 +08:00
|
|
|
int extraDigits{0};
|
2020-01-24 08:59:27 +08:00
|
|
|
while (true) {
|
|
|
|
decimal::ConversionToDecimalResult converted{
|
|
|
|
Convert(extraDigits + fracDigits, edit, flags)};
|
2020-06-20 04:32:26 +08:00
|
|
|
if (IsInfOrNaN(converted)) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return EmitPrefix(edit, converted.length, editWidth) &&
|
|
|
|
io_.Emit(converted.str, converted.length) && EmitSuffix(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-10-01 04:04:44 +08:00
|
|
|
int scale{IsZero() ? 1 : edit.modes.scale}; // kP
|
|
|
|
int expo{converted.decimalExponent + scale};
|
2020-06-20 04:32:26 +08:00
|
|
|
if (expo > extraDigits && extraDigits >= 0) {
|
2020-01-24 08:59:27 +08:00
|
|
|
extraDigits = expo;
|
2020-06-20 04:32:26 +08:00
|
|
|
if (!edit.digits.has_value()) { // F0
|
2020-03-29 12:00:16 +08:00
|
|
|
fracDigits = sizeof buffer_ - extraDigits - 2; // sign & NUL
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-06-20 04:32:26 +08:00
|
|
|
continue;
|
|
|
|
} else if (expo < extraDigits && extraDigits > -fracDigits) {
|
|
|
|
extraDigits = std::max(expo, -fracDigits);
|
|
|
|
continue;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
int signLength{*converted.str == '-' || *converted.str == '+' ? 1 : 0};
|
|
|
|
int convertedDigits{static_cast<int>(converted.length) - signLength};
|
|
|
|
int digitsBeforePoint{std::max(0, std::min(expo, convertedDigits))};
|
|
|
|
int zeroesBeforePoint{std::max(0, expo - digitsBeforePoint)};
|
2020-06-20 04:32:26 +08:00
|
|
|
int zeroesAfterPoint{std::min(fracDigits, std::max(0, -expo))};
|
2020-01-24 08:59:27 +08:00
|
|
|
int digitsAfterPoint{convertedDigits - digitsBeforePoint};
|
|
|
|
int trailingZeroes{flags & decimal::Minimize
|
|
|
|
? 0
|
|
|
|
: std::max(0, fracDigits - (zeroesAfterPoint + digitsAfterPoint))};
|
|
|
|
if (digitsBeforePoint + zeroesBeforePoint + zeroesAfterPoint +
|
|
|
|
digitsAfterPoint + trailingZeroes ==
|
|
|
|
0) {
|
2020-06-20 04:32:26 +08:00
|
|
|
zeroesBeforePoint = 1; // "." -> "0."
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
int totalLength{signLength + digitsBeforePoint + zeroesBeforePoint +
|
|
|
|
1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes};
|
|
|
|
int width{editWidth > 0 ? editWidth : totalLength};
|
|
|
|
if (totalLength > width) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return io_.EmitRepeated('*', width);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
if (totalLength < width && digitsBeforePoint + zeroesBeforePoint == 0) {
|
|
|
|
zeroesBeforePoint = 1;
|
|
|
|
++totalLength;
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
return EmitPrefix(edit, totalLength, width) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(converted.str, signLength + digitsBeforePoint) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', zeroesBeforePoint) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(edit.modes.editingFlags & decimalComma ? "," : ".", 1) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', zeroesAfterPoint) &&
|
2020-01-24 08:59:27 +08:00
|
|
|
io_.Emit(
|
|
|
|
converted.str + signLength + digitsBeforePoint, digitsAfterPoint) &&
|
2020-02-05 08:55:45 +08:00
|
|
|
io_.EmitRepeated('0', trailingZeroes) &&
|
|
|
|
io_.EmitRepeated(' ', trailingBlanks_) && EmitSuffix(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13.7.5.2.3 in F'2018
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
DataEdit RealOutputEditing<binaryPrecision>::EditForGOutput(DataEdit edit) {
|
2020-01-24 08:59:27 +08:00
|
|
|
edit.descriptor = 'E';
|
2020-10-01 06:04:43 +08:00
|
|
|
int significantDigits{
|
|
|
|
edit.digits.value_or(BinaryFloatingPoint::decimalPrecision)}; // 'd'
|
|
|
|
if (!edit.width.has_value() || (*edit.width > 0 && significantDigits == 0)) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return edit; // Gw.0 -> Ew.0 for w > 0
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-10-01 06:04:43 +08:00
|
|
|
decimal::ConversionToDecimalResult converted{
|
|
|
|
Convert(significantDigits, edit)};
|
2020-06-20 04:32:26 +08:00
|
|
|
if (IsInfOrNaN(converted)) {
|
2020-01-24 08:59:27 +08:00
|
|
|
return edit;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
int expo{IsZero() ? 1 : converted.decimalExponent}; // 's'
|
2020-01-24 08:59:27 +08:00
|
|
|
if (expo < 0 || expo > significantDigits) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return edit; // Ew.d
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
edit.descriptor = 'F';
|
2020-03-29 12:00:16 +08:00
|
|
|
edit.modes.scale = 0; // kP is ignored for G when no exponent field
|
2020-01-24 08:59:27 +08:00
|
|
|
trailingBlanks_ = 0;
|
|
|
|
int editWidth{edit.width.value_or(0)};
|
|
|
|
if (editWidth > 0) {
|
|
|
|
int expoDigits{edit.expoDigits.value_or(0)};
|
2020-03-29 12:00:16 +08:00
|
|
|
trailingBlanks_ = expoDigits > 0 ? expoDigits + 2 : 4; // 'n'
|
2020-01-24 08:59:27 +08:00
|
|
|
*edit.width = std::max(0, editWidth - trailingBlanks_);
|
|
|
|
}
|
|
|
|
if (edit.digits.has_value()) {
|
|
|
|
*edit.digits = std::max(0, *edit.digits - expo);
|
|
|
|
}
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// 13.10.4 in F'2018
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RealOutputEditing<binaryPrecision>::EditListDirectedOutput(
|
|
|
|
const DataEdit &edit) {
|
|
|
|
decimal::ConversionToDecimalResult converted{Convert(1, edit)};
|
2020-06-20 04:32:26 +08:00
|
|
|
if (IsInfOrNaN(converted)) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return EditEorDOutput(edit);
|
|
|
|
}
|
|
|
|
int expo{converted.decimalExponent};
|
|
|
|
if (expo < 0 || expo > BinaryFloatingPoint::decimalPrecision) {
|
|
|
|
DataEdit copy{edit};
|
2020-03-29 12:00:16 +08:00
|
|
|
copy.modes.scale = 1; // 1P
|
2020-02-05 08:55:45 +08:00
|
|
|
return EditEorDOutput(copy);
|
|
|
|
}
|
|
|
|
return EditFOutput(edit);
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
// 13.7.5.2.6 in F'2018
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RealOutputEditing<binaryPrecision>::EditEXOutput(const DataEdit &) {
|
|
|
|
io_.GetIoErrorHandler().Crash(
|
2020-03-29 12:00:16 +08:00
|
|
|
"EX output editing is not yet implemented"); // TODO
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int binaryPrecision>
|
2020-02-05 08:55:45 +08:00
|
|
|
bool RealOutputEditing<binaryPrecision>::Edit(const DataEdit &edit) {
|
2020-01-24 08:59:27 +08:00
|
|
|
switch (edit.descriptor) {
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'D':
|
|
|
|
return EditEorDOutput(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
case 'E':
|
|
|
|
if (edit.variation == 'X') {
|
|
|
|
return EditEXOutput(edit);
|
|
|
|
} else {
|
|
|
|
return EditEorDOutput(edit);
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'F':
|
|
|
|
return EditFOutput(edit);
|
2020-01-24 08:59:27 +08:00
|
|
|
case 'B':
|
|
|
|
case 'O':
|
|
|
|
case 'Z':
|
2020-05-05 02:41:39 +08:00
|
|
|
return EditIntegerOutput(io_, edit,
|
|
|
|
decimal::BinaryFloatingPointNumber<binaryPrecision>{x_}.raw());
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
|
|
|
return Edit(EditForGOutput(edit));
|
2020-01-24 08:59:27 +08:00
|
|
|
default:
|
2020-02-05 08:55:45 +08:00
|
|
|
if (edit.IsListDirected()) {
|
|
|
|
return EditListDirectedOutput(edit);
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
io_.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
2020-02-05 08:55:45 +08:00
|
|
|
"Data edit descriptor '%c' may not be used with a REAL data item",
|
2020-01-24 08:59:27 +08:00
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
bool ListDirectedLogicalOutput(IoStatementState &io,
|
|
|
|
ListDirectedStatementState<Direction::Output> &list, bool truth) {
|
2021-05-06 02:37:49 +08:00
|
|
|
return list.EmitLeadingSpaceOrAdvance(io) && io.Emit(truth ? "T" : "F", 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditLogicalOutput(IoStatementState &io, const DataEdit &edit, bool truth) {
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case 'L':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
2020-07-18 02:14:28 +08:00
|
|
|
return io.EmitRepeated(' ', std::max(0, edit.width.value_or(1) - 1)) &&
|
|
|
|
io.Emit(truth ? "T" : "F", 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used with a LOGICAL data item",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListDirectedDefaultCharacterOutput(IoStatementState &io,
|
|
|
|
ListDirectedStatementState<Direction::Output> &list, const char *x,
|
|
|
|
std::size_t length) {
|
2021-05-06 02:37:49 +08:00
|
|
|
bool ok{true};
|
2020-02-14 06:41:56 +08:00
|
|
|
MutableModes &modes{io.mutableModes()};
|
|
|
|
ConnectionState &connection{io.GetConnectionState()};
|
|
|
|
if (modes.delim) {
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && list.EmitLeadingSpaceOrAdvance(io);
|
2020-02-14 06:41:56 +08:00
|
|
|
// Value is delimited with ' or " marks, and interior
|
|
|
|
// instances of that character are doubled. When split
|
|
|
|
// over multiple lines, delimit each lines' part.
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.Emit(&modes.delim, 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
for (std::size_t j{0}; j < length; ++j) {
|
2021-05-06 02:37:49 +08:00
|
|
|
if (connection.NeedAdvance(2)) {
|
|
|
|
ok = ok && io.Emit(&modes.delim, 1) && io.AdvanceRecord() &&
|
2020-02-14 06:41:56 +08:00
|
|
|
io.Emit(&modes.delim, 1);
|
|
|
|
}
|
|
|
|
if (x[j] == modes.delim) {
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.EmitRepeated(modes.delim, 2);
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.Emit(&x[j], 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.Emit(&modes.delim, 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
|
|
|
// Undelimited list-directed output
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok &&
|
|
|
|
list.EmitLeadingSpaceOrAdvance(
|
|
|
|
io, length > 0 && !list.lastWasUndelimitedCharacter());
|
2020-02-14 06:41:56 +08:00
|
|
|
std::size_t put{0};
|
2021-05-06 02:37:49 +08:00
|
|
|
while (ok && put < length) {
|
2020-02-14 06:41:56 +08:00
|
|
|
auto chunk{std::min(length - put, connection.RemainingSpaceInRecord())};
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.Emit(x + put, chunk);
|
2020-02-14 06:41:56 +08:00
|
|
|
put += chunk;
|
|
|
|
if (put < length) {
|
2021-05-06 02:37:49 +08:00
|
|
|
ok = ok && io.AdvanceRecord() && io.Emit(" ", 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
list.set_lastWasUndelimitedCharacter(true);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditDefaultCharacterOutput(IoStatementState &io, const DataEdit &edit,
|
|
|
|
const char *x, std::size_t length) {
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case 'A':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used with a CHARACTER data item",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int len{static_cast<int>(length)};
|
|
|
|
int width{edit.width.value_or(len)};
|
|
|
|
return io.EmitRepeated(' ', std::max(0, width - len)) &&
|
|
|
|
io.Emit(x, std::min(width, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
template bool EditIntegerOutput<std::int64_t, std::uint64_t>(
|
|
|
|
IoStatementState &, const DataEdit &, std::int64_t);
|
|
|
|
template bool EditIntegerOutput<common::uint128_t, common::uint128_t>(
|
|
|
|
IoStatementState &, const DataEdit &, common::uint128_t);
|
|
|
|
|
2020-10-03 03:39:05 +08:00
|
|
|
template class RealOutputEditing<2>;
|
|
|
|
template class RealOutputEditing<3>;
|
|
|
|
template class RealOutputEditing<4>;
|
2020-02-14 06:41:56 +08:00
|
|
|
template class RealOutputEditing<8>;
|
2020-10-03 03:39:05 +08:00
|
|
|
template class RealOutputEditing<10>;
|
|
|
|
// TODO: double/double
|
|
|
|
template class RealOutputEditing<16>;
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|