From 9ab1c0eb13f5eacad108341e0616a82e4f36bcec Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Thu, 30 Jan 2025 13:32:37 +0000 Subject: [PATCH] utils: Factor out _srt_string_ends_with_len() If the length is already known, this avoids recalculating it (and if the suffix contains embedded NULs, this makes it possible to check for). Signed-off-by: Simon McVittie <smcv@collabora.com> --- steam-runtime-tools/utils-internal.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/steam-runtime-tools/utils-internal.h b/steam-runtime-tools/utils-internal.h index 732a9837d..a7d8e098c 100644 --- a/steam-runtime-tools/utils-internal.h +++ b/steam-runtime-tools/utils-internal.h @@ -604,6 +604,23 @@ gboolean _srt_string_read_fd_until_eof (GString *buf, int fd, GError **error); +/* + * _srt_string_ends_with_len: + * @str: A #GString + * @suffix: A suffix + * @len: Length of suffix + * + * Returns: %TRUE if @str ends with @suffix + */ +static inline gboolean +_srt_string_ends_with_len (const GString *str, + const char *suffix, + gsize len) +{ + return (str->len >= len + && memcmp (str->str + str->len - len, suffix, len) == 0); +} + /* * _srt_string_ends_with: * @str: A #GString @@ -615,10 +632,7 @@ static inline gboolean _srt_string_ends_with (const GString *str, const char *suffix) { - size_t len = strlen (suffix); - - return (str->len >= len - && strcmp (str->str + str->len - len, suffix) == 0); + return _srt_string_ends_with_len (str, suffix, strlen (suffix)); } gboolean _srt_is_identifier (const char *name); -- GitLab