about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-19 21:52:38 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-19 21:53:08 -0700
commit839d2df3eaa1bf29f00ebec8eeba4b8c8a158852 (patch)
treee0df204f44f8d9f4146488da4cfa295414af743c /text.lua
parentf7ff4dc9c270520860f0879d317dcf2fad5f77ec (diff)
downloadview.love-839d2df3eaa1bf29f00ebec8eeba4b8c8a158852.tar.gz
bugfix
As usual, binary search is hard to get right. This time I was never
actually selecting between left and right when they were just one
character apart.
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua9
1 files changed, 7 insertions, 2 deletions
diff --git a/text.lua b/text.lua
index 7a442e8..97dcf7c 100644
--- a/text.lua
+++ b/text.lua
@@ -298,20 +298,25 @@ function Text.nearest_cursor_pos(line, x, hint)
       left = hint
     end
   end
-  while left < right-1 do
+--?   print('--')
+  while true do
     local curr = math.floor((left+right)/2)
     local currxmin = Text.cursor_x(line, curr)
     local currxmax = Text.cursor_x(line, curr+1)
+--?     print(x, left, right, curr, currxmin, currxmax)
     if currxmin <= x and x < currxmax then
       return curr
     end
+    if left >= right-1 then
+      return right
+    end
     if currxmin > x then
       right = curr
     else
       left = curr
     end
   end
-  return right
+  assert(false)
 end
 
 function Text.cursor_x(line_data, cursor_pos)