Skip to content

Commit 0f4bc9d

Browse files
walaclenticularis39
authored andcommitted
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: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-11-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent d847188 commit 0f4bc9d

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tools/tracing/rtla/src/utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

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

28+
/**
29+
* str_has_prefix - Test if a string has a given prefix
30+
* @str: The string to test
31+
* @prefix: The string to see if @str starts with
32+
*
33+
* Returns: true if @str starts with @prefix, false otherwise
34+
*/
35+
static inline bool str_has_prefix(const char *str, const char *prefix)
36+
{
37+
return strncmp(str, prefix, strlen(prefix)) == 0;
38+
}
39+
2740
#define container_of(ptr, type, member)({ \
2841
const typeof(((type *)0)->member) *__mptr = (ptr); \
2942
(type *)((char *)__mptr - offsetof(type, member)) ; })

0 commit comments

Comments
 (0)