aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/string-utils.h
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.h
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.h')
-rw-r--r--src/string-utils.h33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/string-utils.h b/src/string-utils.h
index af2c8a1..359329f 100644
--- a/src/string-utils.h
+++ b/src/string-utils.h
@@ -63,6 +63,9 @@ teco_strv_remove(gchar **strv, guint i)
* to functions expecting traditional null-terminated C strings if you can
* guarantee that it contains no null-character other than the trailing one.
*
+ * Since string objects are just two words, they can and should be passed by
+ * value if the callee doesn't have to modify it.
+ *
* @warning For consistency with C idioms the underlying character type is
* `char`, which might be signed!
* Accessing individual characters may yield signed integers and that sign
@@ -164,19 +167,19 @@ gchar *teco_string_echo(const gchar *str, gsize len);
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);
-gsize teco_string_casediff(const teco_string_t *a, const gchar *b, gsize b_len);
+typedef gsize (*teco_string_diff_t)(teco_string_t a, const gchar *b, gsize b_len);
+gsize teco_string_diff(teco_string_t a, const gchar *b, gsize b_len);
+gsize teco_string_casediff(teco_string_t a, const gchar *b, gsize b_len);
-typedef gint (*teco_string_cmp_t)(const teco_string_t *a, const gchar *b, gsize b_len);
-gint teco_string_cmp(const teco_string_t *a, const gchar *b, gsize b_len);
-gint teco_string_casecmp(const teco_string_t *a, const gchar *b, gsize b_len);
+typedef gint (*teco_string_cmp_t)(teco_string_t a, const gchar *b, gsize b_len);
+gint teco_string_cmp(teco_string_t a, const gchar *b, gsize b_len);
+gint teco_string_casecmp(teco_string_t a, const gchar *b, gsize b_len);
/** @memberof teco_string_t */
static inline gboolean
-teco_string_contains(const teco_string_t *str, gchar chr)
+teco_string_contains(teco_string_t str, gchar chr)
{
- return str->data && memchr(str->data, chr, str->len);
+ return str.data && memchr(str.data, chr, str.len);
}
/**
@@ -188,26 +191,26 @@ teco_string_contains(const teco_string_t *str, gchar chr)
* @memberof teco_string_t
*/
static inline gint
-teco_string_rindex(const teco_string_t *str, gchar chr)
+teco_string_rindex(teco_string_t str, gchar chr)
{
gint i;
- for (i = str->len-1; i >= 0 && str->data[i] != chr; i--);
+ for (i = str.len-1; i >= 0 && str.data[i] != chr; i--);
return i;
}
-const gchar *teco_string_last_occurrence(const teco_string_t *str, const gchar *chars);
+const gchar *teco_string_last_occurrence(teco_string_t str, const gchar *chars);
/**
* Validate whether string consists exclusively of valid UTF-8, but accept null bytes.
* @note there is g_utf8_validate_len() in Glib 2.60
*/
static inline gboolean
-teco_string_validate_utf8(const teco_string_t *str)
+teco_string_validate_utf8(teco_string_t str)
{
- const gchar *p = str->data;
- while (!g_utf8_validate(p, str->len - (p - str->data), &p) && !*p)
+ const gchar *p = str.data;
+ while (!g_utf8_validate(p, str.len - (p - str.data), &p) && !*p)
p++;
- return p - str->data == str->len;
+ return p - str.data == str.len;
}
/** @memberof teco_string_t */