From 3a3715a0646d16157cbdb750ecaa96293e0d09b7 Mon Sep 17 00:00:00 2001 From: Orvid King <709247+Orvid@users.noreply.github.com> Date: Wed, 25 Jun 2025 16:36:11 -0700 Subject: [PATCH 1/2] Add support for leading `-` in character sets. --- include/ctre/pcre.gram | 2 +- include/ctre/pcre.hpp | 3 ++- single-header/ctre.hpp | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/ctre/pcre.gram b/include/ctre/pcre.gram index f86efdf4..9ea6819b 100644 --- a/include/ctre/pcre.gram +++ b/include/ctre/pcre.gram @@ -87,7 +87,7 @@ mode_switch->s,[mode_singleline], mode_switch->m,[mode_multiline], mode_switch2->close | -character_class->sopen,,[set_make],sclose|sopen,caret,,[set_make_negative],sclose +character_class->sopen,,[set_make],sclose|sopen,minus,,[set_make],sclose|sopen,caret,,[set_make_negative],sclose set->,[set_start], set2a->,[set_start],|epsilon,[set_empty] set2b->,[set_combine],|epsilon diff --git a/include/ctre/pcre.hpp b/include/ctre/pcre.hpp index 70ee7314..d00fdf49 100644 --- a/include/ctre/pcre.hpp +++ b/include/ctre/pcre.hpp @@ -234,7 +234,8 @@ struct pcre { static constexpr auto rule(c, ctll::set<'!','$','\x28','\x29','*','+',',','.','/','0','1','2','3','4','5','6','7','8','9',':','<','=','>','?','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\"','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\x7B','|','\x7D'>) -> ctll::push>; static constexpr auto rule(c, _others) -> ctll::push>; static constexpr auto rule(c, ctll::term<'^'>) -> ctll::push>; - static constexpr auto rule(c, ctll::set<'-',']'>) -> ctll::reject; + static constexpr auto rule(c, ctll::term<'-'>) -> ctll::push>; + static constexpr auto rule(c, ctll::term<']'>) -> ctll::reject; static constexpr auto rule(class_named_name, ctll::term<'x'>) -> ctll::push, ctll::term<'i'>, ctll::term<'g'>, ctll::term<'i'>, ctll::term<'t'>, class_named_xdigit>; static constexpr auto rule(class_named_name, ctll::term<'d'>) -> ctll::push, ctll::term<'g'>, ctll::term<'i'>, ctll::term<'t'>, class_named_digit>; diff --git a/single-header/ctre.hpp b/single-header/ctre.hpp index 41796262..e54ceb5a 100644 --- a/single-header/ctre.hpp +++ b/single-header/ctre.hpp @@ -1176,7 +1176,8 @@ struct pcre { static constexpr auto rule(c, ctll::set<'!','$','\x28','\x29','*','+',',','.','/','0','1','2','3','4','5','6','7','8','9',':','<','=','>','?','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\"','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\x7B','|','\x7D'>) -> ctll::push>; static constexpr auto rule(c, _others) -> ctll::push>; static constexpr auto rule(c, ctll::term<'^'>) -> ctll::push>; - static constexpr auto rule(c, ctll::set<'-',']'>) -> ctll::reject; + static constexpr auto rule(c, ctll::term<'-'>) -> ctll::push>; + static constexpr auto rule(c, ctll::term<']'>) -> ctll::reject; static constexpr auto rule(class_named_name, ctll::term<'x'>) -> ctll::push, ctll::term<'i'>, ctll::term<'g'>, ctll::term<'i'>, ctll::term<'t'>, class_named_xdigit>; static constexpr auto rule(class_named_name, ctll::term<'d'>) -> ctll::push, ctll::term<'g'>, ctll::term<'i'>, ctll::term<'t'>, class_named_digit>; From 16d83e0b9b33ed4b7e1f6a0b9d1730e616454307 Mon Sep 17 00:00:00 2001 From: Orvid King <709247+Orvid@users.noreply.github.com> Date: Wed, 25 Jun 2025 16:43:18 -0700 Subject: [PATCH 2/2] Add tests Add tests for the changes to support unescaped leading `-` in character sets. --- tests/matching3.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/matching3.cpp b/tests/matching3.cpp index e123634b..c8ccc916 100644 --- a/tests/matching3.cpp +++ b/tests/matching3.cpp @@ -246,3 +246,7 @@ TEST_NOT_MATCH(233, "[^]", ""); TEST_MATCH(234, "[^]+", "x"); TEST_MATCH(235, "[^]?", ""); +// issue #346 +TEST_MATCH(236, "[-A-Z]?", "Hello-World"); +TEST_NOT_MATCH(237, "[A-Z]?", "Hello-World"); +TEST_MATCH(238, "[\\-A-Z]?", "Hello-World");