std::regex_...orm_primary(3)
π std::regex_traits::transform_primary π
okay so bestie, this page is from the C++ Standard Libary manual β yes, that's literally the manual name up top, spelling and all β and the page title is std::regex_traits::transform_primary. Buckle up, this one is niche but iconic.
β¨ NAME β¨
std::regex_traits::transform_primary β std::regex_traits::transform_primary. Yep, the title and the name are matching. Slay.
π Synopsis π
template< class ForwardIt >
string_type transform_primary( ForwardIt first, ForwardIt last )
const;
For the character sequence [first, last), it obtains the primary sort key in the imbued locale's collating order. Translation: it's the sort key that is based on the positions of the letters and collation units in the national alphabet, ignoring case, diacritics, variants, etc. If a primary sort key compares less than another primary sort key with operator<, then the character sequence that produced the first sort key comes before the character sequence that produced the second sort key, in the currently imbued locale's primary collation order. That is the whole vibe.
The regex library uses this trait to match characters against equivalence classes. For example, the regex [[=a=]] is equivalent to the character c1 if traits.transform_primary(c1) is equivalent to traits.transform_primary("a") β which is true for any c1 from "AΓΓΓΓΓΓ
aà ÑÒãÀΓ₯" in the U.S. English locale. Note that transform_primary() takes a character sequence argument because equivalence classes may be multicharacter, such as [[=ch=]] in Czech or [[=dzs=]] in Hungarian. Multicharacter equivalence classes are real and they are coming for you.
Heads up, bestie: there is no portable way to define primary sort key in terms ofstd::locale, since the conversion from the collation key returned bystd::collate::transform()to the primary equivalence key is locale-specific, and if the user replaces thestd::collatefacet, that conversion is no longer known to the standard library'sstd::regex_traits. Standard library specializations ofstd::regex_traitsreturn an empty string unless thestd::collatefacet of the currently-imbued locale was not replaced by the user, and still matches the system-suppliedstd::collatefacet), in which casestd::collate_byname<CharT>::transform(first, last)is executed and the sort key it produces is converted to the expected primary sort key using a locale-specific conversion.
π§Έ Parameters π§Έ
first, lastβ a pair of iterators which determines the sequence of characters to compare.
π¦ Type requirements π¦
ForwardItmust meet the requirements ofLegacyForwardIterator.
π Return value π
The primary sort key for the character sequence [first, last) in the currently imbued locale, ignoring case, variant, diacritics, etc.
π Example π
Demonstrates the regex feature that works through transform_primary().
// Run this code
#include <iostream>
#include <regex>
int main()
{
std::locale::global(std::locale("en_US.UTF-8"));
std::wstring str = L"AΓΓΓΓΓΓ
aà ÑÒãÀΓ₯";
std::wregex re(L"[[=a=]]*", std::regex::basic);
std::cout << std::boolalpha << std::regex_match(str, re) << '\n';
}
π Possible output π
true
This section is incomplete Reason: could use an example with user-definedregex_traitssupplying user-definedtransform_primary
ποΈ Category: ποΈ
- * Todo with reason
okay that's the whole page, bestie. Footer vibes, preserved exactly: http://cppreference.com, version/date 2024.06.10, and the footer title std::regex_...orm_primary(3). Go slay your locale-aware regex matching. π
β¨
π¦ 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