A seemingly simple whitespace test hides two separate complications: Unicode’s definition is broader than the ordinary space, and programming languages do not agree on which characters count. Unicode’s White_Space property contains 25 characters, including tabs, line breaks, thin and figure spaces, and the visibly unusual Ogham Space Mark. Zero Width Space, despite its name, is not among them. Java excludes three non-breaking spaces that Unicode includes; Python accepts those spaces and also treats control character U+001C as whitespace; JavaScript’s regular-expression `\s` rejects U+0085 but accepts U+FEFF. The set can change as Unicode evolves: U+180E, for example, lost its whitespace status in Unicode 6.3, creating compatibility challenges for language runtimes.
Once the desired character set is settled, Guava avoids checking each character against a list by using a perfect hash. It multiplies the character value by a constant, shifts the result to select one of 32 table slots, then compares the stored character with the input to rule out false matches. The table holds all 25 whitespace characters, with duplicates filling unused slots. The multiplier is not unique: a brute-force search can find values that map every member of the set to a distinct slot. Guava’s earlier implementation used a modulo-based approach, but that required division; the hash-table method offers a compact, fast check. The exploration shows how a low-level optimization connects Unicode semantics, runtime compatibility, and an uncommon hashing technique.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.