Skip to content

feat: expose stdin EOF state - #57

Merged
Lellansin merged 2 commits into
masterfrom
feat/scanf-is-eof
Sep 20, 2026
Merged

Lellansin merged 2 commits into
masterfrom
feat/scanf-is-eof

Conversation

@Lellansin

Copy link
Copy Markdown
Owner

Background

Issue #43 reports that callers cannot reliably stop a formatted stdin loop at end-of-file. For formats containing multiple conversions, scanf() returns an array even when every conversion produces null. Arrays are truthy in JavaScript, so a loop that uses the returned array as its condition continues after stdin has ended. The same all-null result can also come from a format mismatch, so inspecting values alone is not a reliable EOF test.

Related to #43.

What changed

  • Add scanf.isEOF() as an opt-in, read-only status query.
  • Track EOF at the readSync() boundary, including both zero-byte reads and the Windows-style EOF error path.
  • Keep every existing scanf() argument and return-value behavior unchanged.
  • Ensure sscanf() does not expose stale stdin EOF state.
  • Preserve the final input byte when a file has no trailing newline, while continuing to trim actual LF and CRLF line endings.
  • Document terminal EOF behavior and the new API in the README.
  • Add the TypeScript declaration for scanf.isEOF().

Example

var scanf = require('scanf');

while (true) {
  var values = scanf('%d %d %d');

  if (scanf.isEOF()) {
    break;
  }

  console.log(values[0], values[1], values[2]);
}

EOF becomes observable only after a read attempts to go past the available input, matching normal stream semantics. An empty line and a format mismatch do not set the EOF state.

Compatibility

This is an additive API. Existing callers receive the same values from scanf() and sscanf(), and no existing parameters are overloaded. The implementation uses ES5-compatible syntax and has been exercised on Node.js 4.9.1.

Verification

  • npm test on the local Node.js runtime: 135 passing
  • npm test in the official node:4.9.1 Linux amd64 container: 135 passing
  • Coverage includes initial state, complete input, repeated EOF reads, empty lines, format mismatches, incomplete records, LF/CRLF handling, files without a trailing newline, Windows-style EOF errors, and sscanf() state isolation.

@Lellansin
Lellansin merged commit 234773c into master Sep 20, 2026
24 checks passed
@Lellansin Lellansin mentioned this pull request Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant