r/programming • • Oct 30 '26

How hard is it to check for whitespace?

https://serce.me/posts/2026-10-30-how-hard-is-it-to-check-for-whitespace
144 Upvotes

34 comments sorted by

71

u/smallproton Oct 30 '26

20

u/SerCeMan Oct 30 '26

Classic. I've never written anything in Whitespace, but I did dabble in https://esolangs.org/wiki/Befunge a long while ago, it was quite literally mindbending

61

u/chucker23n Oct 30 '26
int value = 32 * 1682554634 = -1992826560;

This is why I drink.

43

u/SerCeMan Oct 30 '26

If you pour in too much beer, the glass can overflow too.

5

u/mtranda Oct 31 '26

Overflow? Yes. Have the entire contents of the glass end up on the floor? No. 

12

u/TeraFlint Oct 31 '26

Pythagoras would like to have a word with you.

4

u/mtranda Oct 31 '26

Oh, I actually knew about this one. Well remembered and, more importantly, well played. 

4

u/xhivo Oct 31 '26

It's small and entirely localized so it's mostly fine. You can guess it must be some math with magic numbers to optimize this.

But properly naming them, and adding a comment on the trick being used is helpful for reading it.

9

u/FrancisStokes Oct 31 '26

This is the kind of constant where, if you need a comment to help you, you shouldn't be touching that piece of code.

2

u/nekokattt Oct 31 '26

like the fast squareroot algorithm?

3

u/SkoomaDentist Nov 02 '26

Not like that. Fast square root is very simple as long as the implementation isn’t intentionally obfuscated like all the blog posts use. It’s just an initial guess + one newton raphson iteration.

3

u/Eclipsetech Oct 31 '26

Why 1682554634?

4

u/SerCeMan Oct 31 '26

There is a paragraph on this in the article – basically, I don't know exactly why this number was chosen, but you can easily get a whole array of other numbers that would work just as well.

1

u/Eclipsetech Oct 31 '26

Yeah. I was hoping someone knew. Not a special or fun number. Not the min or max number. Not a unix timestamp. Not a hexspeak code. Doesn't contain distinct digits. It just seems extra random.

16

u/Successful-Money4995 Oct 31 '26

That just happens to be a value that works. They're are others, too. The author does not know why Google picked that one.

11

u/Tuna-Fish2 Oct 31 '26

It's part of a perfect hash function. The way you usually pick those is you just iterate over the set of possibilities and check all values until you hit one that works.

For a typical perfect hash with one multiplication and one shift, it takes like 5 seconds to iterate over the entire 32-bit space to find results.

3

u/Eclipsetech Oct 31 '26

It's not the smallest value to complete the hash. It doesn't have a hexspeak code. It just seems super random.

4

u/Tuna-Fish2 Oct 31 '26

could have started from largest i32 and went down.

4

u/tasty_crayon Oct 31 '26

It's a perfect hash function. They probably just tried a bunch of random operations until it worked and those are the constants and operations they ended up with.

5

u/sweetno Oct 31 '26

Probably it's optimized division by constant. x/y = x × (1/y), and you can use this for integers too. The article mentions that the previous version used division. You need to study number theory to fully understand the trick.

I'm not sure they needed to write it this way, since compilers are supposed to be good at optimizing this.

7

u/guepier Oct 31 '26

compilers are supposed to be good at optimizing this.

Compilers can factor simple constants to optimise multiplication and division, but they don’t run gperf or similar tools to find factors for a perfect hash function. You have to do that yourself.

4

u/sweetno Oct 31 '26

Does anybody know if East-Asian IME produce a space that's different from the ASCII one? I for some time already think that these Unicode whitespace checks are an overkill given that the users are only ever able to type ~4 of these only.

10

u/TripleS941 Oct 31 '26

East Asian IMEs produce two spaces that are different from the ASCII one: full-width and half-width.

And if a user works in publishing, or has some kind of "smart" IME, then all bets are off, any character can come from those.

3

u/vytah Oct 31 '26

Half-width space is the ASCII space.

1

u/f3xjc Nov 01 '26

Fine regular space count as half width.

3

u/xhivo Oct 31 '26

Just measure each glyph that way you always know if there's a zero width character or of there's whitespace with some width etc 🙃

5

u/thesituation531 Oct 31 '26

Only use ASCII. Easy.

1

u/__konrad Oct 31 '26

What about the Character.isSpaceChar which returns true for 00a0?

35

u/Successful-Money4995 Oct 31 '26

Here they are – meticulously copied , , , , , ,…, , , , , , , , , , , , , ,
,
, , , .

Fucking killed me! 😂😂😂

0

u/[deleted] Oct 30 '26

[deleted] — view deleted comment

46

u/RCoder01 Oct 31 '26

So it was never simple, we just choose to ignore it before

7

u/Farlo1 Oct 31 '26

Every ecosystem attempted to solve it separately, thus differently and also incompletely. Microsoft decided that 16bit chars in UCS2 would be enough for a while and it causes me nothing but grief at work. Eventually everyone agreed on UTF but there were still 8, 16, and 32 bit representations for a while. Thank god UTF8 has (mostly) won.

15

u/evaned Nov 01 '26

Microsoft decided that 16bit chars in UCS2 would be enough for a while and it causes me nothing but grief at work.

I mean... that wasn't MS, that was the Unicode Consortium.

MS was a victim of being a very early mover in the Unicode space, because what Unicode is and how it is typically represented got changed out from under them.

They were slow to move from UCS-2 once Unicode expanded, but this isn't a case of them taking their own path or whatever.

3

u/Francois-C Oct 31 '26

Agreed. Even without supporting all the characters from every language in the world, we would still need more than 256 characters today—and therefore characters over one byte.

10

u/Tuna-Fish2 Oct 31 '26

No, there were 25 whitespace characters in the BMP before utf-8.