From: Pierre Habouzit Date: Sun, 07 Oct 2007 14:39:12 GMT Subject: Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit Message-ID: <20071007143912.GB10024@artemis.corp> In-Reply-To: <20071007172425.bb691da9.tihirvon@gmail.com> On Sun, Oct 07, 2007 at 02:24:25PM +0000, Timo Hirvonen wrote: > Alex Riesen wrote: > > > +static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) > > +{ > > + int len = a->len < b->len ? a->len: b->len; > > + int cmp = memcmp(a->buf, b->buf, len); > > + if (cmp) > > + return cmp; > > + return a->len < b->len ? -1: a->len != b->len; > > +} > > strbuf->buf is always non-NULL and NUL-terminated so you could just do > > static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) > { > int len = a->len < b->len ? a->len : b->len; > return memcmp(a->buf, b->buf, len + 1); > } doesn't work, because a buffer can have (in some very specific cases) an embeded NUL. -- ·O· Pierre Habouzit ··O madcoder@debian.org OOO http://www.madism.org