Skip to content

Commit

Permalink
util: added string and string array cmp utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomaschewski committed Mar 9, 2016
1 parent 788a9a3 commit 8cfa473
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/wicked/util.h
Expand Up @@ -112,6 +112,8 @@ extern void ni_string_array_comm(const ni_string_array_t *a, const ni_string_ar
ni_string_array_t *uniq_b,
ni_string_array_t *common);
extern int ni_string_array_is_uniq(const ni_string_array_t *);
extern ni_bool_t ni_string_array_eq(const ni_string_array_t *, const ni_string_array_t *);
extern int ni_string_array_cmp(const ni_string_array_t *, const ni_string_array_t *);

extern void ni_uint_array_init(ni_uint_array_t *);
extern void ni_uint_array_destroy(ni_uint_array_t *);
Expand Down Expand Up @@ -273,6 +275,25 @@ ni_string_eq_nocase(const char *a, const char *b)
return strcasecmp(a, b) == 0;
}

static inline int
ni_string_cmp(const char *a, const char *b)
{
if (a == NULL || b == NULL)
return a > b ? 1 : -1;
else
return strcmp(a, b);
}

static inline int
ni_string_cmp_nocase(const char *a, const char *b)
{
if (a == NULL || b == NULL)
return a > b ? 1 : -1;
else
return strcasecmp(a, b);
}


static inline ni_bool_t
ni_string_contains(const char *haystack, const char *needle)
{
Expand Down
24 changes: 24 additions & 0 deletions src/util.c
Expand Up @@ -316,6 +316,30 @@ ni_string_array_is_uniq(const ni_string_array_t *nsa)
return 1;
}

int
ni_string_array_cmp(const ni_string_array_t *la, const ni_string_array_t *ra)
{
unsigned int i;
int ret;

if (!la || !ra)
return la > ra ? 1 : -1;

if (la->count != ra->count)
return la->count > ra->count ? 1 : -1;

for (ret = i = 0; i < la->count && !ret; ++i)
ret = ni_string_cmp(la->data[i], ra->data[i]);

return ret;
}

ni_bool_t
ni_string_array_eq(const ni_string_array_t *la, const ni_string_array_t *ra)
{
return ni_string_array_cmp(la, ra) == 0;
}

/*
* Array of unsigned integers
*/
Expand Down

0 comments on commit 8cfa473

Please sign in to comment.