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.
This commit is contained in:
Kartik K. Agaram 2022-05-19 21:52:38 -07:00
parent f7ff4dc9c2
commit 839d2df3ea
2 changed files with 8 additions and 2 deletions

View File

@ -2,6 +2,7 @@ file load:
cursor_line = 1
first line is a drawing -> cursor_line = 2
click on text -> cursor moves
click on first character of text -> cursor on first character of text
click on drawing -> cursor doesn't move
create drawing -> cursor bumps down below drawing
backspace

View File

@ -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)