std::ispunct(3)
π 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 ofchis not representable asunsigned charand is not equal toEOF. 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,09,\x9,\11, tab (\t) β flags in order:β 0,0,β 0,β 0,0,0,0,0,0,0,0,010β13,\xAβ\xD,\12β\15, (\n,\v,\f,\r) β flags in order:β 0,0,β 0,0,0,0,0,0,0,0,0,014β31,\xEβ\x1F,\16β\37, control codes β flags in order:β 0,0,0,0,0,0,0,0,0,0,0,032,\x20,\40, space β flags in order:0,β 0,β 0,β 0,0,0,0,0,0,0,0,033β47,\x21β\x2F,\41β\57,!"#$%&'()*+,-./β flags in order:0,β 0,0,0,β 0,β 0,0,0,0,0,0,048β57,\x30β\x39,\60β\71,0123456789β flags in order:0,β 0,0,0,β 0,0,β 0,0,0,0,β 0,β 058β64,\x3Aβ\x40,\72β\100,:;<=>?@β flags in order:0,β 0,0,0,β 0,β 0,0,0,0,0,0,065β70,\x41β\x46,\101β\106,ABCDEFβ flags in order:0,β 0,0,0,β 0,0,β 0,β 0,β 0,0,0,β 071β90,\x47β\x5A,\107β\132,GHIJKLMNOPQRSTUVWXYZβ flags in order:0,β 0,0,0,β 0,0,β 0,β 0,β 0,0,0,091β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,β 0103β122,\x67β\x7A,\147β\172,ghijklmnopqrstuvwxyzβ flags in order:0,β 0,0,0,β 0,0,β 0,β 0,0,β 0,0,0123β126,\x7Bβ\x7E,\172β\176,{|}~β flags in order:0,β 0,0,0,β 0,β 0,0,0,0,0,0,0127,\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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms