read-cache: add strcmp_offset function
Add strcmp_offset() function to also return the offset of the first change. Add unit test and helper to verify. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
cf11a67975
commit
a6db3fbb6e
20
read-cache.c
20
read-cache.c
@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Like strcmp(), but also return the offset of the first change.
|
||||
* If strings are equal, return the length.
|
||||
*/
|
||||
int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
|
||||
{
|
||||
size_t k;
|
||||
|
||||
if (!first_change)
|
||||
return strcmp(s1, s2);
|
||||
|
||||
for (k = 0; s1[k] == s2[k]; k++)
|
||||
if (s1[k] == '\0')
|
||||
break;
|
||||
|
||||
*first_change = k;
|
||||
return (unsigned char)s1[k] - (unsigned char)s2[k];
|
||||
}
|
||||
|
||||
/*
|
||||
* Do we have another file with a pathname that is a proper
|
||||
* subset of the name we're trying to add?
|
||||
|
Reference in New Issue
Block a user