builtin-apply.c: clean-up apply_one_fragment()
We had two pointer variables pointing to the same buffer and an integer variable used to index into its tail part that was active (old, oldlines and oldsize for the preimage, and their 'new' counterparts for the postimage). To help readability, use 'oldlines' as the allocated pointer, and use 'old' as the pointer to the tail that advances while the code builds up the contents in the buffer. The size 'oldsize' can be computed as (old-oldines). Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 30, 2008 at 13:12 UTC
61e08ccacbfdc6046ccabdfdb01f7755ed5ad8b1
1 file changed
+27
-28
builtin-apply.c
+27
-28
@@ -1804,10 +1804,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1804
int match_beginning, match_end;
1805
const char *patch = frag->patch;
1806
int size = frag->size;
1807
- char *old = xmalloc(size);
1808
- char *new = xmalloc(size);
1809
- char *oldlines, *newlines;
1810
- int oldsize = 0, newsize = 0;
1807
+ char *old, *new, *oldlines, *newlines;
1808
int new_blank_lines_at_end = 0;
1809
unsigned long leading, trailing;
1810
int pos, applied_pos;
@@ -1816,7 +1813,11 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1813
1814
memset(&preimage, 0, sizeof(preimage));
1815
memset(&postimage, 0, sizeof(postimage));
1816
+ oldlines = xmalloc(size);
1817
+ newlines = xmalloc(size);
1818
1819
+ old = oldlines;
1820
+ new = newlines;
1821
while (size > 0) {
1822
char first;
1823
int len = linelen(patch, size);
@@ -1833,7 +1834,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1834
* followed by "\ No newline", then we also remove the
1835
* last one (which is the newline, of course).
1836
*/
1836
- plen = len-1;
1837
+ plen = len - 1;
1838
if (len < size && patch[len] == '\\')
1839
plen--;
1840
first = *patch;
@@ -1850,30 +1851,30 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1851
if (plen < 0)
1852
/* ... followed by '\No newline'; nothing */
1853
break;
1853
- old[oldsize++] = '\n';
1854
- new[newsize++] = '\n';
1854
+ *old++ = '\n';
1855
+ *new++ = '\n';
1856
add_line_info(&preimage, "\n", 1, LINE_COMMON);
1857
add_line_info(&postimage, "\n", 1, LINE_COMMON);
1858
break;
1859
case ' ':
1860
case '-':
1860
- memcpy(old + oldsize, patch + 1, plen);
1861
- add_line_info(&preimage, old + oldsize, plen,
1861
+ memcpy(old, patch + 1, plen);
1862
+ add_line_info(&preimage, old, plen,
1863
(first == ' ' ? LINE_COMMON : 0));
1863
- oldsize += plen;
1864
+ old += plen;
1865
if (first == '-')
1866
break;
1867
/* Fall-through for ' ' */
1868
case '+':
1869
if (first != '+' || !no_add) {
1869
- int added = apply_line(new + newsize, patch,
1870
+ int added = apply_line(new, patch,
1871
plen, ws_rule);
1871
- add_line_info(&postimage, new + newsize, added,
1872
+ add_line_info(&postimage, new, added,
1873
(first == '+' ? 0 : LINE_COMMON));
1874
1874
- newsize += added;
1875
+ new += added;
1876
if (first == '+' &&
1876
- added == 1 && new[newsize-1] == '\n')
1877
+ added == 1 && new[-1] == '\n')
1878
added_blank_line = 1;
1879
}
1880
break;
@@ -1892,16 +1893,13 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1893
patch += len;
1894
size -= len;
1895
}
1895
-
1896
if (inaccurate_eof &&
1897
- oldsize > 0 && old[oldsize - 1] == '\n' &&
1898
- newsize > 0 && new[newsize - 1] == '\n') {
1899
- oldsize--;
1900
- newsize--;
1897
+ old > oldlines && old[-1] == '\n' &&
1898
+ new > newlines && new[-1] == '\n') {
1899
+ old--;
1900
+ new--;
1901
}
1902
1903
- oldlines = old;
1904
- newlines = new;
1903
leading = frag->leading;
1904
trailing = frag->trailing;
1905
@@ -1923,10 +1921,10 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1921
}
1922
1923
pos = frag->newpos ? (frag->newpos - 1) : 0;
1926
- preimage.buf = old;
1927
- preimage.len = oldsize;
1928
- postimage.buf = new;
1929
- postimage.len = newsize;
1924
+ preimage.buf = oldlines;
1925
+ preimage.len = old - oldlines;
1926
+ postimage.buf = newlines;
1927
+ postimage.len = new - newlines;
1928
preimage.line = preimage.line_allocated;
1929
postimage.line = postimage.line_allocated;
1930
@@ -1990,11 +1988,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
1988
update_image(img, applied_pos, &preimage, &postimage);
1989
} else {
1990
if (apply_verbosely)
1993
- error("while searching for:\n%.*s", oldsize, oldlines);
1991
+ error("while searching for:\n%.*s",
1992
+ (int)(old - oldlines), oldlines);
1993
}
1994
1996
- free(old);
1997
- free(new);
1995
+ free(oldlines);
1996
+ free(newlines);
1997
free(preimage.line_allocated);
1998
free(postimage.line_allocated);
1999