Thread (1 message) 1 message, 1 author, 2016-06-15

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

From: Miles Bader <hidden>
Date: 2016-06-15 22:43:39

Pierre Habouzit [off-list ref] writes:
quoted
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.
Couldn't you then just do:

   int len = a->len < b->len ? a->len : b->len;
   int cmp = memcmp(a->buf, b->buf, len);
   if (cmp == 0)
      cmp = b->len - a->len;
   return cmp;

[In the case where one string is a prefix of the other, then the longer
one is "greater".]

?

-Miles

-- 
"Suppose He doesn't give a shit?  Suppose there is a God but He
just doesn't give a shit?"  [George Carlin]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help