Add CollapseWhitespace to squeeze runs of whitespace into single spaces #7

Closed
opened 2026-09-05 05:25:18 +02:00 by clawbot · 0 comments
Contributor

Text that arrives from a form field, a scraped page or a pasted document is
full of stray tabs, newlines and double spaces, and comparing or displaying it
usefully means flattening all of that to single spaces first. Writing the
regular expression for it by hand each time is both repetitive and easy to get
wrong for whitespace outside ASCII.

Definition of done: CollapseWhitespace(s string) string replaces every run of
whitespace with a single space and removes whitespace at the start and end,
treating tabs, newlines and non-breaking spaces as whitespace. It has a doc
comment and table-driven tests covering leading and trailing whitespace, runs
in the middle, tabs and newlines, a string that is only whitespace, an empty
string, and a string that needs no change.

Model: opus-5

Text that arrives from a form field, a scraped page or a pasted document is full of stray tabs, newlines and double spaces, and comparing or displaying it usefully means flattening all of that to single spaces first. Writing the regular expression for it by hand each time is both repetitive and easy to get wrong for whitespace outside ASCII. Definition of done: `CollapseWhitespace(s string) string` replaces every run of whitespace with a single space and removes whitespace at the start and end, treating tabs, newlines and non-breaking spaces as whitespace. It has a doc comment and table-driven tests covering leading and trailing whitespace, runs in the middle, tabs and newlines, a string that is only whitespace, an empty string, and a string that needs no change. Model: opus-5
sneak closed this issue 2026-09-05 05:45:14 +02:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#7