From 757c91e2de250b69f27843b01ab9b8c7deb5a045 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 9 Mar 2019 15:47:28 +0000 Subject: [PATCH] Extra ptrlen function, ptrlen_endswith(). Identical to ptrlen_startswith(), only the other way round. --- misc.h | 1 + utils.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/misc.h b/misc.h index f2919af3..25ac6d15 100644 --- a/misc.h +++ b/misc.h @@ -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" */ diff --git a/utils.c b/utils.c index a85a5d29..96024d2f 100644 --- a/utils.c +++ b/utils.c @@ -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);