Adds CollapseWhitespace(s string) string, which replaces every run of
whitespace with a single space and removes whitespace at the start and end.
This belongs here because text that comes from a form field, a scraped page or
a pasted document is full of stray tabs, newlines and double spaces, and
flattening it is a step before almost any comparison, display or storage.
Things to know:
It is built on strings.Fields, which splits on whitespace as Unicode
defines it, so a non-breaking space or a line separator counts too. A regular
expression using \s would not have caught those, since Go's regular
expressions treat \s as ASCII only.
The result is always a single line. If a caller needs paragraph breaks kept,
this is the wrong function.
The test file defines the non-breaking space as a code point rather than
pasting the character, so that the interesting test case is visible to
someone reading the source.
The code is in a new file, whitespace.go, so that the ten proposal branches
do not all conflict in the same place.
make test on this branch reports one failure, TestNowUnixMicro. That test
already fails on master and is unrelated to this change.
Model: opus-5
Adds `CollapseWhitespace(s string) string`, which replaces every run of
whitespace with a single space and removes whitespace at the start and end.
This belongs here because text that comes from a form field, a scraped page or
a pasted document is full of stray tabs, newlines and double spaces, and
flattening it is a step before almost any comparison, display or storage.
Things to know:
- It is built on `strings.Fields`, which splits on whitespace as Unicode
defines it, so a non-breaking space or a line separator counts too. A regular
expression using `\s` would not have caught those, since Go's regular
expressions treat `\s` as ASCII only.
- The result is always a single line. If a caller needs paragraph breaks kept,
this is the wrong function.
- The test file defines the non-breaking space as a code point rather than
pasting the character, so that the interesting test case is visible to
someone reading the source.
- The code is in a new file, `whitespace.go`, so that the ten proposal branches
do not all conflict in the same place.
- `make test` on this branch reports one failure, `TestNowUnixMicro`. That test
already fails on `master` and is unrelated to this change.
Model: opus-5
clawbot
self-assigned this 2026-09-05 05:26:30 +02:00
CollapseWhitespace turns every run of whitespace into a single space and
trims the ends, so a block of text becomes one tidy line. Comes with a doc
comment and table-driven tests. (closes#7)
Model: opus-5
sneak
merged commit c656ce964e into master2026-09-05 05:45:12 +02:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Adds
CollapseWhitespace(s string) string, which replaces every run ofwhitespace with a single space and removes whitespace at the start and end.
This belongs here because text that comes from a form field, a scraped page or
a pasted document is full of stray tabs, newlines and double spaces, and
flattening it is a step before almost any comparison, display or storage.
Things to know:
strings.Fields, which splits on whitespace as Unicodedefines it, so a non-breaking space or a line separator counts too. A regular
expression using
\swould not have caught those, since Go's regularexpressions treat
\sas ASCII only.this is the wrong function.
pasting the character, so that the interesting test case is visible to
someone reading the source.
whitespace.go, so that the ten proposal branchesdo not all conflict in the same place.
make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5