How hard is it to check for whitespace?

When looking through a piece of source code recently, I stumbled across this method from Guava – a popular Java library from Google.

Guava's whitespace matcher implementation

As you might've guessed from the name, this method checks whether a given character is a whitespace character. Yet the code looks a lot more complex than a simple c == ' ' check. This made me curious and sent me down a small rabbit hole trying to understand where all this complexity was coming from. If you're curious how this code works too, this article is for you.

What is whitespace, anyway?

The first thing one needs when inspecting a function that checks whether a character is whitespace is to define what whitespace actually is. While the SPACE (U+0020) is probably the best-known whitespace character, it's not the only one. If you've been writing code for a while, you can probably name a few more – the non-breaking space NO-BREAK SPACE (U+00A0), a tab CHARACTER TABULATION (U+0009), a newline LINE FEED (U+000A).