[colorer/Colorer-library] cregexp_refactoring: Grow the CRegExp parse stack with std::vector instead of manual realloc and memcpy. (a574d0b)

0 views
Skip to first unread message

farg...@farmanager.com

unread,
Aug 16, 2026, 3:31:30 PM (12 hours ago) Aug 16
to farco...@googlegroups.com
Repository : https://github.com/colorer/Colorer-library
On branch : cregexp_refactoring
Link : https://github.com/colorer/Colorer-library/commit/a574d0b95abd10f44cda7c16d8c9ce696c1b9121

>---------------------------------------------------------------

commit a574d0b95abd10f44cda7c16d8c9ce696c1b9121
Author: Aleksey Dobrunov <cta...@ctapmex.com>
Date: Mon Aug 17 00:26:51 2026 +0500

Grow the CRegExp parse stack with std::vector instead of manual realloc and memcpy.


>---------------------------------------------------------------

a574d0b95abd10f44cda7c16d8c9ce696c1b9121
src/colorer/cregexp/cregexp.cpp | 22 +++++-----------------
src/colorer/cregexp/cregexp.h | 7 ++++---
2 files changed, 9 insertions(+), 20 deletions(-)

diff --git a/src/colorer/cregexp/cregexp.cpp b/src/colorer/cregexp/cregexp.cpp
index 944f33d..6f2182b 100644
--- a/src/colorer/cregexp/cregexp.cpp
+++ b/src/colorer/cregexp/cregexp.cpp
@@ -1,8 +1,6 @@
#include "colorer/cregexp/cregexp.h"
-#include <cstring>

-StackElem* CRegExp::RegExpStack {nullptr};
-int CRegExp::RegExpStack_Size {0};
+std::vector<StackElem> CRegExp::RegExpStack;


/////////////////////////////////////////////////////////////////////////////
@@ -953,18 +951,10 @@ bool CRegExp::matchCopiedRange(const UnicodeString& src, int from, int to, int&
void CRegExp::insert_stack(SRegInfo** re, SRegInfo** prev, int* toParse, bool* leftenter, ReAction ifTrueReturn,
ReAction ifFalseReturn, SRegInfo** re2, SRegInfo** prev2, int toParse2)
{
- if (RegExpStack_Size == 0) {
- CRegExp::RegExpStack = new StackElem[INIT_MEM_SIZE];
- RegExpStack_Size = INIT_MEM_SIZE;
+ if (RegExpStack.size() == static_cast<size_t>(count_elem)) {
+ RegExpStack.resize(RegExpStack.empty() ? INIT_MEM_SIZE : RegExpStack.size() + MEM_INC);
}
- if (RegExpStack_Size == count_elem) {
- RegExpStack_Size += MEM_INC;
- StackElem* s = new StackElem[RegExpStack_Size];
- memcpy(s, CRegExp::RegExpStack, count_elem * sizeof(StackElem));
- delete[] CRegExp::RegExpStack;
- CRegExp::RegExpStack = s;
- }
- StackElem& ne = CRegExp::RegExpStack[count_elem++];
+ StackElem& ne = CRegExp::RegExpStack[static_cast<size_t>(count_elem++)];
ne.re = *re;
ne.prev = *prev;
ne.toParse = *toParse;
@@ -1576,9 +1566,7 @@ bool CRegExp::setPositionMoves(bool moves)

void CRegExp::clearRegExpStack()
{
- CRegExp::RegExpStack_Size = 0;
- delete[] CRegExp::RegExpStack;
- CRegExp::RegExpStack = nullptr;
+ std::vector<StackElem>().swap(RegExpStack);
}

int CRegExp::getBracketNo(const UnicodeString* brname) const
diff --git a/src/colorer/cregexp/cregexp.h b/src/colorer/cregexp/cregexp.h
index 8ab011d..3a36546 100644
--- a/src/colorer/cregexp/cregexp.h
+++ b/src/colorer/cregexp/cregexp.h
@@ -3,6 +3,7 @@

#include "colorer/Common.h"
#include <array>
+#include <vector>

/**
@addtogroup cregexp Regular Expressions
@@ -219,7 +220,8 @@ struct StackElem
- No surrogate symbols support,
- No string length changes on case mappings (only 1 <-> 1 mappings),
\par 2.2. Algorithmic problems:
- - Explicit parse stack (unbounded growth, shared between CRegExp instances).
+ - Explicit parse stack (grows as needed and is reused by all CRegExp
+ instances; matching is single-threaded).

@ingroup cregexp
*/
@@ -343,8 +345,7 @@ class CRegExp
void insert_stack(SRegInfo** re, SRegInfo** prev, int* toParse, bool* leftenter, ReAction ifTrueReturn,
ReAction ifFalseReturn, SRegInfo** re2, SRegInfo** prev2, int toParse2);

- static StackElem* RegExpStack;
- static int RegExpStack_Size;
+ static std::vector<StackElem> RegExpStack;

public:
static void clearRegExpStack();


Reply all
Reply to author
Forward
0 new messages