diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 15:33:17 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 15:35:05 +0000 |
| commit | 8745f421b4cf7e1d54c17a360d46fab889ca12cc (patch) | |
| tree | 4c2a3508627db04493af35643580417d41b63f89 /pseudocode-lib/string.cc | |
| parent | 5e84edbe1a79c6a7752ee276ea6c1a4daef4fa8e (diff) | |
feat(input)!: validate INPUT and read whole lines into STRING
Scalar INPUT now reads one whitespace-delimited token through module-local
IR helpers instead of scanf, then skips spaces and tabs and consumes one
newline. INTEGER, REAL, BOOLEAN (TRUE or FALSE in any case) and CHAR are
validated, and bad input or end of input is a runtime error. STRING INPUT
reads the rest of the line. The unused scanf and REAL printf format globals
are removed.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'pseudocode-lib/string.cc')
| -rw-r--r-- | pseudocode-lib/string.cc | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/pseudocode-lib/string.cc b/pseudocode-lib/string.cc index a4c7343..c47899c 100644 --- a/pseudocode-lib/string.cc +++ b/pseudocode-lib/string.cc @@ -180,16 +180,17 @@ const char *_string_impl_c_str(void *Handle) { int _string_impl_compare(void *A, void *B) { return checked([&] { return text(A).compare(text(B)); }); } +// INPUT of a STRING: the rest of the current line, without its "\n" or "\r\n". void _string_impl_input(void *Handle) { checked([&] { std::string Value; - int Ch; - do { Ch = std::getchar(); } while (Ch != EOF && std::isspace(static_cast<unsigned char>(Ch))); - while (Ch != EOF && !std::isspace(static_cast<unsigned char>(Ch))) { + int Ch = std::getchar(); + if (Ch == EOF) throw std::runtime_error("INPUT reached the end of input"); + while (Ch != EOF && Ch != '\n') { Value.push_back(static_cast<char>(Ch)); Ch = std::getchar(); } - if (Ch != EOF) std::ungetc(Ch, stdin); + if (!Value.empty() && Value.back() == '\r') Value.pop_back(); text(Handle) = std::move(Value); }); } |
