mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 01:48:00 +00:00
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:
parent
c02031ffd6
commit
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_string(ptrlen pl, const char *str);
|
||||||
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
|
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
|
||||||
|
int ptrlen_strcmp(ptrlen pl1, ptrlen pl2);
|
||||||
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
|
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
|
||||||
char *mkstr(ptrlen pl);
|
char *mkstr(ptrlen pl);
|
||||||
int string_length_for_printf(size_t);
|
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));
|
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)
|
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
|
||||||
{
|
{
|
||||||
if (whole.len >= prefix.len &&
|
if (whole.len >= prefix.len &&
|
||||||
|
Loading…
Reference in New Issue
Block a user