Skip to content

Commit 01be97c

Browse files
stefanbellergitster
authored andcommitted
diff.c: get rid of duplicate implementation
The implementations in diff.c to detect moved lines needs to compare strings and hash strings, which is implemented in that file, as well as in the xdiff library. Remove the rather recent implementation in diff.c and rely on the well exercised code in the xdiff lib. With this change the hash used for bucketing the strings for the moved line detection changes from FNV32 (that is provided via the hashmaps memhash) to DJB2 (which is used internally in xdiff). Benchmarks found on the web[1] do not indicate that these hashes are different in performance for readable strings. [1] https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5ec8274 commit 01be97c

File tree

1 file changed

+4
-78
lines changed

1 file changed

+4
-78
lines changed

diff.c

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -707,88 +707,14 @@ struct moved_entry {
707707
struct moved_entry *next_line;
708708
};
709709

710-
static int next_byte(const char **cp, const char **endp,
711-
const struct diff_options *diffopt)
712-
{
713-
int retval;
714-
715-
if (*cp >= *endp)
716-
return -1;
717-
718-
if (isspace(**cp)) {
719-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
720-
while (*cp < *endp && isspace(**cp))
721-
(*cp)++;
722-
/*
723-
* After skipping a couple of whitespaces,
724-
* we still have to account for one space.
725-
*/
726-
return (int)' ';
727-
}
728-
729-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
730-
while (*cp < *endp && isspace(**cp))
731-
(*cp)++;
732-
/*
733-
* return the first non-ws character via the usual
734-
* below, unless we ate all of the bytes
735-
*/
736-
if (*cp >= *endp)
737-
return -1;
738-
}
739-
}
740-
741-
retval = (unsigned char)(**cp);
742-
(*cp)++;
743-
return retval;
744-
}
745-
746710
static int moved_entry_cmp(const struct diff_options *diffopt,
747711
const struct moved_entry *a,
748712
const struct moved_entry *b,
749713
const void *keydata)
750714
{
751-
const char *ap = a->es->line, *ae = a->es->line + a->es->len;
752-
const char *bp = b->es->line, *be = b->es->line + b->es->len;
753-
754-
if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
755-
return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
756-
757-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
758-
while (ae > ap && isspace(ae[-1]))
759-
ae--;
760-
while (be > bp && isspace(be[-1]))
761-
be--;
762-
}
763-
764-
while (1) {
765-
int ca, cb;
766-
ca = next_byte(&ap, &ae, diffopt);
767-
cb = next_byte(&bp, &be, diffopt);
768-
if (ca != cb)
769-
return 1;
770-
if (ca < 0)
771-
return 0;
772-
}
773-
}
774-
775-
static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
776-
{
777-
if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
778-
static struct strbuf sb = STRBUF_INIT;
779-
const char *ap = es->line, *ae = es->line + es->len;
780-
int c;
781-
782-
strbuf_reset(&sb);
783-
while (ae > ap && isspace(ae[-1]))
784-
ae--;
785-
while ((c = next_byte(&ap, &ae, o)) >= 0)
786-
strbuf_addch(&sb, c);
787-
788-
return memhash(sb.buf, sb.len);
789-
} else {
790-
return memhash(es->line, es->len);
791-
}
715+
return !xdiff_compare_lines(a->es->line, a->es->len,
716+
b->es->line, b->es->len,
717+
diffopt->xdl_opts);
792718
}
793719

794720
static struct moved_entry *prepare_entry(struct diff_options *o,
@@ -797,7 +723,7 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
797723
struct moved_entry *ret = xmalloc(sizeof(*ret));
798724
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
799725

800-
ret->ent.hash = get_string_hash(l, o);
726+
ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
801727
ret->es = l;
802728
ret->next_line = NULL;
803729

0 commit comments

Comments
 (0)