aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/string-utils.c
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 /src/string-utils.c
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.
Diffstat (limited to 'src/string-utils.c')
-rw-r--r--src/string-utils.c14
1 files changed, 11 insertions, 3 deletions
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)++;
}
}