[Patch, libstdc++/77356] Support escape in regex bracket expression

Jonathan Wakely jwakely@redhat.com
Wed Aug 24 08:41:00 GMT 2016


On 24/08/16 00:18 -0700, Tim Shen wrote:
>I didn't realized that we can actually escape a dash inside bracket
>expression: R"([\-])", in which case the dash should be treated
>literally.

With this patch applied I no longer get a match for:

  regex_match("-", std::regex{"[a-]", std::regex_constants::basic})

"[-a]" still works correctly, but they should be equivalent for POSIX
BREs and EREs.



>diff --git a/libstdc++-v3/include/bits/regex_scanner.h b/libstdc++-v3/include/bits/regex_scanner.h
>index 37dea84..2a83d1c 100644
>--- a/libstdc++-v3/include/bits/regex_scanner.h
>+++ b/libstdc++-v3/include/bits/regex_scanner.h
>@@ -73,6 +73,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       _S_token_comma,
>       _S_token_dup_count,
>       _S_token_eof,
>+      _S_token_bracket_dash,
>       _S_token_unknown
>     };

I wonder if we want to give _S_token_unknown an initializer with a
specific value, so it doesn't change whenever we add new tokens.

If we use -1 it would change the underlying type of
_ScannerBase::_TokenT from unsigned int to signed int, so we should
give it a fixed type too:

  struct _ScannerBase
  {
  public:
    /// Token types returned from the scanner.
    enum _TokenT : unsigned
    {
      ...
      _S_token_unknown = -1u




More information about the Libstdc++ mailing list