# Guava's whitespace check hides a perfect hash function

Original title: How hard is it to check for whitespace?

> Hacker News story 50392733 · language: en · submitted 2026-11-06
> Web version: https://zeli.app/story/50392733
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 125 · by [theanonymousone](<https://news.ycombinator.com/user?id=theanonymousone>) · 💬 33
[original](<https://serce.me/posts/2026-10-30-how-hard-is-it-to-check-for-whitespace>) · [HN discussion](<https://news.ycombinator.com/item?id=50392733>)

## Summary

A look at the surprisingly complex whitespace matcher inside Google's Guava library. Unicode defines 25 whitespace characters, but Java, Python, and JavaScript each disagree on which ones count — Java excludes NO-BREAK SPACE, Python adds FILE SEPARATOR, and JavaScript drops NEXT LINE while accepting ZERO WIDTH NO-BREAK SPACE. Guava skips a linear scan and uses a perfect hash function with a mysterious multiplier to test membership in constant time.

## Key quote

> Despite the name, it's a format character that can indicate a word or line break opportunity without adding a visible gap.

## HN discussion (33 comments)

### [Whitespace](<https://news.ycombinator.com/item?id=50441375>)

> I was here before the first character was carved into stone and I will be here when the last photon is emitted.
>
> I'm also available via: @gmail.com
>
> (please don't write code with `@whitespace` as I get a lot of GitHub notifications)

### [pixelbeat__](<https://news.ycombinator.com/item?id=50438938>)

> I think horizontal white spacing should be distinguished, as that's what most care about with splitting etc.
>
> Have a look at how GNU coreutils classifies each type of spacing (in a cross platform consistent way) at https://github.com/coreutils/coreutils/blob/master/src%2Fsys...

### [smallstepforman](<https://news.ycombinator.com/item?id=50438758>)

> With bitmasks, if they just assumed bit 5 (whatever) as zero=whitespace, they would have saved billions of cpu cycles per device per day. With 32 bit unicode, that gives you 32 unique whitespace chars.
>
> Should have got guys that grew up with the 8-bit micros to write up a spec.

### [exacube](<https://news.ycombinator.com/item?id=50442070>)

> If you want to learn how that magic multiplier value was derived, look up how to derive Perfect Hash Functions for a known set of values.
>
> I'm not sure which method was used for this specific set of values, but from googling it's seen advances in the past decade.

### [efilife](<https://news.ycombinator.com/item?id=50438461>)

> This code is very simple but I still don't really get it. Why is the hashing needed and how is it faster than a regex? Can someone smarter than me explain?

---

## Cite

- Page: https://zeli.app/story/50392733
- Date: 2026-11-06
- Source: serce.me (https://serce.me/posts/2026-10-30-how-hard-is-it-to-check-for-whitespace)
- Hacker News: https://news.ycombinator.com/item?id=50392733

## Other languages

- zh: https://zeli.app/zh/story/50392733 · markdown: https://zeli.app/zh/story/50392733.md
- ja: https://zeli.app/ja/story/50392733 · markdown: https://zeli.app/ja/story/50392733.md
- ko: https://zeli.app/ko/story/50392733 · markdown: https://zeli.app/ko/story/50392733.md
- de: https://zeli.app/de/story/50392733 · markdown: https://zeli.app/de/story/50392733.md
- es: https://zeli.app/es/story/50392733 · markdown: https://zeli.app/es/story/50392733.md
- ru: https://zeli.app/ru/story/50392733 · markdown: https://zeli.app/ru/story/50392733.md
