258. split_whitespace — Split on Runs, Not on Every Single Space
split(' ') hands you empty strings for every doubled space — and silently ignores tabs. split_whitespace is what you actually meant.
The trap
User input is messy: leading spaces, double spaces, a stray tab. split(' ') takes all of that literally:
| |
Two bugs in one line: every consecutive-space pair produces an empty string, and "alpha\tbeta" sails through as a single “word” because a tab isn’t a space.
The fix
split_whitespace splits on runs of any whitespace and never yields empty strings:
| |
Leading and trailing whitespace disappear too — no trim() needed first.
Unicode-aware, with an ASCII fast path
“Whitespace” here means the Unicode White_Space property, so a non-breaking space (\u{00A0}) splits words just like a regular one:
| |
If your input is guaranteed ASCII (log files, protocol lines), split_ascii_whitespace does the same thing with a cheaper per-byte check — same no-empty-strings guarantee:
| |
Keep split(' ') for formats where empty fields are meaningful (CSV-like, fixed positions). For “give me the words”, it’s split_whitespace every time.