From 57b71473dd52d91dd6092f483e73b17cf4fe20d3 Mon Sep 17 00:00:00 2001 From: zhangyouyou Date: Fri, 12 Apr 2024 10:40:48 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20=E6=AD=A3=E5=88=99=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E8=A7=A3=E6=9E=90/(=3F:)*/=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyouyou --- ecmascript/regexp/regexp_parser.cpp | 7 ++++++- ecmascript/regexp/regexp_parser.h | 3 +++ test/moduletest/regexp/expect_output.txt | 2 ++ test/moduletest/regexp/regexp.js | 9 ++++++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ecmascript/regexp/regexp_parser.cpp b/ecmascript/regexp/regexp_parser.cpp index dfebe93946..5500fd38e9 100644 --- a/ecmascript/regexp/regexp_parser.cpp +++ b/ecmascript/regexp/regexp_parser.cpp @@ -111,6 +111,10 @@ void RegExpParser::ParseDisjunction(bool isBackward) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) PrintF("Parse Disjunction------\n"); + if (c0_ == ')') { + isEmpty_ = true; + return; + } size_t start = buffer_.size_; ParseAlternative(isBackward); if (isError_) { @@ -746,7 +750,7 @@ void RegExpParser::ParseQuantifier(size_t atomBcStart, int captureStart, int cap ParseError("nothing to repeat"); return; } - if (min != -1 && max != -1) { + if (min != -1 && max != -1 && !isEmpty_) { stackCount_++; PushOpCode pushOp; pushOp.InsertOpCode(&buffer_, atomBcStart); @@ -788,6 +792,7 @@ void RegExpParser::ParseQuantifier(size_t atomBcStart, int captureStart, int cap PopOpCode popOp; popOp.EmitOpCode(&buffer_); } + isEmpty_ = false; } bool RegExpParser::ParseGroupSpecifier(const uint8_t **pp, CString &name) diff --git a/ecmascript/regexp/regexp_parser.h b/ecmascript/regexp/regexp_parser.h index e3f6afa436..4c4f91af1f 100644 --- a/ecmascript/regexp/regexp_parser.h +++ b/ecmascript/regexp/regexp_parser.h @@ -65,6 +65,7 @@ public: captureCount_(0), stackCount_(0), isError_(false), + isEmpty_(false), buffer_(chunk), groupNames_(chunk) { @@ -216,6 +217,7 @@ private: end_ = nullptr; c0_ = KEY_EOF; isError_ = false; + isEmpty_ = false; } void Advance() @@ -259,6 +261,7 @@ private: int captureCount_; int stackCount_; bool isError_; + bool isEmpty_; char errorMsg_[TMP_BUF_SIZE] = {0}; // NOLINTNEXTLINE(modernize-avoid-c-arrays) int hasNamedCaptures_ = -1; int totalCaptureCount_ = -1; diff --git a/test/moduletest/regexp/expect_output.txt b/test/moduletest/regexp/expect_output.txt index 3017f68013..be59f42322 100644 --- a/test/moduletest/regexp/expect_output.txt +++ b/test/moduletest/regexp/expect_output.txt @@ -25,3 +25,5 @@ false true false SC52BAHL01031234567890123456USD +true + diff --git a/test/moduletest/regexp/regexp.js b/test/moduletest/regexp/regexp.js index 1774fb810c..7ff5ea0eb8 100644 --- a/test/moduletest/regexp/regexp.js +++ b/test/moduletest/regexp/regexp.js @@ -283,4 +283,11 @@ print(temp1) let str1 = 'SC52BAHL01031234567890123456USD' print(str1.replace(/[^A-Z0-9]+/gi, '')) -let reg50 = /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i \ No newline at end of file +let reg50 = /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i + +const regex = /(?:)+/; +const str10 = "abcabcabc"; +const matches = regex.test(str10); +print(matches); +const matches1 = regex.exec(str10); +print(matches1); \ No newline at end of file -- Gitee