std::wcsncmp(3)
π std::wcsncmp π
okay bestie, welcome to std::wcsncmp(3) β that's the page for the C++ Standard Library function, and yes the NAME section literally just says std::wcsncmp back at you π iconic of it honestly. It's the wide-string girlie of the strncmp family and she's got range. Let's get into it π«Ά
β¨ Synopsis β¨
First thing's first: this baby lives in the header <cwchar> π
int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );
πΈ Description πΈ
So what does she do? wcsncmp compares at most count wide characters of two null-terminated wide strings, and the comparison is done lexicographically β no cap, that's the whole vibe.
The sign of the result is the sign of the difference between the values of the first pair of wide characters that differ in the strings being compared. Like, she walks them side by side until somebody disagrees, and that disagreement's sign is what you get back π
β οΈ okay this is the serious part, listen up: the behavior is undefined iflhsorrhsare not pointers to null-terminated strings. Not a drill. Do not pass her a non-terminated string, she will not be okay π
π Parameters π
lhs,rhsβ pointers to the null-terminated wide strings to compare. the two besties facing off π«Άcountβ maximum number of characters to compare. how many steps of the walk we're doing before we call it π
β Return value β
- Negative value if
lhsappears beforerhsin lexicographical order. she comes first, main character energy π₯ - Zero if
lhsandrhscompare equal. twin flames, matched perfectly β¨ - Positive value if
lhsappears afterrhsin lexicographical order. she's bringing up the rear, and that's fine too π
π¦ Example π¦
the comment says // Run this code and honestly? run it. it's giving educational and cute at the same time π
#include <clocale>
#include <cwchar>
#include <iostream>
#include <locale>
void demo(const wchar_t* lhs, const wchar_t* rhs, int sz)
{
int rc = std::wcsncmp(lhs, rhs, sz);
if (rc == 0)
std::wcout << "First " << sz << " characters of ["
<< lhs << "] equal [" << rhs << "]\n";
else if (rc < 0)
std::wcout << "First " << sz << " characters of ["
<< lhs << "] precede [" << rhs << "]\n";
else if (rc > 0)
std::wcout << "First " << sz << " characters of ["
<< lhs << "] follow [" << rhs << "]\n";
}
int main()
{
const wchar_t str1[] = L"μλ
νμΈμ";
const wchar_t str2[] = L"μλ
ν κ°μμμ€";
std::setlocale(LC_ALL, "en_US.utf8");
std::wcout.imbue(std::locale("en_US.utf8"));
demo(str1, str2, 5);
demo(str2, str1, 8);
demo(str1, str2, 2);
}
π Output π
First 5 characters of [μλ
νμΈμ] precede [μλ
ν κ°μμμ€]
First 8 characters of [μλ
ν κ°μμμ€] follow [μλ
νμΈμ]
First 2 characters of [μλ
νμΈμ] equal [μλ
ν κ°μμμ€]
look at that. three demos, three different vibes, all correct. we love a consistent icon π
π See also π
strncmpβ compares a certain number of characters from two strings (function)wcscmpβ compares two wide strings (function)wmemcmpβ compares a certain amount of wide characters from two arrays (function)wcscollβ compares two wide strings in accordance to the current locale (function)- C documentation for
wcsncmpβ the C-side of the family, still iconic in her own right
and that's the page, bestie! π this one came from http://cppreference.com, dated 2024.06.10, and it's std::wcsncmp(3) β go forth and compare your wide strings responsibly, and remember: null-terminate or it's undefined behavior, and we don't do undefined behavior in this group chat πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms