[KuitFormatterPrivate] Start porting QRegExp to QRegularExpression
ClosedPublic

Authored by ahmadsamir on Dec 30 2019, 9:08 AM.

Details

Summary

Port toVisualText() and finalizeVisualText() to use QRegularExpression.
Replace ENTITY_SUBRX macro with a static const char*.

Test Plan

make && ctest

Diff Detail

Repository
R249 KI18n
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
ahmadsamir created this revision.Dec 30 2019, 9:08 AM
Restricted Application added a project: Frameworks. · View Herald TranscriptDec 30 2019, 9:08 AM
ahmadsamir requested review of this revision.Dec 30 2019, 9:08 AM
dfaure requested changes to this revision.Jan 1 2020, 10:04 AM
dfaure added inline comments.
src/kuitmarkup.cpp
1332

This creates a QString at program startup. Please change it to

static const char s_entitySubRx[] = "...";
1342

Duplication :-)

Dangerous, if the actual char* is modified one day.

1572–1575

(same)

1577

Please try to always declare the variables where they are used. The old code said "QString ent = ...", why does the new code look more like C, declaring variables outside the loop?
This is in no way more efficient, construction isn't very different from assignment.

1597

So it's not the "original" value anymore. Why the renaming of plain to text, and of text to original? It makes the diff harder to review (but if you convince me that it makes the code easier to read, that's fine). I'm just not sure that "original" for something that is modified in many places (line 1592 and here) makes sense as a name.

This revision now requires changes to proceed.Jan 1 2020, 10:04 AM
ahmadsamir added inline comments.Jan 1 2020, 2:59 PM
src/kuitmarkup.cpp
1342

Personally, I needed to see the actual regex spelled out like that to understand the rest of the code in this function, this:

QLatin1String("^(") + QLatin1Strig(s_entitySubRx) + QLatin1String(");")

doesn't work as well for me, and especially so with finalizeVisualText(), where ENTITY_SUBRX is many lines away, and there are two capture groups used.

I thought of doing away with ENTITY_SUBRX and just construct the regex's in toVisualText() and finalizeVisualText(), but the two must use the same pattern, except for the beginning, ^ and & respectively.

I can try lessening the danger by tweaking the comment.

1577

I wouldn't know about C, having never written any code with it.

This is in no way more efficient, construction isn't very different from assignment.

That is the key I was missing (which means I should do some research, before "assuming" stuff).

1597

I concede that changing var names makes reading the diff harder, and I should, if needed, have done it in a separate patch.

My, weird, logic is that "original" is the original string that is being modified, v.s. text which is a temporary place to store the parts of original after processing them to prevent the match looping on one singular place in the string. Simply original didn't translate to constant in my head. (And "plain" didn't make sense, it's not plain when it may have "&blah4;" appended to it).

Anyway, I'd rather change them back than argue. :)

ahmadsamir updated this revision to Diff 72539.Jan 1 2020, 3:00 PM
ahmadsamir edited the summary of this revision. (Show Details)

Address comments

dfaure accepted this revision.Jan 1 2020, 6:15 PM

I wonder if others agree that "a ? b() : c()" is bad form for void b() and void c(), compared to an if(), or if it's just me.

src/kuitmarkup.cpp
1577

My statement doesn't apply to all classes obviously, the ctor/dtor could be very expensive and then my statement is incorrect.

But QString is really just a wrapper around a QStringPrivate "d", and both assignment or constructor (from another QString) just do some refcounting and grab the "d" of the other QString, so it's the same.

1588

I don't really like ternary operators that don't return a value, this is really an if().

(I guess the QChar/QString type difference is the reason why append(ok?c:...) wouldn't work).

1597

I thought it was the resolved version, without entities (if they were successfully resolved).

This revision is now accepted and ready to land.Jan 1 2020, 6:15 PM
ahmadsamir updated this revision to Diff 72567.Jan 1 2020, 7:19 PM

Ternary that doesn't return a value is weird, better avoid.

"Grammar fixes" in comments add noise to the diff, be kind to reviewers.

dfaure accepted this revision.Jan 1 2020, 7:22 PM
This revision was automatically updated to reflect the committed changes.
ahmadsamir added inline comments.Jan 2 2020, 3:53 PM
src/kuitmarkup.cpp
1597

If the entity is unknown it'll be left as is, I don't know how often that actually happens though.