std::ispunct(std::locale)(3)

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

πŸ’… std::ispunct(std::locale)(3) πŸ’…

Okay bestie, welcome to the C++ Standard Libary manual, section 3 β€” yes, that's the manual name, typo and all, no cap. Today's main character is the locale-aware punctuation check: std::ispunct(std::locale). It's giving β€œdoes this character count as punctuation in this locale?” energy, and honestly? Iconic. πŸŽ€


🌸 NAME 🌸

std::ispunct(std::locale) β€” std::ispunct(std::locale). That's the name, bestie. It's the locale-flavored punctuation checker. Periodt. πŸ’–

✨ Synopsis ✨

Defined in header <locale>:

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

So what's the vibe check? It checks if the given character is classified as a punctuation character by the given locale's std::ctype facet. That's the whole assignment. πŸ¦‹

πŸŽ€ Parameters πŸŽ€

  • ch β€” character
  • loc β€” locale

πŸ’– Return value πŸ’–

Returns true if the character is classified as punctuation, false otherwise. Simple as that, bestie. ✨

🧚 Possible implementation 🧚

Here's how it could look under the hood, fr fr:

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

🌈 Example 🌈

This one demonstrates the use of std::ispunct() with different locales (OS-specific). So yes, your mileage may vary depending on the OS, bestie β€” it's not me being dramatic, that's literally in the notes. πŸ™ˆ

// Run this code

#include <iostream>
#include <locale>

int main()
{
    const wchar_t c = L'\u214b'; // upside-down ampersand

    std::locale loc1("C");
    std::cout << "ispunct('β…‹', C locale) returned "
              << std::boolalpha << std::ispunct(c, loc1) << '\n';

    std::locale loc2("en_US.UTF-8");
    std::cout << "ispunct('β…‹', Unicode locale) returned "
              << std::boolalpha << std::ispunct(c, loc2) << '\n';
}

πŸ“ Possible output πŸ“

isalpha('β…‹', C locale) returned false
isalpha('β…‹', Unicode locale) returned true

Yes, the output says isalpha while the example calls ispunct β€” I'm not fixing it, I'm preserving it. The lore is the lore. πŸ’€

πŸ‘‘ See also πŸ‘‘

  • ispunct β€” checks if a character is a punctuation character (function)
  • iswpunct β€” checks if a wide character is a punctuation character (function)

Source/version/date sign-off, bestie: http://cppreference.com Β· 2024.06.10 Β· page std::ispunct(std::locale)(3) πŸ’œβœ¨

πŸ“¦ where this page comes from