Merge branch 'ar/clone'
[gitweb.git] / xdiff / xutils.c
index 21ab8e7e263983b54c07e44150cedc5c43963a35..2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 100644 (file)
@@ -44,21 +44,18 @@ long xdl_bogosqrt(long n) {
 
 int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
                     xdemitcb_t *ecb) {
+       int i = 2;
        mmbuffer_t mb[3];
-       int i;
 
        mb[0].ptr = (char *) pre;
        mb[0].size = psize;
        mb[1].ptr = (char *) rec;
        mb[1].size = size;
-       i = 2;
-
-       if (!size || rec[size-1] != '\n') {
-               mb[2].ptr = "\n\\ No newline at end of file\n";
+       if (size > 0 && rec[size - 1] != '\n') {
+               mb[2].ptr = (char *) "\n\\ No newline at end of file\n";
                mb[2].size = strlen(mb[2].ptr);
-               i = 3;
+               i++;
        }
-
        if (ecb->outf(ecb->priv, mb, i) < 0) {
 
                return -1;
@@ -189,11 +186,98 @@ long xdl_guess_lines(mmfile_t *mf) {
        return nl + 1;
 }
 
+int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
+{
+       int i1, i2;
+
+       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++])
+                               return 0;
+               }
+               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)
+                                       i1++;
+                               while (isspace(l2[i2]) && i2 < s2)
+                                       i2++;
+                       } else if (l1[i1++] != l2[i2++])
+                               return 0;
+               }
+               return (i1 >= s1 && i2 >= s2);
+       } else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) {
+               for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) {
+                       if (l1[i1] != l2[i2]) {
+                               while (i1 < s1 && isspace(l1[i1]))
+                                       i1++;
+                               while (i2 < s2 && isspace(l2[i2]))
+                                       i2++;
+                               if (i1 < s1 || i2 < s2)
+                                       return 0;
+                               return 1;
+                       }
+                       i1++;
+                       i2++;
+               }
+               return i1 >= s1 && i2 >= s2;
+       } else
+               return s1 == s2 && !memcmp(l1, l2, s1);
+
+       return 0;
+}
+
+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)) {
+                       const char *ptr2 = ptr;
+                       while (ptr + 1 < top && isspace(ptr[1])
+                                       && ptr[1] != '\n')
+                               ptr++;
+                       if (flags & XDF_IGNORE_WHITESPACE_CHANGE
+                                       && ptr[1] != '\n') {
+                               ha += (ha << 5);
+                               ha ^= (unsigned long) ' ';
+                       }
+                       if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
+                                       && ptr[1] != '\n') {
+                               while (ptr2 != ptr + 1) {
+                                       ha += (ha << 5);
+                                       ha ^= (unsigned long) *ptr2;
+                                       ptr2++;
+                               }
+                       }
+                       continue;
+               }
+               ha += (ha << 5);
+               ha ^= (unsigned long) *ptr;
+       }
+       *data = ptr < top ? ptr + 1: ptr;
+
+       return ha;
+}
+
 
-unsigned long xdl_hash_record(char const **data, char const *top) {
+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;
@@ -296,4 +380,3 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
 
        return 0;
 }
-