Provide str_busdev_eq() function

This commit is contained in:
stefanrueger
2025-10-21 13:58:24 +02:00
parent 1f901210c7
commit 881c8f80aa
2 changed files with 18 additions and 0 deletions

View File

@@ -1678,6 +1678,7 @@ extern "C" {
int str_casematched_by(const char *string, const char *pattern);
int str_is_pattern(const char *str);
int str_is_in_list(const char *s, const char **l, size_t nl, int (*f)(const char *, const char *));
int str_busdev_eq(const char *s, const char *t);
char *str_sprintf(const char *fmt, ...)
#if defined(__GNUC__) // Ask gcc to check whether format and parameters match
__attribute__((format(printf, 1, 2)))

View File

@@ -241,6 +241,23 @@ int str_is_in_list(const char *s, const char **l, size_t nl, int (*f)(const char
return 0;
}
// Do the two strings represent the same decimal number or are they the same up to a limiting colon?
int str_busdev_eq(const char *s, const char *t) {
if(*s >= '0' && *s <='9' && *t >= '0' && *t <='9') // Both look like a decimal number
if(strtoull(s, NULL, 10) == strtoull(t, NULL, 10))
return 1;
int len = 0; // Length of strings up to optional colon
while(s[len] && s[len] != ':' && t[len] && t[len] != ':')
len++;
// Different length means strings are not the same
if((s[len] && s[len] != ':') || (t[len] && t[len] != ':'))
return 0;
return !strncmp(s, t, len);
}
// Return a mmt_malloc'd string with the sprintf() result
char *str_sprintf(const char *fmt, ...) {
int size = 0;