From a6db3fbb6e4ec11695e2a3af8bc2cb9119cb1941 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Fri, 14 Apr 2017 19:12:28 +0000 Subject: 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 Signed-off-by: Junio C Hamano --- read-cache.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index e44775182..11823f5db 100644 --- a/read-cache.c +++ b/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? -- cgit v1.2.1