Skip to content

Tolerate trailing/duplicate whitespace in PLY headers#256

Merged
slimbuck merged 6 commits into
mainfrom
mv-ply-header-whitespace
Jun 12, 2026
Merged

Tolerate trailing/duplicate whitespace in PLY headers#256
slimbuck merged 6 commits into
mainfrom
mv-ply-header-whitespace

Conversation

@mvaligursky

@mvaligursky mvaligursky commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

parseHeader tokenized each header line with split(' ') and required element / property lines to have exactly 3 tokens. A space-padded count — e.g. element vertex 258951032 (written by some exporters / large-PLY tools) — or duplicate spaces between tokens produced extra empty tokens, pushing the token count past 3, so the header was rejected with invalid ply header.

Fix:

  • Tokenize with strings[i].split(' ').filter(Boolean), dropping the empty tokens produced by trailing or duplicate spaces.

That's the whole change — one line.

Scope (deliberately minimal):
This tolerates only extra spaces between or after tokens. It does not add support for:

  • whitespace-only / blank header lines
  • leading whitespace on a line (e.g. an indented comment)
  • tabs as separators

Those aren't valid per the PLY spec — every header line is defined as "a carriage-return terminated ASCII string that begins with a keyword" (spec) — so malformed lines like those still throw, as before. Clean headers parse exactly as they did (no behavior change).

Testing:

  • Added a formats.test.mjs case covering a space-padded vertex count and duplicate spaces between tokens.
  • All format tests pass; a clean-header PLY still loads unchanged.

parseHeader tokenized header lines with split(' ') and required element/property
lines to have exactly 3 tokens, so a space-padded vertex count (e.g.
'element vertex 258951032      ', as written by some exporters) was rejected with
'invalid ply header'. Trim and split on whitespace instead, and split lines on
/\r?\n/ so CRLF headers are also accepted. Clean headers parse unchanged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make the PLY header parser more tolerant of non-canonical formatting, specifically duplicate/trailing whitespace in header lines and CRLF (\r\n) line endings, so that PLYs written by a wider range of tools load successfully.

Changes:

  • Update header line splitting to use \r?\n instead of \n.
  • Update per-line tokenization to trim().split(/\s+/) to ignore trailing/duplicate whitespace.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/readers/read-ply.ts Outdated
Comment thread src/lib/readers/read-ply.ts
Comment thread src/lib/readers/read-ply.ts Outdated
…es, add test

- Drop the CRLF line-split change (readPly's magic/end_header scan is LF-only, so it
  was incomplete); revert to splitting on '\n'. Scope is now purely whitespace tolerance.
- Derive each header line from its trimmed form: skip whitespace-only lines and read
  comment text from the trimmed line so leading whitespace no longer truncates it.
- Add a formats test covering a space-padded vertex count, duplicate spaces, a
  whitespace-only line, and a leading-space comment.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/lib/readers/read-ply.ts Outdated
Comment thread test/formats.test.mjs Outdated
@mvaligursky mvaligursky changed the title Tolerate trailing/duplicate whitespace and CRLF in PLY headers Tolerate trailing/duplicate whitespace in PLY headers Jun 9, 2026
Decode the whole encoded buffer and assert the end_header terminator is found,
instead of searching only the first 4096 bytes (which would silently break if the
header ever exceeded that).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@slimbuck slimbuck merged commit 119e10b into main Jun 12, 2026
3 checks passed
@slimbuck slimbuck deleted the mv-ply-header-whitespace branch June 12, 2026 14:58
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.

3 participants