Giter VIP home page Giter VIP logo

Comments (10)

dail8859 avatar dail8859 commented on June 13, 2024 1

It seems like we'd need to change that behaviour no matter which Regex implementation we used to fix the problem.

That is partially correct. If I recall if you use something like re2 (which I believe QRegularExpression uses) then there are more flags you can pass it to tell it that the beginning of the string you are providing isn't actually the start. But that was a while ago...so maybe I'm misremembering. QRegularExpression doesn't provide that fine of control.

That's the whole reason why in the long run I suspect Notepad++'s implementation might be worth while since it uses boostregex and they've already worked out all the corner cases :)

I assume it's implemented like that so that we are able to dynamically update the document as it goes-along and avoid converting the entire document into QString and duplicating everything?

That is a big part of it. The other part is to let Scintilla properly know about each individual change so that it can properly manage the undo stack along with other internals.

from notepadnext.

mintsoft avatar mintsoft commented on June 13, 2024 1

Yeah, the docs point towards that being quite limited (no ?, or lookaheads/behinds etc)

That is it's own very basic implementation of a regex engine. But it can also use std::regex which I'm not sure how well it matches up to something like boost.

It's probably on-par than Scintilla's one, however if you want PCRE (i.e. "proper regex") then the Boost library is the best bet certainly

from notepadnext.

mintsoft avatar mintsoft commented on June 13, 2024 1

Another reproduction of this bug is any find/replace that does not change the actual content or that the replaced string still matches the "find" i.e.:

Find (.*)
Replace $1

This will also loop forever

from notepadnext.

dail8859 avatar dail8859 commented on June 13, 2024

I would guess this is related to #192

It is probably restarting the search after the replace in the incorrect spot, thus an infinite loop.

The regex implementation is...quite rough to be honest, and I figured there were quite a few corner cases. My gut feeling is that the QRegularExpression class is not a long term solution.

Currently there are 3 options:

  • Stick with QRegularExpression and try to deal with all the intricacies and hope there's no "show stoppers".
  • Use Scintilla's built in regular expression engine (this has quite a few limitations though, e.g. no multiline matches)
  • Or...probably the best option...is try to implement the boost regex engine that Notepad++ uses. It uses boost but it also has to provide an implementation to scintilla so that it can work with that regex engine. I've glanced through Notepad++'s code that does that...and it's not immediately easy to figure out.

from notepadnext.

mintsoft avatar mintsoft commented on June 13, 2024

I think the problem here (and with #192) is actually not QRegularExpression itself; it handles this case fine. I think the issue is with the way it is used with NotepadNext as it seems to be relying on the .captured() returning a non-zero length string. If you use the QRegularExpression in a more direct manner to do the replacements it seems to work, Example:

#include <QtGlobal>
#include <QtCore>
#include <QDebug>
#include <QTextStream>
#include <QRegularExpression>
#include <iostream>

int main(int argc, char *argv[])
{
    QTextStream qout(stdout);
   
    auto options = QRegularExpression::MultilineOption | QRegularExpression::UseUnicodePropertiesOption;
    QRegularExpression re("^", options);
   
    QString replacement = "//";
    QString inputString = "1\n2\n3\n";
    QRegularExpressionMatch match = re.match(inputString, 0, QRegularExpression::NormalMatch, QRegularExpression::NoMatchOption);
   
    qDebug() << "input string: ";
    qDebug() << inputString;

    QString newString = inputString.replace(match.regularExpression(), replacement);

    qDebug() << " --------------------- ";
    qDebug() << newString;
    qDebug() << " --------------------- ";

}

Outputs the following:

input string: 
"1\n2\n3\n"
 --------------------- 
"//1\n//2\n//3\n"
 --------------------- 

Which (unless I'm missing something entirely) suggests that the library is fine for this case (tested under QT 5.15 on Debian 12)

from notepadnext.

dail8859 avatar dail8859 commented on June 13, 2024

@mintsoft Thanks for the info. You are correct that purely using QRegularExpression does work. However to integrate with the editing component you can't simply just call .replace() on a string.

Each search/replace needs a starting location and ending location. Since the substring is pulled out of the editor and turned into a QString, then ^ always matches the beginning of the string, no matter where the search is "started" in the document. Even if you do replace the first valid ^ match with // then when the search starts again after the previously replaced // it then finds another match...and gets stuck.

from notepadnext.

mintsoft avatar mintsoft commented on June 13, 2024

Each search/replace needs a starting location and ending location. Since the substring is pulled out of the editor and turned into a QString, then ^ always matches the beginning of the string, no matter where the search is "started" in the document. Even if you do replace the first valid ^ match with // then when the search starts again after the previously replaced // it then finds another match...and gets stuck.

Ahh I see. It seems like we'd need to change that behaviour no matter which Regex implementation we used to fix the problem.

I assume it's implemented like that so that we are able to dynamically update the document as it goes-along and avoid converting the entire document into QString and duplicating everything?

from notepadnext.

dail8859 avatar dail8859 commented on June 13, 2024

On a side note, Scintilla by default supports the standard library regex std::regex but I recall looking into that at one time and found some limitations...not sure if things have improved since then.

from notepadnext.

mintsoft avatar mintsoft commented on June 13, 2024

On a side note, Scintilla by default supports the standard library regex std::regex but I recall looking into that at one time and found some limitations...not sure if things have improved since then.

Yeah, the docs point towards that being quite limited (no ?, or lookaheads/behinds etc); I think that's actually the regex engine that Notepad++ used to have about 10 years ago, I remember it was very very limited for a while

from notepadnext.

dail8859 avatar dail8859 commented on June 13, 2024

Yeah, the docs point towards that being quite limited (no ?, or lookaheads/behinds etc)

That is it's own very basic implementation of a regex engine. But it can also use std::regex which I'm not sure how well it matches up to something like boost.

from notepadnext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.