Skip to content

Commit

Permalink
stb_textedit: fix find_charpos for n == z to make cursor move correct…
Browse files Browse the repository at this point in the history
…ly when pressing up
  • Loading branch information
blackedout01 committed Oct 3, 2024
1 parent f75e8d1 commit c6fd943
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions stb_textedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,39 +518,17 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s
int z = STB_TEXTEDIT_STRINGLEN(str);
int i=0, first;

if (n == z) {
// if it's at the end, then find the last line -- simpler than trying to
// explicitly handle this case in the regular code
if (single_line) {
STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
find->y = 0;
find->first_char = 0;
find->length = z;
find->height = r.ymax - r.ymin;
find->x = r.x1;
} else {
find->y = 0;
find->x = 0;
find->height = 1;
while (i < z) {
STB_TEXTEDIT_LAYOUTROW(&r, str, i);
prev_start = i;
i += r.num_chars;
}
find->first_char = i;
find->length = 0;
find->prev_first = prev_start;
}
return;
}

// search rows to find the one that straddles character n
find->y = 0;

for(;;) {
STB_TEXTEDIT_LAYOUTROW(&r, str, i);
if (n < i + r.num_chars)
break;
// this is an optimization to avoid checking n == z in every iteration
if (n <= i + r.num_chars) {
if (n < i + r.num_chars || n == z) {
break;
}
}
prev_start = i;
i += r.num_chars;
find->y += r.baseline_y_delta;
Expand Down

0 comments on commit c6fd943

Please sign in to comment.