[systemd-devel] [PATCH] inline static endswith()
WANG Chao
chaowang at redhat.com
Sun Aug 25 22:15:12 PDT 2013
On 08/23/13 at 05:10pm, Zbigniew Jędrzejewski-Szmek wrote:
> 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.
So if I use memcmp instead of strncmp in this patch, it'd make sense to
you? How about the new startswith, memcmp can be used there as well.
>
> 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.
Are you suggesting that it's better leave endswith as it is for now?
Or do you think it works for you to use a static inline endswith which
invokes memcmp?
Thanks for review!
WANG Chao
More information about the systemd-devel
mailing list