refs: use strncmp() instead of strlen() and memcmp()

Simplify ref_entry_cmp_sslice() by using strncmp() to compare the length-limited key and a NUL-terminated entry. While we're at it, retain the const attribute of the input pointers. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 16, 2013 at 02:08 UTC c971ddfdcd319fb5cad95aa7047c54ebe6a22d1a
1 file changed +4 -6
refs.c
+4 -6
@@ -322,14 +322,12 @@ struct string_slice {
322
323 static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
324 {
325 - struct string_slice *key = (struct string_slice *)key_;
326 - struct ref_entry *ent = *(struct ref_entry **)ent_;
327 - int entlen = strlen(ent->name);
328 - int cmplen = key->len < entlen ? key->len : entlen;
329 - int cmp = memcmp(key->str, ent->name, cmplen);
325 + const struct string_slice *key = key_;
326 + const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
327 + int cmp = strncmp(key->str, ent->name, key->len);
328 if (cmp)
329 return cmp;
332 - return key->len - entlen;
330 + return '\0' - (unsigned char)ent->name[key->len];
331 }
332
333 /*