From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Quint Guvernator <quintus.public@gmail.com>, git@vger.kernel.org
Subject: Re: [PATCH] general style: replaces memcmp() with proper starts_with()
Date: Wed, 12 Mar 2014 12:39:01 -0700 [thread overview]
Message-ID: <xmqqiorjky0a.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <20140312175624.GA7982@sigill.intra.peff.net> (Jeff King's message of "Wed, 12 Mar 2014 13:56:24 -0400")
Jeff King <peff@peff.net> writes:
>> static inline int standard_header_field(const char *field, size_t len)
>> {
>> - return ((len == 4 && !memcmp(field, "tree ", 5)) ||
>> - (len == 6 && !memcmp(field, "parent ", 7)) ||
>> - (len == 6 && !memcmp(field, "author ", 7)) ||
>> - (len == 9 && !memcmp(field, "committer ", 10)) ||
>> - (len == 8 && !memcmp(field, "encoding ", 9)));
>> + return ((len == 4 && starts_with(field, "tree ")) ||
>> + (len == 6 && starts_with(field, "parent ")) ||
>> + (len == 6 && starts_with(field, "author ")) ||
>> + (len == 9 && starts_with(field, "committer ")) ||
>> + (len == 8 && starts_with(field, "encoding ")));
>
> These extra "len" checks are interesting. They look like an attempt to
> optimize lookup, since the caller will already have scanned forward to
> the space.
If one really wants to remove the magic constants from this, then
one must take advantage of the pattern
len == strlen(S) - 1 && !memcmp(field, S, strlen(S))
that appears here, and come up with a simple abstraction to express
that we are only using the string S (e.g. "tree "), length len and
location field of the counted string.
Blindly replacing starts_with() with !memcmp() in the above part is
a readability regression otherwise.
> ... I
> think with a few more helpers we could really further clean up some of
> these callsites.
Yes.
next prev parent reply other threads:[~2014-03-12 19:39 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-12 14:43 [PATCH] general style: replaces memcmp() with proper starts_with() Quint Guvernator
2014-03-12 17:56 ` Jeff King
2014-03-12 19:39 ` Junio C Hamano [this message]
2014-03-12 19:49 ` Jeff King
2014-03-12 20:08 ` Junio C Hamano
2014-03-12 21:14 ` Jeff King
2014-03-12 21:39 ` Jeff King
2014-03-12 22:06 ` Jeff King
2014-03-12 22:38 ` Junio C Hamano
2014-03-13 3:33 ` Quint Guvernator
2014-03-13 17:46 ` Junio C Hamano
2014-03-14 4:57 ` Jeff King
2014-03-14 14:51 ` Quint Guvernator
2014-03-14 16:56 ` Junio C Hamano
2014-03-12 20:51 ` René Scharfe
2014-03-12 21:16 ` David Kastrup
2014-03-12 21:45 ` René Scharfe
2014-03-12 20:52 ` David Kastrup
2014-03-12 22:37 ` Junio C Hamano
2014-03-13 6:27 ` David Kastrup
2014-03-13 17:47 ` Junio C Hamano
2014-03-13 17:55 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqiorjky0a.fsf@gitster.dls.corp.google.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
--cc=quintus.public@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.