std::wcsstr(3)

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

πŸ’– std::wcsstr(3) πŸ’–

okay bestie, so this one's from the C++ Standard Libary manual β€” yes that's literally how they spell it in the header, not me, don't come for me πŸ’… β€” and it's like... the wide-string version of "where's that substring at". Let's get into it πŸŽ€


✨ NAME ✨

std::wcsstr β€” literally just std::wcsstr, the name is the name, that's the whole NAME section, iconic of them honestly 🫢


✨ Synopsis ✨

It lives in the header <cwchar> πŸ¦‹

const wchar_t* wcsstr( const wchar_t* dest, const wchar_t* src );
     wchar_t* wcsstr(       wchar_t* dest, const wchar_t* src );

So what does it do?? It finds the first occurrence of the wide string src in the wide string that dest points to. That's the whole vibe check πŸ’…

⚠️ Heads up bestie: the terminating null characters are not compared. Not even a little. So don't be out here expecting null-terminator behaviour, because it's giving "I don't see them" 😭

✨ Parameters ✨

  • dest β€” pointer to the null-terminated wide string you wanna examine πŸ” (the thing you're searching in, the main character of this operation)
  • src β€” pointer to the null-terminated wide string you're searching for 🌸 (the little icon you're hunting through the crowd)

✨ Return value ✨

  • Pointer to the first character of the found substring inside dest 🎯
  • Or a null pointer if no such substring is found β€” no match, no pointer, that's the deal πŸ™ˆ
  • Special case alert: if src points to an empty string, then dest is returned. Like, immediately. Empty search = you get the whole thing back, we don't even try πŸ’€

✨ Example ✨

There's a "Run this code" link on the original page btw β€” this is the code that's sitting behind it πŸ‘‘

#include <clocale>
#include <cwchar>
#include <iostream>

int main()
{
    wchar_t const* origin = L"をルフゑ, ベータ, γ‚¬γƒ³γƒž, をルフゑ, ベータ, γ‚¬γƒ³γƒž.";
    wchar_t const* target = L"ベータ";
    wchar_t const* result = origin;

    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout << L"Substring to find: \"" << target << L"\"\n"
               << L"String to search: \"" << origin << L"\"\n\n";

    for (; (result = std::wcsstr(result, target)) != nullptr; ++result)
        std::wcout << L"Found: \"" << result << L"\"\n";
}

look at that loop bestie β€” it keeps calling std::wcsstr on the result and bumping ++result so it doesn't just find the same one forever. that's the trick, that's the whole trick 🧚


✨ Possible output ✨

Substring to find: "ベータ"
String to search: "をルフゑ, ベータ, γ‚¬γƒ³γƒž, をルフゑ, ベータ, γ‚¬γƒ³γƒž."

Found: "ベータ, γ‚¬γƒ³γƒž, をルフゑ, ベータ, γ‚¬γƒ³γƒž."
Found: "ベータ, γ‚¬γƒ³γƒž."

two matches, found in order, slay ✨ note how each found line prints from the match all the way to the end of the string, because that's what a pointer to the first char of the substring is 😌


✨ See also ✨

  • find β€” finds the first occurrence of the given substring (public member function of std::basic_string<CharT,Traits,Allocator>) πŸ’œ the C++ string version, the comfy one
  • strstr β€” finds the first occurrence of a substring of characters (function) πŸ”₯ the narrow-char sibling, same energy, smaller chars
  • wcschr β€” finds the first occurrence of a wide character in a wide string (function) πŸ¦‹ singular, one char only
  • wcsrchr β€” finds the last occurrence of a wide character in a wide string (function) ⭐ note the r β€” it's backwards, it's the last one
  • C documentation for wcsstr β€” the C version is out there too, go look if you want πŸ“š

okay that's the whole page, no cap 🫢 and for the record this came from http://cppreference.com and it's dated 2024.06.10, so if anything's changed since then, blame the calendar not me πŸ˜­πŸ’– go find your substrings bestie, periodt πŸ’…βœ¨

πŸ“¦ where this page comes from