Skip to content

Commit

Permalink
Extend str_match() and str_casematch() APIs to work on any combination
Browse files Browse the repository at this point in the history
of str and str_const's.
  • Loading branch information
sobomax committed Jan 29, 2021
1 parent 2d9b513 commit ce8b994
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
20 changes: 20 additions & 0 deletions lib/str2const.h
Expand Up @@ -43,6 +43,24 @@
_Generic(*(sin), str: _unescape_paramSS, str_const: _unescape_param)(sin, sout) \
)

#define str_match(_a, _b) _Generic(*(_a), \
str: _Generic(*(_b), \
str: _str_matchSS, \
str_const: _str_matchSC), \
str_const: _Generic(*(_b), \
str: _str_matchCS, \
str_const: _str_matchCC) \
)(_a, _b)

#define str_casematch(_a, _b) _Generic(*(_a), \
str: _Generic(*(_b), \
str: _str_casematchSS, \
str_const: _str_casematchSC), \
str_const: _Generic(*(_b), \
str: _str_casematchCS, \
str_const: _str_casematchCC) \
)(_a, _b)

#define str_strcmp(_a, _b) _Generic(*(_a), \
str: _Generic(*(_b), \
str: _str_strcmpSS, \
Expand All @@ -57,6 +75,8 @@
#define unescape_user(sin, sout) _unescape_user(str2const(sin), sout)
#define escape_param(sin, sout) _escape_param(str2const(sin), sout)
#define unescape_param(sin, sout) _unescape_param(str2const(sin), sout)
#define str_match(_a, _b) _str_matchCC(str2const(_a), str2const(_b))
#define str_casematch(_a, _b) _str_casematchCC(str2const(_a), str2const(_b))
#define str_strcmp(_a, _b) _str_strcmpCC(str2const(_a), str2const(_b))
#endif /* HAVE_GENERICS */

Expand Down
32 changes: 27 additions & 5 deletions ut.h
Expand Up @@ -951,18 +951,29 @@ static inline int pkg_str_extend(str *in, int size)
/*
* test if two str's are equal
*/
static inline int str_match(const str *a, const str *b)
static inline int _str_matchCC(const str_const *a, const str_const *b)
{
return a->len == b->len && !memcmp(a->s, b->s, a->len);
}

static inline int _str_matchSS(const str *a, const str *b)
{
return _str_matchCC(str2const(a), str2const(b));
}
static inline int _str_matchSC(const str *a, const str_const *b)
{
return _str_matchCC(str2const(a), b);
}
static inline int _str_matchCS(const str_const *a, const str *b)
{
return _str_matchCC(a, str2const(b));
}

/*
* test if two str's are equal, case-insensitive
*/
static inline int str_casematch(const str *a, const str *b)
static inline int _str_casematchCC(const str_const *a, const str_const *b)
{
char *p, *q, *end;
const char *p, *q, *end;

if (a->len != b->len)
return 0;
Expand All @@ -982,7 +993,18 @@ static inline int str_casematch(const str *a, const str *b)

return 1;
}

static inline int _str_casematchSS(const str *a, const str *b)
{
return _str_casematchCC(str2const(a), str2const(b));
}
static inline int _str_casematchSC(const str *a, const str_const *b)
{
return _str_casematchCC(str2const(a), b);
}
static inline int _str_casematchCS(const str_const *a, const str *b)
{
return _str_casematchCC(a, str2const(b));
}

/*
* compare two str's
Expand Down

0 comments on commit ce8b994

Please sign in to comment.