std::ctype::scan_not(3)

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

πŸ’… NAME πŸ’…

std::ctype::scan_not β€” bestie, it's literally std::ctype::scan_not. That's the whole name, no notes. πŸŽ€

This one lives in the C++ Standard Library and it's from the <locale> girlies ✨


✨ Synopsis ✨

Okay so first things first, this is defined in header <locale>, k? Don't come for me if you forget the include. 🫢

const char* scan_not( mask m, const char* beg, const char* end ) const;

🌸 What it actually does 🌸

It locates the first character in the character array [beg, end) that does not satisfy the classification mask m. That is, the very first character c such that

table()[(unsigned char)c] & m

would return false. Iconic behavior honestly. πŸ‘‘

Lowkey important caveat bestie: if (unsigned char)c >= std::ctype<char>::table_size, then an implementation-defined value gets substituted instead of table()[(unsigned char)c] β€” and it's possibly different for different values of c. So like... don't vibe too hard on that edge case, mwah. πŸ’€

πŸ¦‹ Parameters πŸ¦‹

  • m β€” the mask to search for πŸ’…
  • beg β€” pointer to the first character in an array of characters to search πŸŽ€
  • end β€” the one-past-the-end pointer for the array of characters to search 🌈

⭐ Return value ⭐

A pointer to the first character in [beg, end) that does not satisfy the mask β€” or end if no such character was found. So if everybody in the group chat passes the vibe check, you just get end back. Periodt. 🧚


πŸ’œ Notes πŸ’œ

Unlike the primary template std::ctype, this specialization doesn't perform a virtual function call when classifying characters. No virtual dispatch, no drama, we love to see it. ✨

To customize the behavior, a derived class may provide a non-default classification table to the base class constructor. That's your main character move right there. πŸ”₯


πŸ“ Example πŸ“

// Run this code

#include <iostream>
#include <iterator>
#include <locale>

int main()
{
    auto& f = std::use_facet<std::ctype<char>>(std::locale());

    // skip leading whitespace
    char s1[] = "      \t\t\n  Test";
    const char* p1 = f.scan_not(std::ctype_base::space, std::begin(s1), std::end(s1));
    std::cout << '\'' << p1 << "'\n";

    // skip leading digits
    char s2[] = "123456789abcd";
    const char* p2 = f.scan_not(std::ctype_base::digit, std::begin(s2), std::end(s2));
    std::cout << '\'' << p2 << "'\n";
}

Output:

'Test'
'abcd'

See? We skipped the whitespace, we skipped the digits, and what's left is giving clean string. Ate. πŸ’…πŸ’–


πŸŽ€ See also πŸŽ€

  • do_scan_not β€” locates the first character in a sequence that fails given classification β€” [virtual] (virtual protected member function of std::ctype<CharT>) 🌸
  • scan_is β€” locates the first character in a sequence that conforms to given classification, using the classification table (public member function) πŸ¦‹

Okay that's the whole page bestie, ily, go read your locale facets responsibly 🫢

Source: http://cppreference.com Β· Version: 2024.06.10 Β· Page: std::ctype::scan_not(3) β€” drink water, unplug your charger at 80%, and ngl this doc was kinda iconic. πŸ’œπŸ‘‘

πŸ“¦ where this page comes from