Skip to content

Commit

Permalink
teco_string_get_coord() returns character offsets now (refs #5)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
rhaberkorn committed Sep 12, 2024
1 parent fe26d83 commit 73d574b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions src/string-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -75,6 +82,7 @@ teco_string_get_coord(const gchar *str, guint pos, guint *line, guint *column)
(*column)++;
break;
}
(*pos)++;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/string-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 73d574b

Please sign in to comment.