From afd16eac09d8178d47d2e39ba8fe87c765369fc7 Mon Sep 17 00:00:00 2001 From: Dmitry Olshansky Date: Wed, 6 Apr 2016 16:52:12 +0300 Subject: [PATCH] Fix issue 7551 - Regex parsing bug for right bracket in character class --- std/regex/internal/parser.d | 17 ++++++++++++++--- std/regex/internal/tests.d | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/std/regex/internal/parser.d b/std/regex/internal/parser.d index 32889131ed1..f8387490922 100644 --- a/std/regex/internal/parser.d +++ b/std/regex/internal/parser.d @@ -1143,9 +1143,20 @@ struct Parser(R) opstack.push(Operator.Negate); enforce(next(), "unexpected end of character class"); } - //[] is prohibited - enforce(current != ']', "wrong character class"); - goto default; + else if (current == ']') // []...] is special cased + { + enforce(next(), "wrong character set"); + auto pair = parseCharTerm(); + pair[0].add(']', ']'+1); + if(pair[1] != Operator.None) + { + if(opstack.top == Operator.Union) + unrollWhile!(unaryFun!"a == a.Union")(vstack, opstack); + opstack.push(pair[1]); + } + vstack.push(pair[0]); + } + break; case ']': enforce(unrollWhile!(unaryFun!"a != a.Open")(vstack, opstack), "character class syntax error"); diff --git a/std/regex/internal/tests.d b/std/regex/internal/tests.d index e5d61300003..88bc0a6eac1 100644 --- a/std/regex/internal/tests.d +++ b/std/regex/internal/tests.d @@ -968,3 +968,12 @@ unittest assert(matchAll(v, ctPat2).front.hit == v); } +// bugzilla 7551 +unittest +{ + auto r = regex("[]abc]*"); + assert("]ab".matchFirst(r).hit == "]ab"); + assertThrown(regex("[]")); + auto r2 = regex("[]abc--ab]*"); + assert("]ac".matchFirst(r2).hit == "]"); +}