From 685507922b0b75da5935076395a5b1ec1ef58356 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 25 Jul 2026 01:55:28 +0200 Subject: revised and improved the Unicode glyph-to-byte conversion heuristics Previously almost all glyph-to-byte offset conversions consulted Scintilla's line index and counted characters on the resulting line. For instance a simple expression like `.+1J` would scan the same line twice completely, which would be very slow on pathologically long lines. Even insertions did that due to having to update the ^Y ranges. If you repeat such an operation over all characters as in `<.+1:J;>` you would have complexity O(n^2) for n = line length. Only commands with an explicit relative nature like `C` and `A` would use teco_view_glyph2bytes_relative() which scans beginning at dot as long as the relative movement is less than 1024 glyphs. Wit the new heuristics almost all glyph-to-byte and byte-to-glyph conversions can make use of that optimization. This requires that dot must at all times be known in glyphs as well - the byte position is managed by Scintilla (SCI_GETCURRENTPOS). We therefore introduced teco_current_doc_set_dot() and teco_current_doc_get_dot() to update dot in the current buffer or Q-Register -- it cannot be stored along with the view since Q-Registers share a single view. A number of auxiliary functions have been introduced for converting relative to a known (glyphs,bytes) offset pair and for converting absolute and relative positions with regard to the current doc and SCI_GETCURRENTPOS position. Of course this is error-prone since the glyph and dot positions are interdependant - they must always be kept in sync. With these new optimizations even pathologically long lines can (usually) be managed even in UTF-8 documents. It does not address slow-downs in Scintilla's line layout, yet. grosciteco.tes for instance runs twice as fast now. --- src/qreg.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/qreg.c') diff --git a/src/qreg.c b/src/qreg.c index 8ef82c0..dae4c9b 100644 --- a/src/qreg.c +++ b/src/qreg.c @@ -237,8 +237,10 @@ teco_qreg_plain_get_character(teco_qreg_t *qreg, teco_int_t position, teco_doc_edit(&qreg->string, teco_default_codepage()); + sptr_t curpos = teco_view_ssm(teco_qreg_view, SCI_GETCURRENTPOS, 0, 0); sptr_t len = teco_view_ssm(teco_qreg_view, SCI_GETLENGTH, 0, 0); - gssize off = teco_view_glyphs2bytes(teco_qreg_view, position); + gssize off = teco_view_glyphs2bytes_rel(teco_qreg_view, qreg->string.dot, curpos, + position - qreg->string.dot); *chr = off >= 0 && off != len ? teco_view_get_character(teco_qreg_view, off, len) : -1; @@ -256,8 +258,10 @@ teco_qreg_plain_get_length(teco_qreg_t *qreg, GError **error) teco_doc_edit(&qreg->string, teco_default_codepage()); + sptr_t curpos = teco_view_ssm(teco_qreg_view, SCI_GETCURRENTPOS, 0, 0); sptr_t len = teco_view_ssm(teco_qreg_view, SCI_GETLENGTH, 0, 0); - teco_int_t ret = teco_view_bytes2glyphs(teco_qreg_view, len); + teco_int_t ret = teco_view_bytes2glyphs_rel(teco_qreg_view, qreg->string.dot, + curpos, len - curpos); if (teco_qreg_current) teco_doc_edit(&teco_qreg_current->string, 0); @@ -396,7 +400,7 @@ teco_qreg_plain_new(const gchar *name, gsize len) static gboolean teco_qreg_dot_set_integer(teco_qreg_t *qreg, teco_int_t value, GError **error) { - gssize pos = teco_interface_glyphs2bytes(value); + gssize pos = teco_interface_glyphs2bytes_absdot(value); if (pos < 0) { g_set_error_literal(error, TECO_ERROR, TECO_ERROR_MOVE, "Attempt to move pointer off page when setting Q-Register \":\""); @@ -404,6 +408,11 @@ teco_qreg_dot_set_integer(teco_qreg_t *qreg, teco_int_t value, GError **error) } teco_interface_ssm(SCI_GOTOPOS, pos, 0); + /* + * FIXME: Currently emits undo tokens. + * But we could just get rid of the undo_set_integer() callback. + */ + teco_current_doc_set_dot(value); return TRUE; } @@ -420,8 +429,7 @@ teco_qreg_dot_undo_set_integer(teco_qreg_t *qreg, GError **error) static gboolean teco_qreg_dot_get_integer(teco_qreg_t *qreg, teco_int_t *ret, GError **error) { - sptr_t pos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0); - *ret = teco_interface_bytes2glyphs(pos); + *ret = teco_current_doc_get_dot(); return TRUE; } -- cgit v1.2.3