diff options
-rw-r--r-- | src/Document.cxx | 26 | ||||
-rw-r--r-- | src/RESearch.cxx | 14 | ||||
-rw-r--r-- | src/RESearch.h | 2 | ||||
-rw-r--r-- | test/unit/testRESearch.cxx | 17 |
4 files changed, 21 insertions, 38 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 2c04bbaae..5d9e10c40 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -3143,11 +3143,6 @@ bool MatchOnLines(const Document *doc, const Regex ®exp, const RESearchRange for (size_t co = 0; co < match.size() && co < RESearch::MAXTAG; co++) { search.bopat[co] = match[co].first.Pos(); search.eopat[co] = match[co].second.PosRoundUp(); - const Sci::Position lenMatch = search.eopat[co] - search.bopat[co]; - search.pat[co].resize(lenMatch); - for (Sci::Position iPos = 0; iPos < lenMatch; iPos++) { - search.pat[co][iPos] = doc->CharAt(iPos + search.bopat[co]); - } } } return matched; @@ -3304,19 +3299,20 @@ Sci::Position BuiltinRegex::FindText(Document *doc, Sci::Position minPos, Sci::P const char *BuiltinRegex::SubstituteByPosition(Document *doc, const char *text, Sci::Position *length) { substituted.clear(); - const DocumentIndexer di(doc, doc->Length()); - search.GrabMatches(di); for (Sci::Position j = 0; j < *length; j++) { if (text[j] == '\\') { - if (text[j + 1] >= '0' && text[j + 1] <= '9') { - const unsigned int patNum = text[j + 1] - '0'; - const Sci::Position len = search.eopat[patNum] - search.bopat[patNum]; - if (!search.pat[patNum].empty()) // Will be null if try for a match that did not occur - substituted.append(search.pat[patNum].c_str(), len); - j++; + const char chNext = text[++j]; + if (chNext >= '0' && chNext <= '9') { + const unsigned int patNum = chNext - '0'; + const Sci::Position startPos = search.bopat[patNum]; + const Sci::Position len = search.eopat[patNum] - startPos; + if (len > 0) { // Will be null if try for a match that did not occur + const size_t size = substituted.length(); + substituted.resize(size + len); + doc->GetCharRange(substituted.data() + size, startPos, len); + } } else { - j++; - switch (text[j]) { + switch (chNext) { case 'a': substituted.push_back('\a'); break; diff --git a/src/RESearch.cxx b/src/RESearch.cxx index 98399c925..8fe3724c8 100644 --- a/src/RESearch.cxx +++ b/src/RESearch.cxx @@ -264,20 +264,6 @@ RESearch::RESearch(CharClassify *charClassTable) { void RESearch::Clear() { bopat.fill(NOTFOUND); eopat.fill(NOTFOUND); - for (int i = 0; i < MAXTAG; i++) { - pat[i].clear(); - } -} - -void RESearch::GrabMatches(const CharacterIndexer &ci) { - for (unsigned int i = 0; i < MAXTAG; i++) { - if ((bopat[i] != NOTFOUND) && (eopat[i] != NOTFOUND)) { - const Sci::Position len = eopat[i] - bopat[i]; - pat[i].resize(len); - for (Sci::Position j = 0; j < len; j++) - pat[i][j] = ci.CharAt(bopat[i] + j); - } - } } void RESearch::ChSet(unsigned char c) noexcept { diff --git a/src/RESearch.h b/src/RESearch.h index c142441ae..37f210e13 100644 --- a/src/RESearch.h +++ b/src/RESearch.h @@ -22,7 +22,6 @@ public: explicit RESearch(CharClassify *charClassTable); // No dynamic allocation so default copy constructor and assignment operator are OK. void Clear(); - void GrabMatches(const CharacterIndexer &ci); const char *Compile(const char *pattern, Sci::Position length, bool caseSensitive, bool posix) noexcept; int Execute(const CharacterIndexer &ci, Sci::Position lp, Sci::Position endp); @@ -32,7 +31,6 @@ public: using MatchPositions = std::array<Sci::Position, MAXTAG>; MatchPositions bopat; MatchPositions eopat; - std::string pat[MAXTAG]; private: diff --git a/test/unit/testRESearch.cxx b/test/unit/testRESearch.cxx index 5c5d54c71..781d776a4 100644 --- a/test/unit/testRESearch.cxx +++ b/test/unit/testRESearch.cxx @@ -41,6 +41,9 @@ public: char CharAt(Sci::Position index) const override { return s.at(index); } + std::string GetCharRange(Sci::Position position, Sci::Position lengthRetrieve) const { + return s.substr(position, lengthRetrieve); + } }; // Test RESearch. @@ -48,18 +51,18 @@ public: TEST_CASE("RESearch") { CharClassify cc; - const char sTextSpace[] = "Scintilla "; - const char pattern[] = "[a-z]+"; + constexpr std::string_view sTextSpace = "Scintilla "; + constexpr std::string_view pattern = "[a-z]+"; SECTION("Compile") { std::unique_ptr<RESearch> re = std::make_unique<RESearch>(&cc); - const char *msg = re->Compile(pattern, strlen(pattern), true, false); + const char *msg = re->Compile(pattern.data(), pattern.length(), true, false); REQUIRE(nullptr == msg); } SECTION("Execute") { std::unique_ptr<RESearch> re = std::make_unique<RESearch>(&cc); - re->Compile(pattern, strlen(pattern), true, false); + re->Compile(pattern.data(), pattern.length(), true, false); StringCI sci(sTextSpace); const int x = re->Execute(sci, 0, sci.Length()); REQUIRE(x == 1); @@ -69,11 +72,11 @@ TEST_CASE("RESearch") { SECTION("Grab") { std::unique_ptr<RESearch> re = std::make_unique<RESearch>(&cc); - re->Compile(pattern, strlen(pattern), true, false); + re->Compile(pattern.data(), pattern.length(), true, false); StringCI sci(sTextSpace); re->Execute(sci, 0, sci.Length()); - re->GrabMatches(sci); - REQUIRE(re->pat[0] == "cintilla"); + std::string pat = sci.GetCharRange(re->bopat[0], re->eopat[0] - re->bopat[0]); + REQUIRE(pat == "cintilla"); } } |