Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328356?usp=email )

Change subject: editor: Avoid text slices for caret positions
......................................................................

editor: Avoid text slices for caret positions

Pass the caret index directly to count and rfind instead of copying the text 
prefix twice.

Preserve line and column results while avoiding temporary strings.

Change-Id: Ibd5e9634511175f97f42b6b2d22ec9d832dae1b3
---
M pywikibot/editor.py
M pywikibot/userinterfaces/gui.py
2 files changed, 4 insertions(+), 4 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/pywikibot/editor.py b/pywikibot/editor.py
index b8bdec0..f58dada 100644
--- a/pywikibot/editor.py
+++ b/pywikibot/editor.py
@@ -78,8 +78,8 @@
             # or to jump to the line of the first occurrence.
             # TODO: Find a better solution than hardcoding these, e.g. a config
             # option.
-            line = text[:jump_index].count('\n')
-            column = jump_index - (text[:jump_index].rfind('\n') + 1)
+            line = text.count('\n', 0, jump_index)
+            column = jump_index - (text.rfind('\n', 0, jump_index) + 1)
         else:
             line = column = 0

diff --git a/pywikibot/userinterfaces/gui.py b/pywikibot/userinterfaces/gui.py
index fddc59e..08ee564 100644
--- a/pywikibot/userinterfaces/gui.py
+++ b/pywikibot/userinterfaces/gui.py
@@ -402,8 +402,8 @@
             self.find_all(highlight)
         if jumpIndex:
             # lines are indexed starting at 1
-            line = text[:jumpIndex].count('\n') + 1
-            column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1)
+            line = text.count('\n', 0, jumpIndex) + 1
+            column = jumpIndex - (text.rfind('\n', 0, jumpIndex) + 1)
             # don't know how to place the caret, but scrolling to the right
             # line should already be helpful.
             self.editbox.see(f'{line}.{column}')

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328356?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ibd5e9634511175f97f42b6b2d22ec9d832dae1b3
Gerrit-Change-Number: 1328356
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to