std::isspace(3)

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

🌸 std::isspace(3) 🌸

ok so this page is std::isspace, from the C++ Standard Libary manual (yes that's how it's spelled in the source, i'm not touching it πŸ’…), manual section 3. it's literally named after itself, main character energy only.


✨ NAME ✨

std::isspace β€” std::isspace. that's it. that's the name. iconic of it honestly.


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <cctype>
int isspace( int ch );

so the whole vibe is: it checks if the character you handed it is a whitespace character as classified by the currently installed C locale. 🫢

and in the default locale, the whitespace characters are exactly these six:

  • space (0x20, ' ')
  • form feed (0x0c, '\f')
  • line feed (0x0a, '\n')
  • carriage return (0x0d, '\r')
  • horizontal tab (0x09, '\t')
  • vertical tab (0x0b, '\v')
⚠️ and listen. the behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF. like, undefined undefined. do not test it, bestie 😭

πŸŽ€ Parameters πŸŽ€

  • ch β€” the character to classify. that's the whole job. give it a char, get a verdict.

⭐ Return value ⭐

  • Non-zero value if the character is a whitespace character πŸ’–
  • zero otherwise πŸ’€

that's the entire contract, no notes.


πŸ”₯ Notes πŸ”₯

like all the other functions from <cctype>, the behavior of std::isspace is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. so to use these functions safely with plain chars (or signed chars), convert the argument to unsigned char first:

bool my_isspace(char ch)
{
    return std::isspace(static_cast<unsigned char>(ch));
}

same energy for standard algorithms β€” do not use them directly when the iterator's value type is char or signed char. convert the value to unsigned char first, like this:

int count_spaces(const std::string& s)
{
    return std::count_if(s.begin(), s.end(),
                          // static_cast<int(*)(int)>(std::isspace)  // wrong
                          // [](int c){ return std::isspace(c); }    // wrong
                          // [](char c){ return std::isspace(c); }   // wrong
                             [](unsigned char c){ return std::isspace(c); }  // correct
                        );
}

🧚 the vibe check table (no cap) 🧚

the source lays out an ASCII values table whose header lists the columns as:

ASCII values  characters  iscntrl isprint isspace isblank isgraph ispunct
isalnum isalpha isupper islower isdigit isxdigit
decimal hexadecimal octal iswcntrl iswprint iswspace iswblank iswgraph
iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit

each row below carries twelve values, in that header order: iscntrl, isprint, isspace, isblank, isgraph, ispunct, isalnum, isalpha, isupper, islower, isdigit, isxdigit. (the wide twins iswcntrl … iswxdigit are named in the header too, k? πŸ‘‘)

  • ASCII 0–8 β€” decimal 0–8, hex \x0–\x8, octal \0–\10 β€” control codes (NUL, etc.) β†’ β‰ 0 0 0 0 0 0 0 0 0 0 0 0
  • ASCII 9 β€” decimal 9, hex \x9, octal \11 β€” tab (\t), whitespaces β†’ β‰ 0 0 β‰ 0 β‰ 0 0 0 0 0 0 0 0 0
  • ASCII 10–13 β€” decimal 10–13, hex \xA–\xD, octal \12–\15 β€” \n, \v, \f, \r β†’ β‰ 0 0 β‰ 0 0 0 0 0 0 0 0 0 0
  • ASCII 14–31 β€” decimal 14–31, hex \xE–\x1F, octal \16–\37 β€” control codes β†’ β‰ 0 0 0 0 0 0 0 0 0 0 0 0
  • ASCII 32 β€” decimal 32, hex \x20, octal \40 β€” space β†’ 0 β‰ 0 β‰ 0 β‰ 0 0 0 0 0 0 0 0 0
  • ASCII 33–47 β€” decimal 33–47, hex \x21–\x2F, octal \41–\57 β€” !"#$%&'()*+,-./ β†’ 0 β‰ 0 0 0 β‰ 0 β‰ 0 0 0 0 0 0 0
  • ASCII 48–57 β€” decimal 48–57, hex \x30–\x39, octal \60–\71 β€” 0123456789 β†’ 0 β‰ 0 0 0 β‰ 0 0 β‰ 0 0 0 0 β‰ 0 β‰ 0
  • ASCII 58–64 β€” decimal 58–64, hex \x3A–\x40, octal \72–\100 β€” :;<=>?@ β†’ 0 β‰ 0 0 0 β‰ 0 β‰ 0 0 0 0 0 0 0
  • ASCII 65–70 β€” decimal 65–70, hex \x41–\x46, octal \101–\106 β€” ABCDEF β†’ 0 β‰ 0 0 0 β‰ 0 0 β‰ 0 β‰ 0 β‰ 0 0 0 β‰ 0
  • ASCII 71–90 β€” decimal 71–90, hex \x47–\x5A, octal \107–\132 β€” GHIJKLMNOPQRSTUVWXYZ β†’ 0 β‰ 0 0 0 β‰ 0 0 β‰ 0 β‰ 0 β‰ 0 0 0 0
  • ASCII 91–96 β€” decimal 91–96, hex \x5B–\x60, octal \133–\140 β€” [\]^_\`` β†’ 0 β‰ 0 0 0 β‰ 0 β‰ 0 0 0 0 0 0 0`
  • ASCII 97–102 β€” decimal 97–102, hex \x61–\x66, octal \141–\146 β€” abcdef β†’ 0 β‰ 0 0 0 β‰ 0 0 β‰ 0 β‰ 0 0 β‰ 0 0 β‰ 0
  • ASCII 103–122 β€” decimal 103–122, hex \x67–\x7A, octal \147–\172 β€” ghijklmnopqrstuvwxyz β†’ 0 β‰ 0 0 0 β‰ 0 0 β‰ 0 β‰ 0 0 β‰ 0 0 0
  • ASCII 123–126 β€” decimal 123–126, hex \x7B–\x7E, octal \172–\176 β€” {|}~ β†’ 0 β‰ 0 0 0 β‰ 0 β‰ 0 0 0 0 0 0 0
  • ASCII 127 β€” decimal 127, hex \x7F, octal \177 β€” backspace character (DEL) β†’ β‰ 0 0 0 0 0 0 0 0 0 0 0 0

πŸ’œ See also πŸ’œ

  • isspace(std::locale) β€” checks if a character is classified as whitespace by a locale (function template)
  • iswspace β€” checks if a wide character is a space character (function)
  • C documentation for isspace

ok that's the whole page bestie, you're soooo ready for your locale questions now 🌈✨

sign-off: this absolute gem comes from http://cppreference.com, dated 2024.06.10, and it's page std::isspace(3) β€” go forth and static_cast<unsigned char> everything πŸ«ΆπŸ’–πŸŽ€

πŸ“¦ where this page comes from