Skip to content

Commit 9786122

Browse files
committed
Bugfix for pymlreplace
The offset had been converted to an idx_t, but in some circumstances it needed to be negative. Introduced an offset_t (int) typedef, and made the currentOffset an offset_t. This probably breaks some lint rules about signed/unsigned conversions, but not sure what is best to do. Also fixed a logic error with the "next" position to start from after making a replacement. The Python version of the string obviously doesn't change after a replacement, therefore the next point can be immediately after the end of the previous found target, ignoring the size of the replacement string.
1 parent e2275dd commit 9786122

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

PythonScript/src/ScintillaWrapper.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ void ScintillaWrapper::rereplace(boost::python::object searchExp, boost::python:
558558
void ScintillaWrapper::pymlreplace(boost::python::object searchExp, boost::python::object replaceStr, boost::python::object count, boost::python::object flags, boost::python::object startPosition, boost::python::object endPosition)
559559
{
560560
boost::python::str contents;
561-
idx_t currentOffset = 0;
561+
offset_t currentOffset = 0;
562562

563563
if (startPosition.is_none() && endPosition.is_none())
564564
{
@@ -586,7 +586,7 @@ void ScintillaWrapper::pymlreplace(boost::python::object searchExp, boost::pytho
586586
range.chrg.cpMax = GetLength();
587587
}
588588

589-
currentOffset = (idx_t)range.chrg.cpMin;
589+
currentOffset = (offset_t)range.chrg.cpMin;
590590

591591
range.lpstrText = new char[size_t((range.chrg.cpMax - range.chrg.cpMin) + 1)];
592592
callScintilla(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&range));
@@ -654,9 +654,8 @@ void ScintillaWrapper::pymlreplace(boost::python::object searchExp, boost::pytho
654654
currentOffset += replacementLength - (matchEnd - matchStart);
655655

656656

657-
// Set startPos (the start of the last match (absolute from the start of the string,
658-
// ignoring the old startPos), plus the length of the replacement
659-
startPos = matchStart + replacementLength;
657+
// Set startPos to the end of the last match - startPos is with the original document
658+
startPos = matchEnd;
660659

661660

662661
}
@@ -916,7 +915,7 @@ void ScintillaWrapper::pymlsearch(boost::python::object searchExp, boost::python
916915
{
917916
match = re.attr("search")(contents, pos, endPos);
918917

919-
// If nothing found, then continue to next line
918+
// If nothing found, then skip
920919
if (!match.is_none())
921920
{
922921
pos = boost::python::extract<int>(match.attr("start")());

PythonScript/src/stdafx.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ typedef std::basic_string<TCHAR> tstring;
7777

7878
// Index type
7979
typedef size_t idx_t;
80+
81+
// Offset type
82+
typedef int offset_t;
83+
8084
#define IDX_MAX SIZE_MAX
8185

86+
#define OFF_MIN INT_MIN
87+
#define OFF_MAX INT_MAX
88+
8289
// Workaround to fix boost::python::len return type, ssize_t, which is a signed integer.
8390
#define _len(x) ((size_t)len(x))

0 commit comments

Comments
 (0)