Skip to content

Preserve input line endings in output file - #373

Open
wegank wants to merge 1 commit into
algebraic-solving:masterfrom
wegank:preserve-line-endings
Open

wegank wants to merge 1 commit into
algebraic-solving:masterfrom
wegank:preserve-line-endings

Conversation

@wegank

@wegank wegank commented Oct 1, 2026

Copy link
Copy Markdown
Member

Fixes #299.

msolve writes its output file in binary mode, so the output always uses \n line endings, whatever the platform or the input. Users working with \r\n input files then have to run unix2dos on the output themselves.

As suggested in #299, this PR detects the line ending of the input file before core_msolve in src/msolve/main.c. If the input uses \r\n, the output file (-o) is rewritten afterwards with \r\n line endings. This adds no overhead for inputs with \n line endings.

  • The line ending is detected from the first line of the input file.
  • Each \n not already preceded by \r becomes \r\n. The file is copied to a tmpfile() and rewritten in place, so its permissions and any symlink are kept. If the conversion fails, msolve prints a warning and the output keeps \n line endings.
  • Output to stdout (no -o) is not converted.

Tests

The new test test/diff/diff_line-endings.sh runs msolve on the pairs of inputs in input_files/line_endings/, which differ only by their line endings, with default options, -g 2 and -P 1. It checks that:

  • the output for the Unix input contains no \r, and
  • the output for the DOS input is byte-identical to the output for the Unix input with \r\n line endings.

The existing line_endings_support test still checks that the parser reads both inputs identically. make check passes locally on macOS (69/69). This has not been tested on Windows.

AI disclosure

This PR was written with the assistance of generative AI (Claude Code), and reviewed by me.

🤖 Generated with Claude Code

msolve always writes its output file with "\n" line endings, since the
output file is opened in binary mode. Users working with "\r\n" input
files then have to convert the output themselves (e.g. with unix2dos).

The line ending of the input file is now detected from its first line
before the computation. If it is "\r\n", the output file is rewritten
afterwards with "\r\n" line endings. Inputs with "\n" line endings are
unaffected and incur no overhead.

A new test checks that, for each pair of inputs differing only by their
line endings, the output for the DOS input is the output for the Unix
input with "\r\n" line endings.

Fixes algebraic-solving#299.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wegank
wegank marked this pull request as ready for review October 1, 2026 22:38
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.

Preserve input line ending style in output

2 participants