std::wcsxfrm(3)
std::wcsxfrm(3) from the C++ Standard Libary β okay bestie, this is the wide-string transformation page, and it's lowkey iconic. Let's get into it π
β¨
π NAME π
std::wcsxfrm β std::wcsxfrm. That's it. Periodt. π
πΈ Synopsis πΈ
Defined in header <cwchar>
std::size_t wcsxfrm( wchar_t* dest, const wchar_t* src, std::size_t count );
So here's the vibe: it transforms the null-terminated wide string pointed to by src into the implementation-defined form such that comparing two transformed strings with std::wcscmp gives the same result as comparing the original strings with std::wcscoll, in the current C locale. Collation math, but make it fashion. π«
The first count characters of the transformed string are written to destination, including the terminating null character, and the length of the full transformed string is returned, excluding the terminating null character. Read that twice, bestie β the count and the return value are not the same thing. π
If count is 0, then dest is allowed to be a null pointer. That's your little escape hatch. π«Ά
π§ Notes π§
- The correct length of the buffer that can receive the entire transformed string is
1 + std::wcsxfrm(nullptr, src, 0). Write that down, bestie π. - This function is used when making multiple locale-dependent comparisons using the same wide string or set of wide strings, because it is more efficient to use
std::wcsxfrmto transform all the strings just once, and subsequently compare the transformed wide strings withstd::wcscmp. Efficiency slay. π₯
π Parameters π
destβ pointer to the first element of a wide null-terminated string to write the transformed string to.srcβ pointer to the null-terminated wide character string to transform.countβ maximum number of characters to output.
π¦ Return value π¦
The length of the transformed wide string, not including the terminating null-character. It's giving "length, but not that length." π
β¨ Example β¨
Run this code, bestie:
#include <cwchar>
#include <iostream>
int main()
{
std::setlocale(LC_ALL, "sv_SE.utf8");
std::wstring in1 = L"\u00e5r";
std::wstring out1(1 + std::wcsxfrm(nullptr, in1.c_str(), 0), L' ');
std::wstring in2 = L"\u00e4ngel";
std::wstring out2(1 + std::wcsxfrm(nullptr, in2.c_str(), 0), L' ');
std::wcsxfrm(&out1[0], in1.c_str(), out1.size());
std::wcsxfrm(&out2[0], in2.c_str(), out2.size());
std::wcout << "In the Swedish locale: ";
if (out1 < out2)
std::wcout << in1 << " before " << in2 << '\n';
else
std::wcout << in2 << " before " << in1 << '\n';
std::wcout << "In lexicographical comparison: ";
if (in1 < in2)
std::wcout << in1 << " before " << in2 << '\n';
else
std::wcout << in2 << " before " << in1 << '\n';
}
π Output π
In the Swedish locale: Γ₯r before Γ€ngel
In lexicographical comparison: Γ€ngel before Γ₯r
π See also π
strxfrmβ transform a string so thatstrcmpwould produce the same result asstrcoll(function)do_transform[virtual]β transforms a string so that collation can be replaced by comparison (virtual protected member function ofstd::collate<CharT>)wcscollβ compares two wide strings in accordance to the current locale (function)- C documentation for
wcsxfrm
Source/version/date: http://cppreference.com Β· 2024.06.10 Β· std::wcsxfrm(3) β bye bestie, go transform some wide strings responsibly ππ«Ά
π¦ 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