Skip to content

Commit

Permalink
fix get_string_pos and improve performance (#288)
Browse files Browse the repository at this point in the history
Don't attempt to advance past the end of the string. Also, don't use
substring(), which will duplicate the string.
  • Loading branch information
Prince781 committed Apr 8, 2023
1 parent 36c051a commit 3b5fe94
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/util.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,25 @@ namespace Vls.Util {
* Both lineno and charno must be zero-indexed.
*/
public static size_t get_string_pos (string str, uint lineno, uint charno) {
int linepos = -1;
int pos = 0;
unowned string curstr = str;

for (uint lno = 0; lno < lineno; ++lno) {
int pos = str.index_of_char ('\n', linepos + 1);
if (pos == -1)
int rel_idx = curstr.index_of_char ('\n');
if (rel_idx == -1)
break;
linepos = pos;
pos += rel_idx;
curstr = curstr.offset (rel_idx);
if (curstr[1] != '\0') {
// skip past the newline
pos++;
curstr = curstr.offset (1);
} else {
break;
}
}

string remaining_str = str.substring (linepos + 1);

return linepos + 1 + remaining_str.index_of_nth_char (charno);
return pos + curstr.index_of_nth_char (charno);
}

/**
Expand Down

0 comments on commit 3b5fe94

Please sign in to comment.