{"thread":{"id":"4620","subject":"[PATCH 3/3] Teach diff about whitespace options.","startedAt":"2006-06-22T18:58:21Z","lastAt":"2006-06-22T18:58:21Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"22292","messageId":"7vwtb9kofm.fsf@assigned-by-dhcp.cox.net","threadId":"4620","inReplyTo":null,"subject":"[PATCH 3/3] Teach diff about whitespace options.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-06-22T18:58:21Z","receivedAt":"2006-06-22T18:58:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"From: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n\nThis adds XDF_IGNORE_WHITESPACE and XDF_IGNORE_WHITESPACE_CHANGE\nto the set of flags that can be passed to xdl_change_compact(),\nto implement -b (--ignore-space-change) and -w (--ignore-all-space)\noptions.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n * The original done by Johannes named a new function\n   xdl_line_match(), but that collided with what is in\n   xpatchi.c, so I renamed it to xdl_recmatch().\n\n xdiff.h    |    3 +++\n xdiffi.c   |   12 ++++++------\n xdiffi.h   |    1 -\n xmacros.h  |    1 -\n xprepare.c |   16 ++++++++++------\n xutils.c   |   51 ++++++++++++++++++++++++++++++++++++++++++++++++++-\n xutils.h   |    3 ++-\n 7 files changed, 71 insertions(+), 16 deletions(-)\n\ndiff --git a/xdiff.h b/xdiff.h\nindex f4af41b..224bd12 100644\n--- a/xdiff.h\n+++ b/xdiff.h\n@@ -29,6 +29,9 @@ #endif /* #ifdef __cplusplus */\n \n \n #define XDF_NEED_MINIMAL (1 << 1)\n+#define XDF_IGNORE_WHITESPACE (1 << 2)\n+#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)\n+#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)\n \n #define XDL_PATCH_NORMAL '-'\n #define XDL_PATCH_REVERSE '+'\ndiff --git a/xdiffi.c b/xdiffi.c\nindex b95ade2..ed7ad20 100644\n--- a/xdiffi.c\n+++ b/xdiffi.c\n@@ -45,7 +45,7 @@ static long xdl_split(unsigned long cons\n \t\t      long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,\n \t\t      xdalgoenv_t *xenv);\n static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2);\n-static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo);\n+static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags);\n \n \n \n@@ -397,7 +397,7 @@ static xdchange_t *xdl_add_change(xdchan\n }\n \n \n-static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo) {\n+static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {\n \tlong ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;\n \tchar *rchg = xdf->rchg, *rchgo = xdfo->rchg;\n \txrecord_t **recs = xdf->recs;\n@@ -440,7 +440,7 @@ static int xdl_change_compact(xdfile_t *\n \t\t\t * the group.\n \t\t\t */\n \t\t\twhile (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha &&\n-\t\t\t       XDL_RECMATCH(recs[ixs - 1], recs[ix - 1])) {\n+\t\t\t       xdl_recmatch(recs[ixs - 1]->ptr, recs[ixs - 1]->size, recs[ix - 1]->ptr, recs[ix - 1]->size, flags)) {\n \t\t\t\trchg[--ixs] = 1;\n \t\t\t\trchg[--ix] = 0;\n \n@@ -468,7 +468,7 @@ static int xdl_change_compact(xdfile_t *\n \t\t\t * the group.\n \t\t\t */\n \t\t\twhile (ix < nrec && recs[ixs]->ha == recs[ix]->ha &&\n-\t\t\t       XDL_RECMATCH(recs[ixs], recs[ix])) {\n+\t\t\t       xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size, recs[ix]->ptr, recs[ix]->size, flags)) {\n \t\t\t\trchg[ixs++] = 0;\n \t\t\t\trchg[ix++] = 1;\n \n@@ -546,8 +546,8 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf\n \n \t\treturn -1;\n \t}\n-\tif (xdl_change_compact(&xe.xdf1, &xe.xdf2) < 0 ||\n-\t    xdl_change_compact(&xe.xdf2, &xe.xdf1) < 0 ||\n+\tif (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||\n+\t    xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||\n \t    xdl_build_script(&xe, &xscr) < 0) {\n \n \t\txdl_free_env(&xe);\ndiff --git a/xdiffi.h b/xdiffi.h\nindex dd8f3c9..d3b7271 100644\n--- a/xdiffi.h\n+++ b/xdiffi.h\n@@ -55,6 +55,5 @@ void xdl_free_script(xdchange_t *xscr);\n int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,\n \t\t  xdemitconf_t const *xecfg);\n \n-\n #endif /* #if !defined(XDIFFI_H) */\n \ndiff --git a/xmacros.h b/xmacros.h\nindex 78f0260..4c2fde8 100644\n--- a/xmacros.h\n+++ b/xmacros.h\n@@ -33,7 +33,6 @@ #define XDL_ABS(v) ((v) >= 0 ? (v): -(v)\n #define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9')\n #define XDL_HASHLONG(v, b) (((unsigned long)(v) * GR_PRIME) >> ((CHAR_BIT * sizeof(unsigned long)) - (b)))\n #define XDL_PTRFREE(p) do { if (p) { xdl_free(p); (p) = NULL; } } while (0)\n-#define XDL_RECMATCH(r1, r2) ((r1)->size == (r2)->size && memcmp((r1)->ptr, (r2)->ptr, (r1)->size) == 0)\n #define XDL_LE32_PUT(p, v) \\\n do { \\\n \tunsigned char *__p = (unsigned char *) (p); \\\ndiff --git a/xprepare.c b/xprepare.c\nindex add5a75..1be7b31 100644\n--- a/xprepare.c\n+++ b/xprepare.c\n@@ -43,12 +43,13 @@ typedef struct s_xdlclassifier {\n \txdlclass_t **rchash;\n \tchastore_t ncha;\n \tlong count;\n+\tlong flags;\n } xdlclassifier_t;\n \n \n \n \n-static int xdl_init_classifier(xdlclassifier_t *cf, long size);\n+static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);\n static void xdl_free_classifier(xdlclassifier_t *cf);\n static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,\n \t\t\t       xrecord_t *rec);\n@@ -63,9 +64,11 @@ static int xdl_optimize_ctxs(xdfile_t *x\n \n \n \n-static int xdl_init_classifier(xdlclassifier_t *cf, long size) {\n+static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {\n \tlong i;\n \n+\tcf->flags = flags;\n+\n \tcf->hbits = xdl_hashbits((unsigned int) size);\n \tcf->hsize = 1 << cf->hbits;\n \n@@ -103,8 +106,9 @@ static int xdl_classify_record(xdlclassi\n \tline = rec->ptr;\n \thi = (long) XDL_HASHLONG(rec->ha, cf->hbits);\n \tfor (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)\n-\t\tif (rcrec->ha == rec->ha && rcrec->size == rec->size &&\n-\t\t    !memcmp(line, rcrec->line, rec->size))\n+\t\tif (rcrec->ha == rec->ha &&\n+\t\t\t\txdl_recmatch(rcrec->line, rcrec->size,\n+\t\t\t\t\trec->ptr, rec->size, cf->flags))\n \t\t\tbreak;\n \n \tif (!rcrec) {\n@@ -173,7 +177,7 @@ static int xdl_prepare_ctx(mmfile_t *mf,\n \t\t\t\ttop = blk + bsize;\n \t\t\t}\n \t\t\tprev = cur;\n-\t\t\thav = xdl_hash_record(&cur, top);\n+\t\t\thav = xdl_hash_record(&cur, top, xpp->flags);\n \t\t\tif (nrec >= narec) {\n \t\t\t\tnarec *= 2;\n \t\t\t\tif (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {\n@@ -268,7 +272,7 @@ int xdl_prepare_env(mmfile_t *mf1, mmfil\n \tenl1 = xdl_guess_lines(mf1) + 1;\n \tenl2 = xdl_guess_lines(mf2) + 1;\n \n-\tif (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {\n+\tif (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {\n \n \t\treturn -1;\n \t}\ndiff --git a/xutils.c b/xutils.c\nindex a776706..695a458 100644\n--- a/xutils.c\n+++ b/xutils.c\n@@ -483,12 +483,61 @@ long xdl_guess_lines(mmfile_t *mf) {\n \treturn nl + 1;\n }\n \n+int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)\n+{\n+\tint i1, i2;\n+\n+\tif (flags & XDF_IGNORE_WHITESPACE) {\n+\t\tfor (i1 = i2 = 0; i1 < s1 && i2 < s2; i1++, i2++) {\n+\t\t\tif (isspace(l1[i1]))\n+\t\t\t\twhile (isspace(l1[i1]) && i1 < s1)\n+\t\t\t\t\ti1++;\n+\t\t\telse if (isspace(l2[i2]))\n+\t\t\t\twhile (isspace(l2[i2]) && i2 < s2)\n+\t\t\t\t\ti2++;\n+\t\t\telse if (l1[i1] != l2[i2])\n+\t\t\t\treturn l2[i2] - l1[i1];\n+\t\t}\n+\t\tif (i1 >= s1)\n+\t\t\treturn 1;\n+\t\telse if (i2 >= s2)\n+\t\t\treturn -1;\n+\t} else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {\n+\t\tfor (i1 = i2 = 0; i1 < s1 && i2 < s2; i1++, i2++) {\n+\t\t\tif (isspace(l1[i1])) {\n+\t\t\t\tif (!isspace(l2[i2]))\n+\t\t\t\t\treturn -1;\n+\t\t\t\twhile (isspace(l1[i1]) && i1 < s1)\n+\t\t\t\t\ti1++;\n+\t\t\t\twhile (isspace(l2[i2]) && i2 < s2)\n+\t\t\t\t\ti2++;\n+\t\t\t} else if (l1[i1] != l2[i2])\n+\t\t\t\treturn l2[i2] - l1[i1];\n+\t\t}\n+\t\tif (i1 >= s1)\n+\t\t\treturn 1;\n+\t\telse if (i2 >= s2)\n+\t\t\treturn -1;\n+\t} else\n+\t\treturn s1 == s2 && !memcmp(l1, l2, s1);\n \n-unsigned long xdl_hash_record(char const **data, char const *top) {\n+\treturn 0;\n+}\n+\n+unsigned long xdl_hash_record(char const **data, char const *top, long flags) {\n \tunsigned long ha = 5381;\n \tchar const *ptr = *data;\n \n \tfor (; ptr < top && *ptr != '\\n'; ptr++) {\n+\t\tif (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) {\n+\t\t\twhile (ptr < top && isspace(*ptr) && ptr[1] != '\\n')\n+\t\t\t\tptr++;\n+\t\t\tif (flags & XDF_IGNORE_WHITESPACE_CHANGE) {\n+\t\t\t\tha += (ha << 5);\n+\t\t\t\tha ^= (unsigned long) ' ';\n+\t\t\t}\n+\t\t\tcontinue;\n+\t\t}\n \t\tha += (ha << 5);\n \t\tha ^= (unsigned long) *ptr;\n \t}\ndiff --git a/xutils.h b/xutils.h\nindex 5a3e13b..275fa74 100644\n--- a/xutils.h\n+++ b/xutils.h\n@@ -35,7 +35,8 @@ void *xdl_cha_alloc(chastore_t *cha);\n void *xdl_cha_first(chastore_t *cha);\n void *xdl_cha_next(chastore_t *cha);\n long xdl_guess_lines(mmfile_t *mf);\n-unsigned long xdl_hash_record(char const **data, char const *top);\n+int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);\n+unsigned long xdl_hash_record(char const **data, char const *top, long flags);\n unsigned int xdl_hashbits(unsigned int size);\n int xdl_num_out(char *out, long val);\n long xdl_atol(char const *str, char const **next);\n-- \n1.4.0.gbf9e\n"}]}