std::isspace(std::locale)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::isspace(std::locale)(3) πŸ’–

okay so bestie, welcome to the C++ Standard Libary manual, section 3 β€” yes that's how they spell it, don't come for me, i'm just the messenger πŸŽ€ this page is literally all about std::isspace(std::locale) and it's giving "tiny function, big feelings."


✨ NAME ✨

std::isspace(std::locale) β€” literally just std::isspace(std::locale), that's the whole name, she's not hiding anything πŸ’…


🌸 Synopsis 🌸

first things first, she lives in this header, no cap:

Defined in header <locale>

and here's her whole signature, character for character:

template< class CharT >
bool isspace( CharT ch, const locale& loc );

so what does she do?? she checks if the given character is classified as a whitespace character by the given locale's std::ctype facet πŸ¦‹ that's it, that's the vibe check.


πŸŽ€ Parameters πŸŽ€

  • ch β€” the character. your lil guy. the one getting checked πŸ‘€
  • loc β€” the locale. the whole cultural context deciding whether your character counts as space or not, iconic behavior honestly

πŸ’œ Return value πŸ’œ

returns true if the character is classified as a whitespace character, and false otherwise ✨ simple! clean! no drama!


🧚 Possible implementation 🧚

here's how she could be built under the hood, and honestly it's giving one-liner main character energy:

template<class CharT>
bool isspace(CharT ch, const std::locale& loc)
{
    return std::use_facet<std::ctype<CharT>>(loc).is(std::ctype_base::space, ch);
}

🌈 Example 🌈

this one demonstrates std::isspace() with different locales, and heads up β€” it's OS-specific, so your mileage may absolutely vary 🫢

the page literally has a // Run this code button above this bad boy, that's the cppreference special
#include <iostream>
#include <locale>

void try_with(wchar_t c, const char* loc)
{
    std::wcout << "isspace('" << c << "', locale(\"" << loc << "\")) returned "
               << std::boolalpha << std::isspace(c, std::locale(loc)) << '\n';
}

int main()
{
    const wchar_t EM_SPACE = L'\u2003'; // Unicode character 'EM SPACE'
    try_with(EM_SPACE, "C");
    try_with(EM_SPACE, "en_US.UTF8");
}

⭐ Possible output ⭐

look at this little glow-up between locales, we love to see it:

isspace(' ', locale("C")) returned false
isspace(' ', locale("en_US.UTF8")) returned true

like… the C locale said "not my problem" and en_US.UTF8 said "oh she's space space" 😭✨


πŸ¦‹ See also πŸ¦‹

  • isspace β€” checks if a character is a space character (function) πŸ‘‘
  • iswspace β€” checks if a wide character is a space character (function) πŸ’«

okay that's the whole page, you're basically a locale expert now, no cap πŸ’…βœ¨

sign-off time bestie: this lil beauty lives over at http://cppreference.com, dated 2024.06.10, and she's filed under std::isspace(std::locale)(3) β€” mwah, go forth and check your whitespace πŸ“πŸ«ΆπŸ’–

πŸ“¦ where this page comes from