1 //===-- runtime/edit-input.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "edit-input.h"
10 #include "namelist.h"
11 #include "utf.h"
12 #include "flang/Common/real.h"
13 #include "flang/Common/uint128.h"
14 #include <algorithm>
15 #include <cfenv>
16 
17 namespace Fortran::runtime::io {
18 
19 template <int LOG2_BASE>
20 static bool EditBOZInput(
21     IoStatementState &io, const DataEdit &edit, void *n, std::size_t bytes) {
22   std::optional<int> remaining;
23   std::optional<char32_t> next{io.PrepareInput(edit, remaining)};
24   if (*next == '0') {
25     do {
26       next = io.NextInField(remaining, edit);
27     } while (next && *next == '0');
28   }
29   // Count significant digits after any leading white space & zeroes
30   int digits{0};
31   for (; next; next = io.NextInField(remaining, edit)) {
32     char32_t ch{*next};
33     if (ch == ' ' || ch == '\t') {
34       continue;
35     }
36     if (ch >= '0' && ch <= '1') {
37     } else if (LOG2_BASE >= 3 && ch >= '2' && ch <= '7') {
38     } else if (LOG2_BASE >= 4 && ch >= '8' && ch <= '9') {
39     } else if (LOG2_BASE >= 4 && ch >= 'A' && ch <= 'F') {
40     } else if (LOG2_BASE >= 4 && ch >= 'a' && ch <= 'f') {
41     } else {
42       io.GetIoErrorHandler().SignalError(
43           "Bad character '%lc' in B/O/Z input field", ch);
44       return false;
45     }
46     ++digits;
47   }
48   auto significantBytes{static_cast<std::size_t>(digits * LOG2_BASE + 7) / 8};
49   if (significantBytes > bytes) {
50     io.GetIoErrorHandler().SignalError(IostatBOZInputOverflow,
51         "B/O/Z input of %d digits overflows %zd-byte variable", digits, bytes);
52     return false;
53   }
54   // Reset to start of significant digits
55   io.HandleRelativePosition(-digits);
56   remaining.reset();
57   // Make a second pass now that the digit count is known
58   std::memset(n, 0, bytes);
59   int increment{isHostLittleEndian ? -1 : 1};
60   auto *data{reinterpret_cast<unsigned char *>(n) +
61       (isHostLittleEndian ? significantBytes - 1 : 0)};
62   int shift{((digits - 1) * LOG2_BASE) & 7};
63   if (shift + LOG2_BASE > 8) {
64     shift -= 8; // misaligned octal
65   }
66   while (digits > 0) {
67     char32_t ch{*io.NextInField(remaining, edit)};
68     int digit{0};
69     if (ch >= '0' && ch <= '9') {
70       digit = ch - '0';
71     } else if (ch >= 'A' && ch <= 'F') {
72       digit = ch + 10 - 'A';
73     } else if (ch >= 'a' && ch <= 'f') {
74       digit = ch + 10 - 'a';
75     } else {
76       continue;
77     }
78     --digits;
79     if (shift < 0) {
80       shift += 8;
81       if (shift + LOG2_BASE > 8) { // misaligned octal
82         *data |= digit >> (8 - shift);
83       }
84       data += increment;
85     }
86     *data |= digit << shift;
87     shift -= LOG2_BASE;
88   }
89   return true;
90 }
91 
92 static inline char32_t GetDecimalPoint(const DataEdit &edit) {
93   return edit.modes.editingFlags & decimalComma ? char32_t{','} : char32_t{'.'};
94 }
95 
96 // Prepares input from a field, and consumes the sign, if any.
97 // Returns true if there's a '-' sign.
98 static bool ScanNumericPrefix(IoStatementState &io, const DataEdit &edit,
99     std::optional<char32_t> &next, std::optional<int> &remaining) {
100   next = io.PrepareInput(edit, remaining);
101   bool negative{false};
102   if (next) {
103     negative = *next == '-';
104     if (negative || *next == '+') {
105       io.SkipSpaces(remaining);
106       next = io.NextInField(remaining, edit);
107     }
108   }
109   return negative;
110 }
111 
112 bool EditIntegerInput(
113     IoStatementState &io, const DataEdit &edit, void *n, int kind) {
114   RUNTIME_CHECK(io.GetIoErrorHandler(), kind >= 1 && !(kind & (kind - 1)));
115   switch (edit.descriptor) {
116   case DataEdit::ListDirected:
117     if (IsNamelistName(io)) {
118       return false;
119     }
120     break;
121   case 'G':
122   case 'I':
123     break;
124   case 'B':
125     return EditBOZInput<1>(io, edit, n, kind);
126   case 'O':
127     return EditBOZInput<3>(io, edit, n, kind);
128   case 'Z':
129     return EditBOZInput<4>(io, edit, n, kind);
130   case 'A': // legacy extension
131     return EditCharacterInput(io, edit, reinterpret_cast<char *>(n), kind);
132   default:
133     io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
134         "Data edit descriptor '%c' may not be used with an INTEGER data item",
135         edit.descriptor);
136     return false;
137   }
138   std::optional<int> remaining;
139   std::optional<char32_t> next;
140   bool negate{ScanNumericPrefix(io, edit, next, remaining)};
141   common::UnsignedInt128 value{0};
142   bool any{negate};
143   bool overflow{false};
144   for (; next; next = io.NextInField(remaining, edit)) {
145     char32_t ch{*next};
146     if (ch == ' ' || ch == '\t') {
147       if (edit.modes.editingFlags & blankZero) {
148         ch = '0'; // BZ mode - treat blank as if it were zero
149       } else {
150         continue;
151       }
152     }
153     int digit{0};
154     if (ch >= '0' && ch <= '9') {
155       digit = ch - '0';
156     } else {
157       io.GetIoErrorHandler().SignalError(
158           "Bad character '%lc' in INTEGER input field", ch);
159       return false;
160     }
161     static constexpr auto maxu128{~common::UnsignedInt128{0}};
162     static constexpr auto maxu128OverTen{maxu128 / 10};
163     static constexpr int maxLastDigit{
164         static_cast<int>(maxu128 - (maxu128OverTen * 10))};
165     overflow |= value >= maxu128OverTen &&
166         (value > maxu128OverTen || digit > maxLastDigit);
167     value *= 10;
168     value += digit;
169     any = true;
170   }
171   auto maxForKind{common::UnsignedInt128{1} << ((8 * kind) - 1)};
172   overflow |= value >= maxForKind && (value > maxForKind || !negate);
173   if (overflow) {
174     io.GetIoErrorHandler().SignalError(IostatIntegerInputOverflow,
175         "Decimal input overflows INTEGER(%d) variable", kind);
176     return false;
177   }
178   if (negate) {
179     value = -value;
180   }
181   if (any || !io.GetConnectionState().IsAtEOF()) {
182     std::memcpy(n, &value, kind); // a blank field means zero
183   }
184   return any;
185 }
186 
187 // Parses a REAL input number from the input source as a normalized
188 // fraction into a supplied buffer -- there's an optional '-', a
189 // decimal point, and at least one digit.  The adjusted exponent value
190 // is returned in a reference argument.  The returned value is the number
191 // of characters that (should) have been written to the buffer -- this can
192 // be larger than the buffer size and can indicate overflow.  Replaces
193 // blanks with zeroes if appropriate.
194 static int ScanRealInput(char *buffer, int bufferSize, IoStatementState &io,
195     const DataEdit &edit, int &exponent) {
196   std::optional<int> remaining;
197   std::optional<char32_t> next;
198   int got{0};
199   std::optional<int> decimalPoint;
200   auto Put{[&](char ch) -> void {
201     if (got < bufferSize) {
202       buffer[got] = ch;
203     }
204     ++got;
205   }};
206   if (ScanNumericPrefix(io, edit, next, remaining)) {
207     Put('-');
208   }
209   bool bzMode{(edit.modes.editingFlags & blankZero) != 0};
210   if (!next || (!bzMode && *next == ' ')) { // empty/blank field means zero
211     remaining.reset();
212     if (!io.GetConnectionState().IsAtEOF()) {
213       Put('0');
214     }
215     return got;
216   }
217   char32_t decimal{GetDecimalPoint(edit)};
218   char32_t first{*next >= 'a' && *next <= 'z' ? *next + 'A' - 'a' : *next};
219   if (first == 'N' || first == 'I') {
220     // NaN or infinity - convert to upper case
221     // Subtle: a blank field of digits could be followed by 'E' or 'D',
222     for (; next &&
223          ((*next >= 'a' && *next <= 'z') || (*next >= 'A' && *next <= 'Z'));
224          next = io.NextInField(remaining, edit)) {
225       if (*next >= 'a' && *next <= 'z') {
226         Put(*next - 'a' + 'A');
227       } else {
228         Put(*next);
229       }
230     }
231     if (next && *next == '(') { // NaN(...)
232       Put('(');
233       int depth{1};
234       while (true) {
235         next = io.NextInField(remaining, edit);
236         if (depth == 0) {
237           break;
238         } else if (!next) {
239           return 0; // error
240         } else if (*next == '(') {
241           ++depth;
242         } else if (*next == ')') {
243           --depth;
244         }
245         Put(*next);
246       }
247     }
248     exponent = 0;
249   } else if (first == decimal || (first >= '0' && first <= '9') ||
250       (bzMode && (first == ' ' || first == '\t')) || first == 'E' ||
251       first == 'D' || first == 'Q') {
252     Put('.'); // input field is normalized to a fraction
253     auto start{got};
254     for (; next; next = io.NextInField(remaining, edit)) {
255       char32_t ch{*next};
256       if (ch == ' ' || ch == '\t') {
257         if (bzMode) {
258           ch = '0'; // BZ mode - treat blank as if it were zero
259         } else {
260           continue;
261         }
262       }
263       if (ch == '0' && got == start && !decimalPoint) {
264         // omit leading zeroes before the decimal
265       } else if (ch >= '0' && ch <= '9') {
266         Put(ch);
267       } else if (ch == decimal && !decimalPoint) {
268         // the decimal point is *not* copied to the buffer
269         decimalPoint = got - start; // # of digits before the decimal point
270       } else {
271         break;
272       }
273     }
274     if (got == start) {
275       // Nothing but zeroes and maybe a decimal point.  F'2018 requires
276       // at least one digit, but F'77 did not, and a bare "." shows up in
277       // the FCVS suite.
278       Put('0'); // emit at least one digit
279     }
280     if (next &&
281         (*next == 'e' || *next == 'E' || *next == 'd' || *next == 'D' ||
282             *next == 'q' || *next == 'Q')) {
283       // Optional exponent letter.  Blanks are allowed between the
284       // optional exponent letter and the exponent value.
285       io.SkipSpaces(remaining);
286       next = io.NextInField(remaining, edit);
287     }
288     // The default exponent is -kP, but the scale factor doesn't affect
289     // an explicit exponent.
290     exponent = -edit.modes.scale;
291     if (next &&
292         (*next == '-' || *next == '+' || (*next >= '0' && *next <= '9') ||
293             *next == ' ' || *next == '\t')) {
294       bool negExpo{*next == '-'};
295       if (negExpo || *next == '+') {
296         next = io.NextInField(remaining, edit);
297       }
298       for (exponent = 0; next; next = io.NextInField(remaining, edit)) {
299         if (*next >= '0' && *next <= '9') {
300           if (exponent < 10000) {
301             exponent = 10 * exponent + *next - '0';
302           }
303         } else if (*next == ' ' || *next == '\t') {
304           if (bzMode) {
305             exponent = 10 * exponent;
306           }
307         } else {
308           break;
309         }
310       }
311       if (negExpo) {
312         exponent = -exponent;
313       }
314     }
315     if (decimalPoint) {
316       exponent += *decimalPoint;
317     } else {
318       // When no decimal point (or comma) appears in the value, the 'd'
319       // part of the edit descriptor must be interpreted as the number of
320       // digits in the value to be interpreted as being to the *right* of
321       // the assumed decimal point (13.7.2.3.2)
322       exponent += got - start - edit.digits.value_or(0);
323     }
324   } else {
325     // TODO: hex FP input
326     exponent = 0;
327     return 0;
328   }
329   // Consume the trailing ')' of a list-directed or NAMELIST complex
330   // input value.
331   if (edit.descriptor == DataEdit::ListDirectedImaginaryPart) {
332     if (next && (*next == ' ' || *next == '\t')) {
333       next = io.NextInField(remaining, edit);
334     }
335     if (!next) { // NextInField fails on separators like ')'
336       std::size_t byteCount{0};
337       next = io.GetCurrentChar(byteCount);
338       if (next && *next == ')') {
339         io.HandleRelativePosition(byteCount);
340       }
341     }
342   } else if (remaining) {
343     while (next && (*next == ' ' || *next == '\t')) {
344       next = io.NextInField(remaining, edit);
345     }
346     if (next) {
347       return 0; // error: unused nonblank character in fixed-width field
348     }
349   }
350   return got;
351 }
352 
353 static void RaiseFPExceptions(decimal::ConversionResultFlags flags) {
354 #undef RAISE
355 #ifdef feraisexcept // a macro in some environments; omit std::
356 #define RAISE feraiseexcept
357 #else
358 #define RAISE std::feraiseexcept
359 #endif
360   if (flags & decimal::ConversionResultFlags::Overflow) {
361     RAISE(FE_OVERFLOW);
362   }
363   if (flags & decimal::ConversionResultFlags::Inexact) {
364     RAISE(FE_INEXACT);
365   }
366   if (flags & decimal::ConversionResultFlags::Invalid) {
367     RAISE(FE_INVALID);
368   }
369 #undef RAISE
370 }
371 
372 // If no special modes are in effect and the form of the input value
373 // that's present in the input stream is acceptable to the decimal->binary
374 // converter without modification, this fast path for real input
375 // saves time by avoiding memory copies and reformatting of the exponent.
376 template <int PRECISION>
377 static bool TryFastPathRealInput(
378     IoStatementState &io, const DataEdit &edit, void *n) {
379   if (edit.modes.editingFlags & (blankZero | decimalComma)) {
380     return false;
381   }
382   if (edit.modes.scale != 0) {
383     return false;
384   }
385   const char *str{nullptr};
386   std::size_t got{io.GetNextInputBytes(str)};
387   if (got == 0 || str == nullptr ||
388       !io.GetConnectionState().recordLength.has_value()) {
389     return false; // could not access reliably-terminated input stream
390   }
391   const char *p{str};
392   std::int64_t maxConsume{
393       std::min<std::int64_t>(got, edit.width.value_or(got))};
394   const char *limit{str + maxConsume};
395   decimal::ConversionToBinaryResult<PRECISION> converted{
396       decimal::ConvertToBinary<PRECISION>(p, edit.modes.round, limit)};
397   if (converted.flags & (decimal::Invalid | decimal::Overflow)) {
398     return false;
399   }
400   if (edit.digits.value_or(0) != 0) {
401     // Edit descriptor is Fw.d (or other) with d != 0, which
402     // implies scaling
403     const char *q{str};
404     for (; q < limit; ++q) {
405       if (*q == '.' || *q == 'n' || *q == 'N') {
406         break;
407       }
408     }
409     if (q == limit) {
410       // No explicit decimal point, and not NaN/Inf.
411       return false;
412     }
413   }
414   for (; p < limit && (*p == ' ' || *p == '\t'); ++p) {
415   }
416   if (edit.descriptor == DataEdit::ListDirectedImaginaryPart) {
417     // Need to consume a trailing ')' and any white space after
418     if (p >= limit || *p != ')') {
419       return false;
420     }
421     for (++p; p < limit && (*p == ' ' || *p == '\t'); ++p) {
422     }
423   }
424   if (edit.width && p < str + *edit.width) {
425     return false; // unconverted characters remain in fixed width field
426   }
427   // Success on the fast path!
428   *reinterpret_cast<decimal::BinaryFloatingPointNumber<PRECISION> *>(n) =
429       converted.binary;
430   io.HandleRelativePosition(p - str);
431   // Set FP exception flags
432   if (converted.flags != decimal::ConversionResultFlags::Exact) {
433     RaiseFPExceptions(converted.flags);
434   }
435   return true;
436 }
437 
438 template <int KIND>
439 bool EditCommonRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
440   constexpr int binaryPrecision{common::PrecisionOfRealKind(KIND)};
441   if (TryFastPathRealInput<binaryPrecision>(io, edit, n)) {
442     return true;
443   }
444   // Fast path wasn't available or didn't work; go the more general route
445   static constexpr int maxDigits{
446       common::MaxDecimalConversionDigits(binaryPrecision)};
447   static constexpr int bufferSize{maxDigits + 18};
448   char buffer[bufferSize];
449   int exponent{0};
450   int got{ScanRealInput(buffer, maxDigits + 2, io, edit, exponent)};
451   if (got >= maxDigits + 2) {
452     io.GetIoErrorHandler().Crash("EditCommonRealInput: buffer was too small");
453     return false;
454   }
455   if (got == 0) {
456     io.GetIoErrorHandler().SignalError(IostatBadRealInput);
457     return false;
458   }
459   bool hadExtra{got > maxDigits};
460   if (exponent != 0) {
461     buffer[got++] = 'e';
462     if (exponent < 0) {
463       buffer[got++] = '-';
464       exponent = -exponent;
465     }
466     if (exponent > 9999) {
467       exponent = 9999; // will convert to +/-Inf
468     }
469     if (exponent > 999) {
470       int dig{exponent / 1000};
471       buffer[got++] = '0' + dig;
472       int rest{exponent - 1000 * dig};
473       dig = rest / 100;
474       buffer[got++] = '0' + dig;
475       rest -= 100 * dig;
476       dig = rest / 10;
477       buffer[got++] = '0' + dig;
478       buffer[got++] = '0' + (rest - 10 * dig);
479     } else if (exponent > 99) {
480       int dig{exponent / 100};
481       buffer[got++] = '0' + dig;
482       int rest{exponent - 100 * dig};
483       dig = rest / 10;
484       buffer[got++] = '0' + dig;
485       buffer[got++] = '0' + (rest - 10 * dig);
486     } else if (exponent > 9) {
487       int dig{exponent / 10};
488       buffer[got++] = '0' + dig;
489       buffer[got++] = '0' + (exponent - 10 * dig);
490     } else {
491       buffer[got++] = '0' + exponent;
492     }
493   }
494   buffer[got] = '\0';
495   const char *p{buffer};
496   decimal::ConversionToBinaryResult<binaryPrecision> converted{
497       decimal::ConvertToBinary<binaryPrecision>(p, edit.modes.round)};
498   if (hadExtra) {
499     converted.flags = static_cast<enum decimal::ConversionResultFlags>(
500         converted.flags | decimal::Inexact);
501   }
502   if (*p) { // unprocessed junk after value
503     io.GetIoErrorHandler().SignalError(IostatBadRealInput);
504     return false;
505   }
506   *reinterpret_cast<decimal::BinaryFloatingPointNumber<binaryPrecision> *>(n) =
507       converted.binary;
508   // Set FP exception flags
509   if (converted.flags != decimal::ConversionResultFlags::Exact) {
510     if (converted.flags & decimal::ConversionResultFlags::Overflow) {
511       io.GetIoErrorHandler().SignalError(IostatRealInputOverflow);
512       return false;
513     }
514     RaiseFPExceptions(converted.flags);
515   }
516   return true;
517 }
518 
519 template <int KIND>
520 bool EditRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
521   switch (edit.descriptor) {
522   case DataEdit::ListDirected:
523     if (IsNamelistName(io)) {
524       return false;
525     }
526     return EditCommonRealInput<KIND>(io, edit, n);
527   case DataEdit::ListDirectedRealPart:
528   case DataEdit::ListDirectedImaginaryPart:
529   case 'F':
530   case 'E': // incl. EN, ES, & EX
531   case 'D':
532   case 'G':
533     return EditCommonRealInput<KIND>(io, edit, n);
534   case 'B':
535     return EditBOZInput<1>(io, edit, n,
536         common::BitsForBinaryPrecision(common::PrecisionOfRealKind(KIND)) >> 3);
537   case 'O':
538     return EditBOZInput<3>(io, edit, n,
539         common::BitsForBinaryPrecision(common::PrecisionOfRealKind(KIND)) >> 3);
540   case 'Z':
541     return EditBOZInput<4>(io, edit, n,
542         common::BitsForBinaryPrecision(common::PrecisionOfRealKind(KIND)) >> 3);
543   case 'A': // legacy extension
544     return EditCharacterInput(io, edit, reinterpret_cast<char *>(n), KIND);
545   default:
546     io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
547         "Data edit descriptor '%c' may not be used for REAL input",
548         edit.descriptor);
549     return false;
550   }
551 }
552 
553 // 13.7.3 in Fortran 2018
554 bool EditLogicalInput(IoStatementState &io, const DataEdit &edit, bool &x) {
555   switch (edit.descriptor) {
556   case DataEdit::ListDirected:
557     if (IsNamelistName(io)) {
558       return false;
559     }
560     break;
561   case 'L':
562   case 'G':
563     break;
564   default:
565     io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
566         "Data edit descriptor '%c' may not be used for LOGICAL input",
567         edit.descriptor);
568     return false;
569   }
570   std::optional<int> remaining;
571   std::optional<char32_t> next{io.PrepareInput(edit, remaining)};
572   if (next && *next == '.') { // skip optional period
573     next = io.NextInField(remaining, edit);
574   }
575   if (!next) {
576     io.GetIoErrorHandler().SignalError("Empty LOGICAL input field");
577     return false;
578   }
579   switch (*next) {
580   case 'T':
581   case 't':
582     x = true;
583     break;
584   case 'F':
585   case 'f':
586     x = false;
587     break;
588   default:
589     io.GetIoErrorHandler().SignalError(
590         "Bad character '%lc' in LOGICAL input field", *next);
591     return false;
592   }
593   if (remaining) { // ignore the rest of the field
594     io.HandleRelativePosition(*remaining);
595   } else if (edit.descriptor == DataEdit::ListDirected) {
596     while (io.NextInField(remaining, edit)) { // discard rest of field
597     }
598   }
599   return true;
600 }
601 
602 // See 13.10.3.1 paragraphs 7-9 in Fortran 2018
603 template <typename CHAR>
604 static bool EditDelimitedCharacterInput(
605     IoStatementState &io, CHAR *x, std::size_t length, char32_t delimiter) {
606   bool result{true};
607   while (true) {
608     std::size_t byteCount{0};
609     auto ch{io.GetCurrentChar(byteCount)};
610     if (!ch) {
611       if (io.AdvanceRecord()) {
612         continue;
613       } else {
614         result = false; // EOF in character value
615         break;
616       }
617     }
618     io.HandleRelativePosition(byteCount);
619     if (*ch == delimiter) {
620       auto next{io.GetCurrentChar(byteCount)};
621       if (next && *next == delimiter) {
622         // Repeated delimiter: use as character value
623         io.HandleRelativePosition(byteCount);
624       } else {
625         break; // closing delimiter
626       }
627     }
628     if (length > 0) {
629       *x++ = *ch;
630       --length;
631     }
632   }
633   std::fill_n(x, length, ' ');
634   return result;
635 }
636 
637 template <typename CHAR>
638 static bool EditListDirectedCharacterInput(
639     IoStatementState &io, CHAR *x, std::size_t length, const DataEdit &edit) {
640   std::size_t byteCount{0};
641   auto ch{io.GetCurrentChar(byteCount)};
642   if (ch && (*ch == '\'' || *ch == '"')) {
643     io.HandleRelativePosition(byteCount);
644     return EditDelimitedCharacterInput(io, x, length, *ch);
645   }
646   if (IsNamelistName(io) || io.GetConnectionState().IsAtEOF()) {
647     return false;
648   }
649   // Undelimited list-directed character input: stop at a value separator
650   // or the end of the current record.  Subtlety: the "remaining" count
651   // here is a dummy that's used to avoid the interpretation of separators
652   // in NextInField.
653   std::optional<int> remaining{length > 0 ? maxUTF8Bytes : 0};
654   while (std::optional<char32_t> next{io.NextInField(remaining, edit)}) {
655     switch (*next) {
656     case ' ':
657     case '\t':
658     case ',':
659     case ';':
660     case '/':
661       remaining = 0; // value separator: stop
662       break;
663     default:
664       *x++ = *next;
665       remaining = --length > 0 ? maxUTF8Bytes : 0;
666     }
667   }
668   std::fill_n(x, length, ' ');
669   return true;
670 }
671 
672 template <typename CHAR>
673 bool EditCharacterInput(
674     IoStatementState &io, const DataEdit &edit, CHAR *x, std::size_t length) {
675   switch (edit.descriptor) {
676   case DataEdit::ListDirected:
677     return EditListDirectedCharacterInput(io, x, length, edit);
678   case 'A':
679   case 'G':
680     break;
681   case 'B':
682     return EditBOZInput<1>(io, edit, x, length * sizeof *x);
683   case 'O':
684     return EditBOZInput<3>(io, edit, x, length * sizeof *x);
685   case 'Z':
686     return EditBOZInput<4>(io, edit, x, length * sizeof *x);
687   default:
688     io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
689         "Data edit descriptor '%c' may not be used with a CHARACTER data item",
690         edit.descriptor);
691     return false;
692   }
693   const ConnectionState &connection{io.GetConnectionState()};
694   if (connection.IsAtEOF()) {
695     return false;
696   }
697   std::size_t remaining{length};
698   if (edit.width && *edit.width > 0) {
699     remaining = *edit.width;
700   }
701   // When the field is wider than the variable, we drop the leading
702   // characters.  When the variable is wider than the field, there can be
703   // trailing padding.
704   const char *input{nullptr};
705   std::size_t ready{0};
706   // Skip leading bytes.
707   // These bytes don't count towards INQUIRE(IOLENGTH=).
708   std::size_t skip{remaining > length ? remaining - length : 0};
709   // Transfer payload bytes; these do count.
710   while (remaining > 0) {
711     if (ready == 0) {
712       ready = io.GetNextInputBytes(input);
713       if (ready == 0) {
714         if (io.CheckForEndOfRecord()) {
715           std::fill_n(x, length, ' '); // PAD='YES'
716         }
717         return !io.GetIoErrorHandler().InError();
718       }
719     }
720     std::size_t chunk;
721     bool skipping{skip > 0};
722     if (connection.isUTF8) {
723       chunk = MeasureUTF8Bytes(*input);
724       if (skipping) {
725         --skip;
726       } else if (auto ucs{DecodeUTF8(input)}) {
727         *x++ = *ucs;
728         --length;
729       } else if (chunk == 0) {
730         // error recovery: skip bad encoding
731         chunk = 1;
732       }
733       --remaining;
734     } else {
735       if (skipping) {
736         chunk = std::min<std::size_t>(skip, ready);
737         skip -= chunk;
738       } else {
739         chunk = std::min<std::size_t>(remaining, ready);
740         std::memcpy(x, input, chunk);
741         x += chunk;
742         length -= chunk;
743       }
744       remaining -= chunk;
745     }
746     input += chunk;
747     if (!skipping) {
748       io.GotChar(chunk);
749     }
750     io.HandleRelativePosition(chunk);
751     ready -= chunk;
752   }
753   // Pad the remainder of the input variable, if any.
754   std::fill_n(x, length, ' ');
755   return true;
756 }
757 
758 template bool EditRealInput<2>(IoStatementState &, const DataEdit &, void *);
759 template bool EditRealInput<3>(IoStatementState &, const DataEdit &, void *);
760 template bool EditRealInput<4>(IoStatementState &, const DataEdit &, void *);
761 template bool EditRealInput<8>(IoStatementState &, const DataEdit &, void *);
762 template bool EditRealInput<10>(IoStatementState &, const DataEdit &, void *);
763 // TODO: double/double
764 template bool EditRealInput<16>(IoStatementState &, const DataEdit &, void *);
765 
766 template bool EditCharacterInput(
767     IoStatementState &, const DataEdit &, char *, std::size_t);
768 template bool EditCharacterInput(
769     IoStatementState &, const DataEdit &, char16_t *, std::size_t);
770 template bool EditCharacterInput(
771     IoStatementState &, const DataEdit &, char32_t *, std::size_t);
772 
773 } // namespace Fortran::runtime::io
774