git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] tree-walk: simplify via strnncmp()

From
Jeremiah Mahler <jmmahler@gmail.com>
Date
Jun 17, 2014, 07:13 UTC
Message-ID
<20140617071332.GA3940@hudson.localdomain>
In-Reply-To
<20140616201806.GO8557@google.com>
Jonathan,
On Mon, Jun 16, 2014 at 01:18:06PM -0700, Jonathan Nieder wrote:
Show 15 quoted lines
> Jeremiah Mahler wrote:
> 
> > --- a/tree-walk.c
> > +++ b/tree-walk.c
> [...]
> > @@ -174,7 +164,7 @@ static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
> >  	 * scanning further.
> >  	 */
> >  
> > -	int cmp = name_compare(a, a_len, b, b_len);
> > +	int cmp = strnncmp(a, a_len, b, b_len);
> 
> This changes behavior: the old version would only have 0 < cmp if
> 'a' comes after 'b', while the new version always has 0 < cmp when
> a != b.

Thanks for catching this. I did not realize that when I tried to cleanup the logic I inadvertently changed its behavior.

    int strnncmp(const char *a, int len_a, const char *b, int len_b)
    {
           int min_len = (len_a < len_b) ? len_a : len_b;
           return (memcmp(a, b, min_len) || (len_a - len_b));
    }
is not the same as:
    static int name_compare(const char *a, int a_len,
                           const char *b, int b_len)
    {
           int len = (a_len < b_len) ? a_len : b_len;
           int cmp = memcmp(a, b, len);
           if (cmp)
                   return cmp;
           return (a_len - b_len);
    }
(-5 || 3) is 1, not -5.
-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler
Previous: Jonathan NiederNext: Jeremiah Mahler
Message 6 of 8 in “add strnncmp() function”
  1. 0/3 add strnncmp() functionJeremiah Mahler, Jun 16, 2014
  2. 1/3 add strnncmp() functionJeremiah Mahler, Jun 16, 2014
  3. Jonathan NiederJun 16, 2014
  4. 2/3 tree-walk: simplify via strnncmp()Jeremiah Mahler, Jun 16, 2014
  5. Jonathan NiederJun 16, 2014
  6. Jeremiah MahlerJun 17, 2014
  7. 3/3 unpack-trees: simplify via strnncmp()Jeremiah Mahler, Jun 16, 2014
  8. Jonathan NiederJun 16, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.