diff options
Diffstat (limited to 'xdiff/xutils.c')
| -rw-r--r-- | xdiff/xutils.c | 111 |
1 files changed, 86 insertions, 25 deletions
diff --git a/xdiff/xutils.c b/xdiff/xutils.c index 1b899f32c4..bc12f29895 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -190,51 +190,96 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) { int i1, i2; + if (!(flags & XDF_WHITESPACE_FLAGS)) + return s1 == s2 && !memcmp(l1, l2, s1); + + i1 = 0; + i2 = 0; + + /* + * -w matches everything that matches with -b, and -b in turn + * matches everything that matches with --ignore-space-at-eol. + * + * Each flavor of ignoring needs different logic to skip whitespaces + * while we have both sides to compare. + */ if (flags & XDF_IGNORE_WHITESPACE) { - for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) { - if (isspace(l1[i1])) - while (isspace(l1[i1]) && i1 < s1) - i1++; - if (isspace(l2[i2])) - while (isspace(l2[i2]) && i2 < s2) - i2++; - if (i1 < s1 && i2 < s2 && l1[i1++] != l2[i2++]) + goto skip_ws; + while (i1 < s1 && i2 < s2) { + if (l1[i1++] != l2[i2++]) return 0; + skip_ws: + while (i1 < s1 && isspace(l1[i1])) + i1++; + while (i2 < s2 && isspace(l2[i2])) + i2++; } - return (i1 >= s1 && i2 >= s2); } else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) { - for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) { - if (isspace(l1[i1])) { - if (!isspace(l2[i2])) - return 0; - while (isspace(l1[i1]) && i1 < s1) + while (i1 < s1 && i2 < s2) { + if (isspace(l1[i1]) && isspace(l2[i2])) { + /* Skip matching spaces and try again */ + while (i1 < s1 && isspace(l1[i1])) i1++; - while (isspace(l2[i2]) && i2 < s2) + while (i2 < s2 && isspace(l2[i2])) i2++; - } else if (l1[i1++] != l2[i2++]) + continue; + } + if (l1[i1++] != l2[i2++]) return 0; } - return (i1 >= s1 && i2 >= s2); - } else - return s1 == s2 && !memcmp(l1, l2, s1); + } else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) { + while (i1 < s1 && i2 < s2 && l1[i1++] == l2[i2++]) + ; /* keep going */ + } - return 0; + /* + * After running out of one side, the remaining side must have + * nothing but whitespace for the lines to match. Note that + * ignore-whitespace-at-eol case may break out of the loop + * while there still are characters remaining on both lines. + */ + if (i1 < s1) { + while (i1 < s1 && isspace(l1[i1])) + i1++; + if (s1 != i1) + return 0; + } + if (i2 < s2) { + while (i2 < s2 && isspace(l2[i2])) + i2++; + return (s2 == i2); + } + return 1; } -unsigned long xdl_hash_record(char const **data, char const *top, long flags) { +static unsigned long xdl_hash_record_with_whitespace(char const **data, + char const *top, long flags) { unsigned long ha = 5381; char const *ptr = *data; for (; ptr < top && *ptr != '\n'; ptr++) { - if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) { + if (isspace(*ptr)) { + const char *ptr2 = ptr; + int at_eol; while (ptr + 1 < top && isspace(ptr[1]) && ptr[1] != '\n') ptr++; - if (flags & XDF_IGNORE_WHITESPACE_CHANGE - && ptr[1] != '\n') { + at_eol = (top <= ptr + 1 || ptr[1] == '\n'); + if (flags & XDF_IGNORE_WHITESPACE) + ; /* already handled */ + else if (flags & XDF_IGNORE_WHITESPACE_CHANGE + && !at_eol) { ha += (ha << 5); ha ^= (unsigned long) ' '; } + else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL + && !at_eol) { + while (ptr2 != ptr + 1) { + ha += (ha << 5); + ha ^= (unsigned long) *ptr2; + ptr2++; + } + } continue; } ha += (ha << 5); @@ -246,6 +291,23 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) { } +unsigned long xdl_hash_record(char const **data, char const *top, long flags) { + unsigned long ha = 5381; + char const *ptr = *data; + + if (flags & XDF_WHITESPACE_FLAGS) + return xdl_hash_record_with_whitespace(data, top, flags); + + for (; ptr < top && *ptr != '\n'; ptr++) { + ha += (ha << 5); + ha ^= (unsigned long) *ptr; + } + *data = ptr < top ? ptr + 1: ptr; + + return ha; +} + + unsigned int xdl_hashbits(unsigned int size) { unsigned int val = 1, bits = 0; @@ -338,4 +400,3 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, return 0; } - |
