aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2024-09-12 13:33:18 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2024-09-12 16:42:08 +0200
commit73d574b71a10d4661ada20275cafde75aff6c1ba (patch)
tree10ba478542edd4f428120c3609f8dfbe8fb79137
parentfe26d83c69c9da6594ba7e27aacc185faa1be161 (diff)
downloadsciteco-73d574b71a10d4661ada20275cafde75aff6c1ba.tar.gz
teco_string_get_coord() returns character offsets now (refs #5)
* This is used for error messages (TECO macro stackframes), so it's important to display columns in characters. * Program counters are in bytes and therefore everywhere gsize. This is by glib convention.
-rw-r--r--src/cmdline.h2
-rw-r--r--src/error.c7
-rw-r--r--src/error.h6
-rw-r--r--src/parser.h2
-rw-r--r--src/string-utils.c14
-rw-r--r--src/string-utils.h2
-rw-r--r--src/undo.c2
-rw-r--r--src/undo.h2
8 files changed, 21 insertions, 16 deletions
diff --git a/src/cmdline.h b/src/cmdline.h
index 78d101c..4aa862c 100644
--- a/src/cmdline.h
+++ b/src/cmdline.h
@@ -46,7 +46,7 @@ typedef struct {
gsize effective_len;
/** Program counter within the command-line macro */
- guint pc;
+ gsize pc;
/**
* Specifies whether the immediate editing modifier
diff --git a/src/error.c b/src/error.c
index 120ed1d..afa2ac1 100644
--- a/src/error.c
+++ b/src/error.c
@@ -37,13 +37,6 @@ guint teco_error_return_args = 0;
*/
guint teco_error_pos = 0, teco_error_line = 0, teco_error_column = 0;
-void
-teco_error_set_coord(const gchar *str, guint pos)
-{
- teco_error_pos = pos;
- teco_string_get_coord(str, pos, &teco_error_line, &teco_error_column);
-}
-
typedef enum {
TECO_FRAME_QREG,
TECO_FRAME_FILE,
diff --git a/src/error.h b/src/error.h
index 7543d02..b12ec80 100644
--- a/src/error.h
+++ b/src/error.h
@@ -143,7 +143,11 @@ teco_error_return_set(GError **error, guint args)
extern guint teco_error_pos, teco_error_line, teco_error_column;
-void teco_error_set_coord(const gchar *str, guint pos);
+static inline void
+teco_error_set_coord(const gchar *str, gsize pos)
+{
+ teco_string_get_coord(str, pos, &teco_error_pos, &teco_error_line, &teco_error_column);
+}
void teco_error_display_short(const GError *error);
void teco_error_display_full(const GError *error);
diff --git a/src/parser.h b/src/parser.h
index ae2cb9b..fe7f559 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -440,7 +440,7 @@ typedef enum {
struct teco_machine_main_t {
teco_machine_t parent;
- gint macro_pc;
+ gsize macro_pc;
/**
* Aliases bitfield with an integer.
diff --git a/src/string-utils.c b/src/string-utils.c
index d9b12e0..b284760 100644
--- a/src/string-utils.c
+++ b/src/string-utils.c
@@ -55,13 +55,20 @@ teco_string_echo(const gchar *str, gsize len)
return ret;
}
-/** @memberof teco_string_t */
+/**
+ * Get character coordinates for a given byte index.
+ *
+ * The given string must be valid UTF-8.
+ *
+ * @memberof teco_string_t
+ */
void
-teco_string_get_coord(const gchar *str, guint pos, guint *line, guint *column)
+teco_string_get_coord(const gchar *str, gsize off, guint *pos, guint *line, guint *column)
{
+ *pos = 0;
*line = *column = 1;
- for (guint i = 0; i < pos; i++) {
+ for (guint i = 0; i < off; i = g_utf8_next_char(str+i) - str) {
switch (str[i]) {
case '\r':
if (str[i+1] == '\n')
@@ -75,6 +82,7 @@ teco_string_get_coord(const gchar *str, guint pos, guint *line, guint *column)
(*column)++;
break;
}
+ (*pos)++;
}
}
diff --git a/src/string-utils.h b/src/string-utils.h
index 806140e..ebe25d5 100644
--- a/src/string-utils.h
+++ b/src/string-utils.h
@@ -162,7 +162,7 @@ void undo__teco_string_truncate(teco_string_t *, gsize);
gchar *teco_string_echo(const gchar *str, gsize len);
-void teco_string_get_coord(const gchar *str, guint pos, guint *line, guint *column);
+void teco_string_get_coord(const gchar *str, gsize off, guint *pos, guint *line, guint *column);
typedef gsize (*teco_string_diff_t)(const teco_string_t *a, const gchar *b, gsize b_len);
gsize teco_string_diff(const teco_string_t *a, const gchar *b, gsize b_len);
diff --git a/src/undo.c b/src/undo.c
index dc54c7a..bc12107 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -112,7 +112,7 @@ teco_undo_push_size(teco_undo_action_t action_cb, gsize size)
}
void
-teco_undo_pop(gint pc)
+teco_undo_pop(gsize pc)
{
while ((gint)teco_undo_heads->len > pc) {
teco_undo_token_t *top =
diff --git a/src/undo.h b/src/undo.h
index 9715c7a..1d1d6fb 100644
--- a/src/undo.h
+++ b/src/undo.h
@@ -243,5 +243,5 @@ TECO_DECLARE_UNDO_SCALAR(gconstpointer);
/** @} */
-void teco_undo_pop(gint pc);
+void teco_undo_pop(gsize pc);
void teco_undo_clear(void);