aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/string-utils.c
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-28 16:23:22 +0100
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-28 20:57:31 +0100
commitea0a23645f03a42252ab1ce8df45ae4076ebae75 (patch)
tree5fe606a9b07f2f7039b1839f9fac6bab2d212c13 /src/string-utils.c
parentd94521fb5b5a5c3a6315c425dba5f1218f0dd323 (diff)
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.
Diffstat (limited to 'src/string-utils.c')
-rw-r--r--src/string-utils.c42
1 files changed, 20 insertions, 22 deletions
diff --git a/src/string-utils.c b/src/string-utils.c
index 10e34a8..573a17b 100644
--- a/src/string-utils.c
+++ b/src/string-utils.c
@@ -98,12 +98,12 @@ teco_string_get_coord(const gchar *str, gsize off, guint *pos, guint *line, guin
* @memberof teco_string_t
*/
gsize
-teco_string_diff(const teco_string_t *a, const gchar *b, gsize b_len)
+teco_string_diff(teco_string_t a, const gchar *b, gsize b_len)
{
gsize len = 0;
- while (len < a->len && len < b_len &&
- a->data[len] == b[len])
+ while (len < a.len && len < b_len &&
+ a.data[len] == b[len])
len++;
return len;
@@ -124,12 +124,12 @@ teco_string_diff(const teco_string_t *a, const gchar *b, gsize b_len)
* @memberof teco_string_t
*/
gsize
-teco_string_casediff(const teco_string_t *a, const gchar *b, gsize b_len)
+teco_string_casediff(teco_string_t a, const gchar *b, gsize b_len)
{
gsize len = 0;
- while (len < a->len && len < b_len) {
- gunichar a_chr = g_utf8_get_char(a->data+len);
+ while (len < a.len && len < b_len) {
+ gunichar a_chr = g_utf8_get_char(a.data+len);
gunichar b_chr = g_utf8_get_char(b+len);
if (g_unichar_tolower(a_chr) != g_unichar_tolower(b_chr))
break;
@@ -141,36 +141,36 @@ teco_string_casediff(const teco_string_t *a, const gchar *b, gsize b_len)
/** @memberof teco_string_t */
gint
-teco_string_cmp(const teco_string_t *a, const gchar *b, gsize b_len)
+teco_string_cmp(teco_string_t a, const gchar *b, gsize b_len)
{
- for (guint i = 0; i < a->len; i++) {
+ for (guint i = 0; i < a.len; i++) {
if (i == b_len)
/* b is a prefix of a */
return 1;
- gint ret = (gint)a->data[i] - (gint)b[i];
+ gint ret = (gint)a.data[i] - (gint)b[i];
if (ret != 0)
/* a and b have a common prefix of length i */
return ret;
}
- return a->len == b_len ? 0 : -1;
+ return a.len == b_len ? 0 : -1;
}
/** @memberof teco_string_t */
gint
-teco_string_casecmp(const teco_string_t *a, const gchar *b, gsize b_len)
+teco_string_casecmp(teco_string_t a, const gchar *b, gsize b_len)
{
- for (guint i = 0; i < a->len; i++) {
+ for (guint i = 0; i < a.len; i++) {
if (i == b_len)
/* b is a prefix of a */
return 1;
- gint ret = (gint)g_ascii_tolower(a->data[i]) - (gint)g_ascii_tolower(b[i]);
+ gint ret = (gint)g_ascii_tolower(a.data[i]) - (gint)g_ascii_tolower(b[i]);
if (ret != 0)
/* a and b have a common prefix of length i */
return ret;
}
- return a->len == b_len ? 0 : -1;
+ return a.len == b_len ? 0 : -1;
}
/**
@@ -184,22 +184,20 @@ teco_string_casecmp(const teco_string_t *a, const gchar *b, gsize b_len)
* @memberof teco_string_t
*/
const gchar *
-teco_string_last_occurrence(const teco_string_t *str, const gchar *chars)
+teco_string_last_occurrence(teco_string_t str, const gchar *chars)
{
- teco_string_t ret = *str;
-
- if (!ret.len)
+ if (!str.len)
return NULL;
do {
- gint i = teco_string_rindex(&ret, *chars);
+ gint i = teco_string_rindex(str, *chars);
if (i >= 0) {
- ret.data += i+1;
- ret.len -= i+1;
+ str.data += i+1;
+ str.len -= i+1;
}
} while (*chars++);
- return ret.data;
+ return str.data;
}
TECO_DEFINE_UNDO_CALL(teco_string_truncate, teco_string_t *, gsize);