std::wcslen(3)

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

πŸ’… std::wcslen(3) β€” C++ Standard Libary πŸ’…

🌸 NAME 🌸

std::wcslen β€” std::wcslen. Literally the wide-string length girlie, and she's iconic for counting non-null wide chars before the terminating null wide character. No cap.

πŸ¦‹ Synopsis πŸ¦‹

Defined in header <cwchar> β€” write it down bestie.

std::size_t wcslen( const wchar_t* str );

Returns the length of a wide string, that is the number of non-null wide characters that precede the terminating null wide character.

🚨 Behavior check: the behavior is undefined if there is no null character in the wide character array pointed to by str. That's a hard no, fr fr.

πŸŽ€ Parameters πŸŽ€

  • str β€” pointer to the null-terminated wide string to be examined. Yes, she's the main character here.

✨ Return value ✨

The length of the null-terminated wide string str. Periodt.

🧚 Possible implementation 🧚

One possible implementation, looking cute:

std::size_t wcslen(const wchar_t* start)
{
    // NB: start is not checked for nullptr!
    const wchar_t* end = start;
    while (*end != L'\0')
        ++end;
    return end - start;
}

πŸ“ Example πŸ“

// Run this code

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

int main()
{
    const wchar_t* str = L"ηˆ†γœγ‚γƒͺγ‚’γƒ«οΌεΌΎγ‘γ‚γ‚·γƒŠγƒ—γ‚ΉοΌγƒ‘γƒ‹γƒƒγ‚·γƒ₯γƒ‘γƒ³γƒˆγƒ‡γ‚£γ‚Ήγ€γƒ―γƒΌγƒ«γƒ‰οΌ";

    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << "The length of \"" << str << "\" is " << std::wcslen(str) << '\n';
}

🌈 Output 🌈

The length of "ηˆ†γœγ‚γƒͺγ‚’γƒ«οΌεΌΎγ‘γ‚γ‚·γƒŠγƒ—γ‚ΉοΌγƒ‘γƒ‹γƒƒγ‚·γƒ₯γƒ‘γƒ³γƒˆγƒ‡γ‚£γ‚Ήγ€γƒ―γƒΌγƒ«γƒ‰οΌ" is 32

Lowkey obsessed with that output. It ate.

πŸ‘‘ See also πŸ‘‘

  • strlen β€” returns the length of a given string (function)
  • mblen β€” returns the number of bytes in the next multibyte character (function)
  • C documentation for wcslen

🫢 source vibes: http://cppreference.com Β· version/date: 2024.06.10 Β· bye bestie, go count those wide chars πŸ’–
πŸ“¦ where this page comes from