1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

Extra ptrlen function, ptrlen_endswith().

Identical to ptrlen_startswith(), only the other way round.
This commit is contained in:
Simon Tatham 2019-03-09 15:47:28 +00:00
parent 5eb6c19047
commit 757c91e2de
2 changed files with 15 additions and 0 deletions

1
misc.h
View File

@ -169,6 +169,7 @@ 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);
bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail);
char *mkstr(ptrlen pl);
int string_length_for_printf(size_t);
/* Derive two printf arguments from a ptrlen, suitable for "%.*s" */

14
utils.c
View File

@ -924,6 +924,20 @@ bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
return false;
}
bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
{
if (whole.len >= suffix.len &&
!memcmp((char *)whole.ptr + (whole.len - suffix.len),
suffix.ptr, suffix.len)) {
if (tail) {
tail->ptr = whole.ptr;
tail->len = whole.len - suffix.len;
}
return true;
}
return false;
}
char *mkstr(ptrlen pl)
{
char *p = snewn(pl.len + 1, char);