std::ispunct(3)

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

πŸ’– NAME πŸ’–

std::ispunct β€” literally std::ispunct, bestie. We're in the C++ Standard Libary manual (yes, that's the spelling in the header, don't come for me πŸ’…), page std::ispunct(3). This lil function is your punctuation vibe check.

✨ Synopsis ✨

Defined in header <cctype>

int ispunct( int ch );

Okay so std::ispunct checks if the given character is a punctuation character as classified by the current C locale. The default C locale classifies these characters as punctuation:

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF. No cap, that's the danger zone πŸ’€.

πŸŽ€ Parameters πŸŽ€

  • ch β€” character to classify.

🌸 Return value 🌸

Non-zero value if the character is a punctuation character, zero otherwise. That's it, that's the tea β˜•.

πŸ¦‹ Notes πŸ¦‹

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

bool my_ispunct(char ch)
{
    return std::ispunct(static_cast<unsigned char>(ch));
}

Similarly, they should not be directly used with standard algorithms when the iterator's value type is char or signed char. Convert the value to unsigned char first:

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

πŸ”₯ Example πŸ”₯

Run this code, bestie:

// Run this code
#include <cctype>
#include <clocale>
#include <iostream>

int main()
{
    unsigned char c = '\xd7'; // the character Γ— (multiplication sign) in ISO-8859-1

    std::cout << "ispunct(\'\\xd7\', default C locale) returned "
              << std::boolalpha << (bool)std::ispunct(c) << '\n';

    std::setlocale(LC_ALL, "en_GB.iso88591");
    std::cout << "ispunct(\'\\xd7\', ISO-8859-1 locale) returned "
              << std::boolalpha << (bool)std::ispunct(c) << '\n';
}

🌈 Possible output 🌈

ispunct('\xd7', default C locale) returned false
ispunct('\xd7', ISO-8859-1 locale) returned true

πŸ‘‘ See also πŸ‘‘

  • ispunct(std::locale) β€” checks if a character is classified as punctuation by a locale (function template).
  • iswpunct β€” checks if a wide character is a punctuation character (function).
  • C documentation for ispunct.

πŸ“Š ASCII values πŸ“Š

The original page has a big ASCII values table, and tables are banned in this terminal glow-up, so here's the same data as a list. Each bullet gives decimal, hexadecimal, octal, characters, then the 12 narrow flag values in this exact order: iscntrl, isprint, isspace, isblank, isgraph, ispunct, isalnum, isalpha, isupper, islower, isdigit, isxdigit.

The heading also names these wide-character columns: iswcntrl, iswprint, iswspace, iswblank, iswgraph, iswpunct, iswalnum, iswalpha, iswupper, iswlower, iswdigit, iswxdigit.

The whitespaces group label sits with the 9 and 10–13 rows.

  • 0–8, \x0–\x8, \0–\10, control codes (NUL, etc.) β€” flags in order: β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 9, \x9, \11, tab (\t) β€” flags in order: β‰ 0, 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 10–13, \xA–\xD, \12–\15, (\n, \v, \f, \r) β€” flags in order: β‰ 0, 0, β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 14–31, \xE–\x1F, \16–\37, control codes β€” flags in order: β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 32, \x20, \40, space β€” flags in order: 0, β‰ 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 33–47, \x21–\x2F, \41–\57, !"#$%&'()*+,-./ β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0
  • 48–57, \x30–\x39, \60–\71, 0123456789 β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, 0, β‰ 0, 0, 0, 0, β‰ 0, β‰ 0
  • 58–64, \x3A–\x40, \72–\100, :;<=>?@ β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0
  • 65–70, \x41–\x46, \101–\106, ABCDEF β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, 0, β‰ 0, β‰ 0, β‰ 0, 0, 0, β‰ 0
  • 71–90, \x47–\x5A, \107–\132, GHIJKLMNOPQRSTUVWXYZ β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, 0, β‰ 0, β‰ 0, β‰ 0, 0, 0, 0
  • 91–96, \x5B–\x60, \133–\140 β€” characters:
[\]^_`

flags in order: 0, β‰ 0, 0, 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0

  • 97–102, \x61–\x66, \141–\146, abcdef β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, 0, β‰ 0, β‰ 0, 0, β‰ 0, 0, β‰ 0
  • 103–122, \x67–\x7A, \147–\172, ghijklmnopqrstuvwxyz β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, 0, β‰ 0, β‰ 0, 0, β‰ 0, 0, 0
  • 123–126, \x7B–\x7E, \172–\176, {|}~ β€” flags in order: 0, β‰ 0, 0, 0, β‰ 0, β‰ 0, 0, 0, 0, 0, 0, 0
  • 127, \x7F, \177, backspace character (DEL) β€” flags in order: β‰ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

Okay bestie, that's the whole std::ispunct(3) page from http://cppreference.com, version 2024.06.10, manual std::ispunct(3) β€” go classify some punctuation and stay safe with those unsigned char casts πŸ’–βœ¨ mwah 🫢

πŸ“¦ where this page comes from