home | help
std::regex_traits::value(3)    C++ Standard Libary   std::regex_traits::value(3)

NAME
     std::regex_traits::value - std::regex_traits::value

Synopsis
	int value( CharT ch, int radix ) const;  (since C++11)

	Determines  the  value	represented  by the digit ch in the numeric base
     radix, given
	the currently imbued locale. This function is called by std::regex  when
     processing
	Quantifiers  such  as {1} or {2,5}, Backreferences such as \1, and hexa-
     decimal and
	Unicode character escapes.

Parameters
	ch    - the character that may represent a digit
	radix - either 8, 10, or 16

Return value
	The numeric value if ch indeed represents a digit in the  currently  im-
     bued locale
	that is valid for the numeric base radix, or -1 on error.

Example
     // 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'a',0},   {L'a,',1},
     {L'ao',2},
						  {L'a,',3},	       {L'a',4},
     {L'ao',5},
						  {L'a',6},	      {L'a,',7},
     {L'a<<',8},
						  {L'a^1',9},	  {L'i1/4!',10},
     {L'i1/4c',11},
						  {L'i1/4L',12},  {L'i1/4x',13},
     {L'i1/4Y',14},
						  {L'i1/4|',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"ec"";

	  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"\\ua^1a<<i1/2a<<")))
	      std::wcout  <<  L"\\ua^1a<<i1/2a<< with custom traits matched " <<
     in << '\n';
      }

Output:
      \u98a8 matched ec"
      \ua^1a<<i1/2a<< with custom traits matched ec"

http://cppreference.com 	   2024.06.10	     std::regex_traits::value(3)

home | help