std::strxfrm(3)
π NAME π
Okay so this is the std::strxfrm(3) page from the C++ Standard Libary manual (yes, thatβs the headerβs spelling, not me π), and the name is literally std::strxfrm β std::strxfrm. No cap, thatβs the whole NAME section.
β¨ Synopsis β¨
Defined in header <cstring>:
std::size_t strxfrm( char* dest, const char* src, std::size_t count
);
So hereβs the vibe: it transforms the null-terminated byte string pointed to by src into the implementation-defined form such that comparing two transformed strings with std::strcmp gives the same result as comparing the original strings with std::strcoll, in the current C locale.
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.
π¨ Undefined behavior alert, bestie: the behavior is undefined if thedestarray is not large enough. The behavior is undefined ifdestandsrcoverlap.
If count is 0, then dest is allowed to be a null pointer. Slay.
π§ Notes π§
The correct length of the buffer that can receive the entire transformed string is:
1 + std::strxfrm(nullptr, src, 0)
This function is used when making multiple locale-dependent comparisons using the same string or set of strings, because it is more efficient to use std::strxfrm to transform all the strings just once, and subsequently compare the transformed strings with std::strcmp. Literally the βdo the work once, thank yourself laterβ icon.
πΈ Parameters πΈ
destβ pointer to the first element of the array where the transformed string will be written.srcβ pointer to the first character of a null-terminated byte string to transform.countβ maximum number of characters to be written.
π Return value π
The length of the transformed string, not including the terminating null-character. Thatβs the whole return-value tea π«.
π Example π
// Run this code
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
char* loc = std::setlocale(LC_COLLATE, "cs_CZ.iso88592");
assert(loc);
std::string in1 = "hrnec";
std::string out1(1 + std::strxfrm(nullptr, in1.c_str(), 0), ' ');
std::string in2 = "chrt";
std::string out2(1 + std::strxfrm(nullptr, in2.c_str(), 0), ' ');
std::strxfrm(&out1[0], in1.c_str(), out1.size());
std::strxfrm(&out2[0], in2.c_str(), out2.size());
std::cout << "In the Czech locale: ";
if (out1 < out2)
std::cout << in1 << " before " << in2 << '\n';
else
std::cout << in2 << " before " << in1 << '\n';
std::cout << "In lexicographical comparison: ";
if (in1 < in2)
std::cout << in1 << " before " << in2 << '\n';
else
std::cout << in2 << " before " << in1 << '\n';
}
Possible output:
In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec
π See also π
wcsxfrmβ transform a wide string so thatwcscmpwould produce the same result aswcscoll(function).do_transformβ transforms a string so that collation can be replaced by comparison [virtual] (virtual protected member function ofstd::collate<CharT>).strcollβ compares two strings in accordance to the current locale (function).- C documentation for
strxfrm.
source/version/date: http://cppreference.com, version/date 2024.06.10, page std::strxfrm(3) β bye bestie, go transform 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