std::wcsncat(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’… std::wcsncat

okay bestie, welcome to the std::wcsncat page from the C++ Standard Library โ€” she's a little wide-string appender and honestly she ate ๐Ÿ’–


โœจ NAME โœจ

std::wcsncat โ€” yes that's literally it, the name is the name, we love a self-titled icon ๐Ÿ‘‘


๐ŸŒธ Synopsis ๐ŸŒธ

She lives in the header <cwchar>, as all the wide-char girlies do:

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

So here's the vibe check: she appends at most count wide characters from the wide string pointed to by src onto the end of the character string pointed to by dest โ€” and she stops early if the null terminator gets copied. ๐Ÿ›‘

The way that src[0] just replaces the null terminator sitting at the end of destโ€ฆ main character energy, fr fr.

And the null terminator? Always appended at the end, no exceptions. So the maximum number of wide characters this function may write is count + 1. Math girlie. ๐Ÿงฎโœจ

โš ๏ธ Lowkey important, ngl: the behavior is undefined if the destination array isn't large enough to hold the contents of both src and dest plus the terminating null wide character. Like, she will not check for you. She trusts you. Do not betray her. ๐Ÿ˜ญ
โš ๏ธ Also undefined if the strings overlap. Justโ€ฆ don't. ๐Ÿ™ˆ

๐ŸŽ€ Parameters ๐ŸŽ€

  • dest โ€” pointer to the null-terminated wide string you're appending to ๐Ÿซถ
  • src โ€” pointer to the null-terminated wide string you're copying from ๐ŸŒˆ
  • count โ€” the maximum number of wide characters to copy, like a little limit, a lil snack portion ๐Ÿ“

๐Ÿ’œ Return value ๐Ÿ’œ

She gives you back dest. That's it. That's the return. Simple. Iconic. Periodt. ๐Ÿ‘‘


๐Ÿฆ‹ Example ๐Ÿฆ‹

Run this code, bestie:

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

int main(void)
{
    wchar_t str[50] = L"ะ—ะตะผะปั, ะฟั€ะพั‰ะฐะน.";
    std::wcsncat(str, L" ", 1);
    std::wcsncat(str, L"ะ’ ะดะพะฑั€ั‹ะน ะฟัƒั‚ัŒ.", 8); // only append the first 8 wide chars

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

Possible output:

ะ—ะตะผะปั, ะฟั€ะพั‰ะฐะน. ะ’ ะดะพะฑั€ั‹ะน

See how she only grabbed the first 8 wide chars on that second call? Limited edition behavior, and we're obsessed. ๐Ÿ’…โœจ


๐Ÿ”ฅ See also ๐Ÿ”ฅ

  • wcscat โ€” appends a copy of one wide string to another (function)
  • strncat โ€” concatenates a certain amount of characters of two strings (function)
  • wcscpy โ€” copies one wide string to another (function)
  • C documentation for wcsncat โ€” the OG, the ancestor, the blueprint ๐Ÿ‘‘

๐Ÿ’– okay that's the whole page, bestie! This gem came from http://cppreference.com, dated 2024.06.10, and it's the std::wcsncat(3) manual page โ€” go forth and append responsibly, and remember: the array must be big enough or it's undefined behavior city ๐Ÿ˜ญ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from