# The C Standard Library Function isspace() Depends on Locale

DevFeed: [The C Standard Library Function isspace() Depends on Locale](<https://devfeed.tech/articles/the-c-standard-library-function-isspace-depends-on-locale-20753.md>)

Original publisher: [Read original article](<https://www.evanjones.ca/isspace_locale.html>)

Published: 2023-06-06T13:41:23Z

Content type: article

Language: en

Sources: [Evan Jones](<https://devfeed.tech/sources/evan-jones.md>)

Topics: [C](<https://devfeed.tech/topics/c.md>), [ASCII](<https://devfeed.tech/topics/ascii.md>), [bug](<https://devfeed.tech/topics/bug.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [ascii](<https://devfeed.tech/tags/ascii.md>), [bug](<https://devfeed.tech/tags/bug.md>), [c](<https://devfeed.tech/tags/c.md>), [function](<https://devfeed.tech/tags/function.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [library](<https://devfeed.tech/tags/library.md>), [locale](<https://devfeed.tech/tags/locale.md>), [mac](<https://devfeed.tech/tags/mac.md>), [mac-os](<https://devfeed.tech/tags/mac-os.md>), [standard](<https://devfeed.tech/tags/standard.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

## AI overview

The article explains that C's isspace() behavior depends on the active locale. In the default C locale it recognizes six ASCII whitespace characters, while other locales may also recognize Unicode whitespace. The author connects this behavior to a parsing bug involving PostgreSQL Hstore values on Mac OS X.

## Source excerpt

This is a post for myself, because I wasted a lot of time understanding this bug, and I want to be able to remember it in the future. I expect close to zero others to be interested. The C standard library function isspace() returns a non-zero value (true) for the six "standard" ASCII white-space characters ('\t', '\n', '\v', '\f', '\r', ' '), and any locale-specific characters. By default, a program starts in the "C" locale, which will only return true for the six ASCII white-space characters. However, if the program changes locales, it can return true for other values. As a result, unless you really understand locales, you should use your own version of this function, or ICU4C's u_isspace() function. An implementation of isspace() for ASCII is one line: /* Returns true for the 6 ASCII white-space characters: \t \n \v \f \r ' '. */ int isspace_ascii(int c) { return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' '; } I ran into this because On Mac OS X, Postgres switches to the system's default locale, which is something that uses UTF-8 (e.g. en_US.UTF-8, fr_CA.UTF-8, etc). In this case, isspace() returns true for Unicode white-space values, which includes 0x85 = NEL = Next Line, and 0xA0 = NBSP = No-Break Space. This caused a bug in parsing Postgres Hstore values that use Unicode. I have attempted to submit a patch to fix this (mailing list post, commitfest entry). For a program to demonstrate the behaviour on different systems, see isspace_locale on Github.