[systemd-devel] [PATCH] inline static endswith()
Zbigniew Jędrzejewski-Szmek
zbyszek at in.waw.pl
Fri Aug 23 08:10:46 PDT 2013
On Fri, Aug 23, 2013 at 03:36:51PM +0800, WANG Chao wrote:
> ---
> src/shared/util.c | 21 ---------------------
> src/shared/util.h | 9 ++++++++-
> 2 files changed, 8 insertions(+), 22 deletions(-)
>
> diff --git a/src/shared/util.c b/src/shared/util.c
> index 1dde8af..3a22000 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -107,27 +107,6 @@ bool streq_ptr(const char *a, const char *b) {
> return false;
> }
>
> -char* endswith(const char *s, const char *postfix) {
> - size_t sl, pl;
> -
> - assert(s);
> - assert(postfix);
> -
> - sl = strlen(s);
> - pl = strlen(postfix);
> -
> - if (pl == 0)
> - return (char*) s + sl;
> -
> - if (sl < pl)
> - return NULL;
> -
> - if (memcmp(s + sl - pl, postfix, pl) != 0)
> - return NULL;
> -
> - return (char*) s + sl - pl;
> -}
> -
> bool first_word(const char *s, const char *word) {
> size_t sl, wl;
>
> diff --git a/src/shared/util.h b/src/shared/util.h
> index 63f4e3d..0dab4cb 100644
> --- a/src/shared/util.h
> +++ b/src/shared/util.h
> @@ -118,7 +118,14 @@ static inline const char *startswith_no_case(const char *s, const char *prefix)
> return NULL;
> }
>
> -char *endswith(const char *s, const char *postfix) _pure_;
> +static inline char *endswith(const char *s, const char *postfix) {
> + size_t sl = strlen(s);
> + size_t pl = strlen(postfix);
> +
> + if (sl > pl && strncmp(s + sl -pl, postfix, pl) == 0)
> + return (char *) s + sl -pl;
> + return NULL;
> +}
Hm, you're replacing a memcmp with strncmp. memcmp is actually faster,
because strncmp does check for '\0', which memcmp doesn't have to do,
and can thus e.g. compare from the end, which is faster on some
architectures.
Also, endswith is not that trivial — three function calls and
some arithmetic, and it's used in many places. So in the end,
I don't think that this patch will *increase* our footprint.
Zbyszek
More information about the systemd-devel
mailing list