Use QString::count(QChar)+1 over QString::splitRef().length()
ClosedPublic

Authored by kossebau on Sep 2 2019, 12:03 PM.

Details

Diff Detail

Repository
R39 KTextEditor
Branch
morecountoversplit
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 16314
Build 16332: arc lint + arc unit
kossebau created this revision.Sep 2 2019, 12:03 PM
Restricted Application added projects: Kate, Frameworks. · View Herald TranscriptSep 2 2019, 12:03 PM
Restricted Application added subscribers: kde-frameworks-devel, kwrite-devel. · View Herald Transcript
kossebau requested review of this revision.Sep 2 2019, 12:03 PM
apol added a subscriber: apol.Sep 2 2019, 1:10 PM
apol added inline comments.
src/vimode/modes/normalvimode.cpp
3848 ↗(On Diff #65228)

oh I see, how about just using splitRef? This way we don't have to allocate all the strings but the code still is more readable.

Hmm, is lastLineLenght correct? Must one not decrement by one?

Hmm, is lastLineLenght correct? Must one not decrement by one?

Yes, someone missed in their brain to properly turn - (x + 1) into - x - 1, just happened again with me when redoing the code in brain on first try, darn.
As main idea of logic is: "fulllength" - "length of all chars up to last linebreak", so "length" - "lastindex + 1"

So example:

012345 <- pos
CCCNCC <- pasted text, C: char, N: newline
->
size: 6
last N pos: 3
last line length: 2

const int lastLineLength = 6 - (3 + 1) -> 2,

Adding brackets now, so the original idea is visible

src/vimode/modes/normalvimode.cpp
3848 ↗(On Diff #65228)

Still one allocation done for the vector ;)

Not sure about readibility. While the mistake done in formula spoils my point, I find the new code more readable, as the splitref would make me expect more sections are used, while it is just a hack to get the precalculated length of the last section, with info about all other sections discarded.
New code has less surprises to me. but two more error-prone calculations. Guess this is up to opinions, so up to maintainer :)

kossebau updated this revision to Diff 65738.Sep 10 2019, 11:50 AM

use brackets for correct logic

cullmann accepted this revision.Sep 10 2019, 12:03 PM

Thanks!

This revision is now accepted and ready to land.Sep 10 2019, 12:03 PM
This revision was automatically updated to reflect the committed changes.