std::strspn(3)

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

πŸ’… std::strspn πŸ’…

okay so bestie, this is std::strspn(3) from the C++ Standard Libary manual, and it's giving β€œfind the longest opening chunk that only uses the characters you said were allowed” energy. iconic behavior, honestly. 🫢


✨ NAME ✨

std::strspn β€” literally std::strspn, no cap. That's the name, periodt. πŸ’…

🌸 Synopsis 🌸

Defined in header <cstring>.

size_t strspn( const char* dest, const char* src );

It returns the length of the maximum initial segment (span) of the byte string pointed to by dest that consists of only the characters found in the byte string pointed to by src. like, it keeps looking at the start of dest and counts how many characters are all from src, and then it stops the second it sees a character that's not in src. πŸ’–

πŸŽ€ Parameters πŸŽ€

  • dest β€” pointer to the null-terminated byte string to be analyzed. this is the main character we're checking, bestie. πŸ‘‘
  • src β€” pointer to the null-terminated byte string that contains the characters to search for. this is the VIP list of allowed characters. ✨

πŸ¦‹ Return value πŸ¦‹

The length of the maximum initial segment that contains only characters from the byte string pointed to by src. that's the count, and it's literally just the length of that allowed opening span. πŸ’―

🌈 Example 🌈

// Run this code

       // Run this code

        #include <cstring>
        #include <iostream>
        #include <string>

        const char* low_alpha = "qwertyuiopasdfghjklzxcvbnm";

        int main()
        {
            std::string s = "abcde312$#@";

            std::size_t spnsz = std::strspn(s.c_str(), low_alpha);
            std::cout << "After skipping initial lowercase letters from '" << s
                      << "'\nThe remainder is '" << s.substr(spnsz) << "'\n";
        }

🌟 Output: 🌟

        After skipping initial lowercase letters from 'abcde312$#@'
        The remainder is '312$#@'

πŸ”₯ See also πŸ”₯

  • strcspn β€” returns the length of the maximum initial segment that consists of only the characters not found in another byte string (function). the opposite vibe, lowkey. πŸ™ˆ
  • wcsspn β€” returns the length of the maximum initial segment that consists of only the wide characters found in another wide string (function). wide-character version, slay. 🌸
  • strpbrk β€” finds the first location of any character from a set of separators (function). it's giving β€œfirst match” energy. ⭐
  • C documentation for strspn β€” yes bestie, the C docs for strspn are right there too. πŸ’œ

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::strspn(3) β€” mwah, that's the footer, periodt. πŸ’‹βœ¨

πŸ“¦ where this page comes from