std::regex_traits::value(3)
πΈ NAME πΈ
okay bestie, welcome to std::regex_traits::value(3) β the page from the C++ Standard Libary manual (yes, that's literally how it's spelled, iykyk π). In classic manpage fashion, the name is also std::regex_traits::value β std::regex_traits::value. It's giving "same name, main character energy" π
β¨
π Synopsis π
int value( CharT ch, int radix ) const; (since C++11)
bestie, this one determines the value represented by the digit ch in the numeric base radix, given the currently imbued locale. No cap, this function is called by std::regex when processing Quantifiers such as {1} or {2,5}, Backreferences such as \1, and hexadecimal and Unicode character escapes. It's lowkey the digit detective of regex traits π₯
π Parameters π
chβ the character that may represent a digitradixβ either8,10, or16
π¦ Return value π¦
The numeric value if ch indeed represents a digit in the currently imbued locale that is valid for the numeric base radix, or -1 on error. periodt. If it's not valid, it's a -1 situation and we move on π«Ά
π§ Example π§
okay so here's the whole code moment, bestie β run this code and watch the custom traits do their thing π
// Run this code
#include <iostream>
#include <locale>
#include <map>
#include <regex>
// This custom regex traits allows japanese numerals
struct jnum_traits : std::regex_traits<wchar_t>
{
static std::map<wchar_t, int> data;
int value(wchar_t ch, int radix) const
{
wchar_t up = std::toupper(ch, getloc());
return data.count(up) ? data[up] : regex_traits::value(ch,
radix);
}
};
std::map<wchar_t, int> jnum_traits::data = {{L'γ',0}, {L'δΈ',1},
{L'δΊ',2},
{L'δΈ',3}, {L'ε',4},
{L'δΊ',5},
{L'ε
',6}, {L'δΈ',7},
{L'ε
«',8},
{L'δΉ',9}, {L'οΌ‘',10},
{L'οΌ’',11},
{L'οΌ£',12}, {L'οΌ€',13},
{L'οΌ₯',14},
{L'οΌ¦',15}};
int main()
{
std::locale::global(std::locale("ja_JP.utf8"));
std::wcout.sync_with_stdio(false);
std::wcout.imbue(std::locale());
std::wstring in = L"ι’¨";
if (std::regex_match(in, std::wregex(L"\\u98a8")))
std::wcout << "\\u98a8 matched " << in << '\n';
if (std::regex_match(in, std::basic_regex<wchar_t,
jnum_traits>(L"\\uδΉε
«ο½ε
«")))
std::wcout << L"\\uδΉε
«ο½ε
« with custom traits matched " << in
<< '\n';
}
π Output π
\u98a8 matched ι’¨
\uδΉε
«ο½ε
« with custom traits matched ι’¨
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::regex_traits::value(3) β and that's the whole manual page, bestie, slay πβ¨π
π¦ 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