std::wcscspn(3)

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

πŸ’œ std::wcscspn(3) β€” the C++ Standard Library page, but make it make sense

Okay bestie, so this is the manual page for std::wcscspn from the C++ Standard Library, and I promise it's way less scary than the name looks 🫢 (wcscspn is literally just "wide char span" energy, iykyk).


✨ NAME ✨

std::wcscspn β€” yes that's the whole name, she's self-titled, main character energy πŸ‘‘


πŸŽ€ Synopsis πŸŽ€

Defined in header <cwchar> πŸ’–

std::size_t wcscspn( const wchar_t* dest, const wchar_t* src );

So here's the vibe: it returns the length of the maximum initial segment of the wide string pointed to by dest that consists of only the characters not found in the wide string pointed to by src.

Read that again, because the "not" is doing so much work 😭 it counts how far you can walk through dest before you bump into anything from src. Periodt.


🌸 Parameters 🌸

  • dest β€” pointer to the null-terminated wide string to be analyzed πŸ“
  • src β€” pointer to the null-terminated wide string that contains the characters to search for πŸ”₯
Both of these are null-terminated wide strings, so nobody's out here guessing lengths, okay? They just keep reading until the null. Slay but also be careful πŸ™ˆ

🧚 Return value 🧚

The length of the maximum initial segment that contains only characters not found in the character string pointed to by src ✨

That's it. That's the whole return. No cap.


πŸ¦‹ Example πŸ¦‹

The output below was obtained using clang (libc++). πŸ’…

(the page literally has a // Run this code line right here, so here it is, verbatim, and then the code:)

// Run this code

#include <cwchar>
#include <iostream>
#include <locale>

int main()
{
    wchar_t dest[] = L"η™½ηŒ« ι»‘η‹— η”²θ™«";
    //                      └───┐
    const wchar_t* src = L"η”²θ™«,ι»‘η‹—";

    const std::size_t len = std::wcscspn(dest, src);
    dest[len] = L'\0'; // terminates the segment to print it out

    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << L"The length of maximum initial segment is " << len
<< L".\n";
    std::wcout << L"The segment is \"" << dest << L"\".\n";
}

⭐ Possible output ⭐

The length of maximum initial segment is 3.
The segment is "η™½ηŒ« ".

Not me getting emotional over a L'\0' being used to slam a terminator into place so the segment can be printed 😭 iconic behaviour honestly.


🌈 See also 🌈

  • wcsspn β€” returns the length of the maximum initial segment that consists of only the wide characters found in another wide string (function) πŸŽ€ (yes she's the mirror-image sister, opposite energy, obsessed)
  • wcspbrk β€” finds the first location of any wide character in one wide string, in another wide string (function) πŸ”
  • C documentation for wcscspn πŸ“š

Alright bestie, that's the whole page! This one came from http://cppreference.com, dated 2024.06.10, and it's the std::wcscspn(3) page πŸ’– go forth and span those wide chars responsibly, ily πŸŒΈπŸ¦‹πŸ‘‘

πŸ“¦ where this page comes from