зеркало из https://github.com/github/putty.git
New helper function ptrlen_strcmp().
Compares strings with the same semantics as strcmp, but uses ptrlens as input instead of zero-terminated strings.
This commit is contained in:
Родитель
c02031ffd6
Коммит
d169b04dba
1
misc.h
1
misc.h
|
@ -157,6 +157,7 @@ static inline ptrlen ptrlen_from_strbuf(strbuf *sb)
|
|||
|
||||
bool ptrlen_eq_string(ptrlen pl, const char *str);
|
||||
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
|
||||
int ptrlen_strcmp(ptrlen pl1, ptrlen pl2);
|
||||
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
|
||||
char *mkstr(ptrlen pl);
|
||||
int string_length_for_printf(size_t);
|
||||
|
|
11
utils.c
11
utils.c
|
@ -928,6 +928,17 @@ bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
|
|||
return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
|
||||
}
|
||||
|
||||
int ptrlen_strcmp(ptrlen pl1, ptrlen pl2)
|
||||
{
|
||||
size_t minlen = pl1.len < pl2.len ? pl1.len : pl2.len;
|
||||
if (minlen) { /* tolerate plX.ptr==NULL as long as plX.len==0 */
|
||||
int cmp = memcmp(pl1.ptr, pl2.ptr, minlen);
|
||||
if (cmp)
|
||||
return cmp;
|
||||
}
|
||||
return pl1.len < pl2.len ? -1 : pl1.len > pl2.len ? +1 : 0;
|
||||
}
|
||||
|
||||
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
|
||||
{
|
||||
if (whole.len >= prefix.len &&
|
||||
|
|
Загрузка…
Ссылка в новой задаче