color-words: refactor word splitting and use ALLOC_GROW()
Word splitting is now performed by the function diff_words_fill(), avoiding having the same code twice. In the same spirit, avoid duplicating the code of ALLOC_GROW(). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jan 17, 2009 at 17:29 UTC
23c1575f747393f9847874fd1ed72a44557459d1
1 file changed
+19
-21
diff.c
+19
-21
@@ -326,10 +326,7 @@ struct diff_words_buffer {
326
static void diff_words_append(char *line, unsigned long len,
327
struct diff_words_buffer *buffer)
328
{
329
- if (buffer->text.size + len > buffer->alloc) {
330
- buffer->alloc = (buffer->text.size + len) * 3 / 2;
331
- buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
332
- }
329
+ ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
330
line++;
331
len--;
332
memcpy(buffer->text.ptr + buffer->text.size, line, len);
@@ -398,6 +395,22 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
395
}
396
}
397
398
+/*
399
+ * This function splits the words in buffer->text, and stores the list with
400
+ * newline separator into out.
401
+ */
402
+static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out)
403
+{
404
+ int i;
405
+ out->size = buffer->text.size;
406
+ out->ptr = xmalloc(out->size);
407
+ memcpy(out->ptr, buffer->text.ptr, out->size);
408
+ for (i = 0; i < out->size; i++)
409
+ if (isspace(out->ptr[i]))
410
+ out->ptr[i] = '\n';
411
+ buffer->current = 0;
412
+}
413
+
414
/* this executes the word diff on the accumulated buffers */
415
static void diff_words_show(struct diff_words_data *diff_words)
416
{
@@ -405,26 +418,11 @@ static void diff_words_show(struct diff_words_data *diff_words)
418
xdemitconf_t xecfg;
419
xdemitcb_t ecb;
420
mmfile_t minus, plus;
408
- int i;
421
422
memset(&xpp, 0, sizeof(xpp));
423
memset(&xecfg, 0, sizeof(xecfg));
412
- minus.size = diff_words->minus.text.size;
413
- minus.ptr = xmalloc(minus.size);
414
- memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
415
- for (i = 0; i < minus.size; i++)
416
- if (isspace(minus.ptr[i]))
417
- minus.ptr[i] = '\n';
418
- diff_words->minus.current = 0;
419
-
420
- plus.size = diff_words->plus.text.size;
421
- plus.ptr = xmalloc(plus.size);
422
- memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
423
- for (i = 0; i < plus.size; i++)
424
- if (isspace(plus.ptr[i]))
425
- plus.ptr[i] = '\n';
426
- diff_words->plus.current = 0;
427
-
424
+ diff_words_fill(&diff_words->minus, &minus);
425
+ diff_words_fill(&diff_words->plus, &plus);
426
xpp.flags = XDF_NEED_MINIMAL;
427
xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
428
xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,