From 3b5fe941bea6ecd5c8fcb84930c40b8582eea165 Mon Sep 17 00:00:00 2001 From: Princeton Ferro Date: Thu, 30 Mar 2023 01:57:16 -0400 Subject: [PATCH] fix get_string_pos and improve performance (#288) Don't attempt to advance past the end of the string. Also, don't use substring(), which will duplicate the string. --- src/util.vala | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/util.vala b/src/util.vala index 1f726028..cd079f20 100644 --- a/src/util.vala +++ b/src/util.vala @@ -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); } /**