Commit 0f4bc9d6 authored by Wander Lairson Costa's avatar Wander Lairson Costa Committed by Tomas Glozar
Browse files

rtla: Add str_has_prefix() helper function



Add a str_has_prefix() helper function that tests whether a string
starts with a given prefix. This function provides a cleaner interface
for prefix matching compared to using strncmp() with strlen() directly.

The function returns a boolean value indicating whether the string
starts with the specified prefix. This helper will be used in
subsequent changes to simplify prefix matching code throughout rtla.

Also add the missing string.h include which is needed for the strlen()
and strncmp() functions used by str_has_prefix() and the existing
strncmp_static() macro.

Signed-off-by: default avatarWander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260309195040.1019085-11-wander@redhat.com


Signed-off-by: default avatarTomas Glozar <tglozar@redhat.com>
parent d847188b
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0

#include <stdint.h>
#include <string.h>
#include <time.h>
#include <sched.h>
#include <stdbool.h>
@@ -24,6 +25,18 @@
/* Compare string with static string, length determined at compile time */
#define strncmp_static(s1, s2) strncmp(s1, s2, ARRAY_SIZE(s2))

/**
 * str_has_prefix - Test if a string has a given prefix
 * @str: The string to test
 * @prefix: The string to see if @str starts with
 *
 * Returns: true if @str starts with @prefix, false otherwise
 */
static inline bool str_has_prefix(const char *str, const char *prefix)
{
	return strncmp(str, prefix, strlen(prefix)) == 0;
}

#define container_of(ptr, type, member)({			\
	const typeof(((type *)0)->member) *__mptr = (ptr);	\
	(type *)((char *)__mptr - offsetof(type, member)) ; })