X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=xdiff%2Fxutils.c;h=3653864e4bf704c024453df8d9abf26d89b4778d;hb=20f50f1670c18173d74a34527b0c538c3fbbfde3;hp=8221806f78bab437e8863c3fb2ea6229ddae9bca;hpb=d93067d9c7fb210766c7282d662289f54d53f93a;p=git.git diff --git a/xdiff/xutils.c b/xdiff/xutils.c index 8221806f7..3653864e4 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -29,23 +29,33 @@ +long xdl_bogosqrt(long n) { + long i; + + /* + * Classical integer square root approximation using shifts. + */ + for (i = 1; n > 0; n >>= 2) + i <<= 1; + + return i; +} + + 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; @@ -176,12 +186,81 @@ 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; +} -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; for (; ptr < top && *ptr != '\n'; ptr++) { + if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) { + 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; } @@ -235,7 +314,8 @@ long xdl_atol(char const *str, char const **next) { } -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) { +int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, + const char *func, long funclen, xdemitcb_t *ecb) { int nb = 0; mmbuffer_t mb; char buf[128]; @@ -243,7 +323,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) { memcpy(buf, "@@ -", 4); nb += 4; - nb += xdl_num_out(buf + nb, c1 ? s1: 0); + nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1); if (c1 != 1) { memcpy(buf + nb, ",", 1); @@ -255,7 +335,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) { memcpy(buf + nb, " +", 2); nb += 2; - nb += xdl_num_out(buf + nb, c2 ? s2: 0); + nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1); if (c2 != 1) { memcpy(buf + nb, ",", 1); @@ -264,8 +344,16 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) { nb += xdl_num_out(buf + nb, c2); } - memcpy(buf + nb, " @@\n", 4); - nb += 4; + memcpy(buf + nb, " @@", 3); + nb += 3; + if (func && funclen) { + buf[nb++] = ' '; + if (funclen > sizeof(buf) - nb - 1) + funclen = sizeof(buf) - nb - 1; + memcpy(buf + nb, func, funclen); + nb += funclen; + } + buf[nb++] = '\n'; mb.ptr = buf; mb.size = nb;