From ea0a23645f03a42252ab1ce8df45ae4076ebae75 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 28 Dec 2025 16:23:22 +0100 Subject: teco_string_t is now passed by value like a scalar if the callee isn't expected to modify it * When passing a struct that should not be modified, I usually use a const pointer. * Strings however are small 2-word objects and they are often now already passed via separate `gchar*` and gsize parameters. So it is consistent to pass teco_string_t by value as well. A teco_string_t will usually fit into registers just like a pointer. * It's now obvious which function just _uses_ and which function _modifies_ a string. There is also no chance to pass a NULL pointer to those functions. --- src/search.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'src/search.c') diff --git a/src/search.c b/src/search.c index 2a39b59..d6601ae 100644 --- a/src/search.c +++ b/src/search.c @@ -537,7 +537,7 @@ teco_pattern2regexp(teco_string_t *pattern, teco_machine_qregspec_t *qreg_machin if (state == TECO_SEARCH_STATE_ALT) teco_string_append_c(&re, ')'); - g_assert(!teco_string_contains(&re, '\0')); + g_assert(!teco_string_contains(re, '\0')); return g_steal_pointer(&re.data) ? : g_strdup(""); } @@ -696,7 +696,7 @@ teco_do_search(GRegex *re, gsize from, gsize to, gint *count, GError **error) } static gboolean -teco_state_search_process(teco_machine_main_t *ctx, const teco_string_t *str, gsize new_chars, GError **error) +teco_state_search_process(teco_machine_main_t *ctx, teco_string_t str, gsize new_chars, GError **error) { /* FIXME: Should G_REGEX_OPTIMIZE be added under certain circumstances? */ GRegexCompileFlags flags = G_REGEX_MULTILINE | G_REGEX_DOTALL; @@ -741,10 +741,9 @@ teco_state_search_process(teco_machine_main_t *ctx, const teco_string_t *str, gs qreg_machine = teco_machine_qregspec_new(TECO_QREG_REQUIRED, ctx->qreg_table_locals, FALSE); g_autoptr(GRegex) re = NULL; - teco_string_t pattern = *str; g_autofree gchar *re_pattern; /* NOTE: teco_pattern2regexp() modifies str pointer */ - re_pattern = teco_pattern2regexp(&pattern, qreg_machine, + re_pattern = teco_pattern2regexp(&str, qreg_machine, ctx->expectstring.machine.codepage, FALSE, error); if (!re_pattern) return FALSE; @@ -829,7 +828,7 @@ failure: } static teco_state_t * -teco_state_search_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_search_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; @@ -837,14 +836,14 @@ teco_state_search_done(teco_machine_main_t *ctx, const teco_string_t *str, GErro teco_qreg_t *search_reg = teco_qreg_table_find(&teco_qreg_table_globals, "_", 1); g_assert(search_reg != NULL); - if (str->len > 0) { + if (str.len > 0) { /* workaround: preserve selection (also on rubout) */ gint anchor = teco_interface_ssm(SCI_GETANCHOR, 0, 0); if (teco_current_doc_must_undo()) undo__teco_interface_ssm(SCI_SETANCHOR, anchor, 0); if (!search_reg->vtable->undo_set_string(search_reg, error) || - !search_reg->vtable->set_string(search_reg, str->data, str->len, + !search_reg->vtable->set_string(search_reg, str.data, str.len, teco_default_codepage(), error)) return NULL; @@ -853,7 +852,7 @@ teco_state_search_done(teco_machine_main_t *ctx, const teco_string_t *str, GErro g_auto(teco_string_t) search_str = {NULL, 0}; if (!search_reg->vtable->get_string(search_reg, &search_str.data, &search_str.len, NULL, error) || - !teco_state_search_process(ctx, &search_str, search_str.len, error)) + !teco_state_search_process(ctx, search_str, search_str.len, error)) return NULL; } @@ -1016,7 +1015,7 @@ teco_state_search_all_initial(teco_machine_main_t *ctx, GError **error) } static teco_state_t * -teco_state_search_all_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_search_all_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; @@ -1080,7 +1079,7 @@ TECO_DEFINE_STATE_SEARCH(teco_state_search_all, ); static teco_state_t * -teco_state_search_kill_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_search_kill_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; @@ -1164,7 +1163,7 @@ TECO_DEFINE_STATE_SEARCH(teco_state_search_kill, ); static teco_state_t * -teco_state_search_delete_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_search_delete_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; @@ -1237,7 +1236,7 @@ static TECO_DEFINE_STATE_INSERT(teco_state_replace_insert, ); static teco_state_t * -teco_state_replace_ignore_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_ignore_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { return &teco_state_start; } @@ -1247,7 +1246,7 @@ static TECO_DEFINE_STATE_EXPECTSTRING(teco_state_replace_ignore, ); static teco_state_t * -teco_state_replace_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_replace_ignore; @@ -1292,7 +1291,7 @@ TECO_DEFINE_STATE_SEARCH(teco_state_replace, ); static teco_state_t * -teco_state_replace_default_insert_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_default_insert_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; @@ -1300,16 +1299,16 @@ teco_state_replace_default_insert_done(teco_machine_main_t *ctx, const teco_stri teco_qreg_t *replace_reg = teco_qreg_table_find(&teco_qreg_table_globals, "-", 1); g_assert(replace_reg != NULL); - if (str->len > 0) { + if (str.len > 0) { if (!replace_reg->vtable->undo_set_string(replace_reg, error) || - !replace_reg->vtable->set_string(replace_reg, str->data, str->len, + !replace_reg->vtable->set_string(replace_reg, str.data, str.len, teco_default_codepage(), error)) return NULL; } else { g_auto(teco_string_t) replace_str = {NULL, 0}; if (!replace_reg->vtable->get_string(replace_reg, &replace_str.data, &replace_str.len, NULL, error) || - (replace_str.len > 0 && !teco_state_insert_process(ctx, &replace_str, replace_str.len, error))) + (replace_str.len > 0 && !teco_state_insert_process(ctx, replace_str, replace_str.len, error))) return NULL; } @@ -1324,17 +1323,17 @@ static TECO_DEFINE_STATE_INSERT(teco_state_replace_default_insert, ); static teco_state_t * -teco_state_replace_default_ignore_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_default_ignore_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL || - !str->len) + !str.len) return &teco_state_start; teco_qreg_t *replace_reg = teco_qreg_table_find(&teco_qreg_table_globals, "-", 1); g_assert(replace_reg != NULL); if (!replace_reg->vtable->undo_set_string(replace_reg, error) || - !replace_reg->vtable->set_string(replace_reg, str->data, str->len, + !replace_reg->vtable->set_string(replace_reg, str.data, str.len, teco_default_codepage(), error)) return NULL; @@ -1346,7 +1345,7 @@ static TECO_DEFINE_STATE_EXPECTSTRING(teco_state_replace_default_ignore, ); static teco_state_t * -teco_state_replace_default_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_default_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_replace_default_ignore; @@ -1390,7 +1389,7 @@ TECO_DEFINE_STATE_SEARCH(teco_state_replace_default, ); static teco_state_t * -teco_state_replace_default_all_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) +teco_state_replace_default_all_done(teco_machine_main_t *ctx, teco_string_t str, GError **error) { if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_replace_default_ignore; -- cgit v1.2.3