xdiff: factor out get_func_line()

Move the code to search for a function line to be shown in the hunk header into its own function and to make returning the length-limited result string easier, introduce struct func_line. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 9, 2011 at 13:34 UTC f99f4b3667a83f870565bb140711f0e585999108
1 file changed +27 -16
xdiff/xemit.c
+27 -16
@@ -100,14 +100,35 @@ static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
100 return 0;
101 }
102
103 +struct func_line {
104 + long len;
105 + char buf[80];
106 +};
107 +
108 +static void get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
109 + struct func_line *func_line, long start, long limit)
110 +{
111 + find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;
112 + long l, size = sizeof(func_line->buf);
113 + char *buf = func_line->buf;
114 +
115 + for (l = start; l > limit && 0 <= l; l--) {
116 + const char *rec;
117 + long reclen = xdl_get_rec(&xe->xdf1, l, &rec);
118 + long len = ff(rec, reclen, buf, size, xecfg->find_func_priv);
119 + if (len >= 0) {
120 + func_line->len = len;
121 + break;
122 + }
123 + }
124 +}
125 +
126 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
127 xdemitconf_t const *xecfg) {
128 long s1, s2, e1, e2, lctx;
129 xdchange_t *xch, *xche;
107 - char funcbuf[80];
108 - long funclen = 0;
130 long funclineprev = -1;
110 - find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;
131 + struct func_line func_line = { 0 };
132
133 if (xecfg->flags & XDL_EMIT_COMMON)
134 return xdl_emit_common(xe, xscr, ecb, xecfg);
@@ -130,22 +151,12 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
151 */
152
153 if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
133 - long l;
134 - for (l = s1 - 1; l >= 0 && l > funclineprev; l--) {
135 - const char *rec;
136 - long reclen = xdl_get_rec(&xe->xdf1, l, &rec);
137 - long newfunclen = ff(rec, reclen, funcbuf,
138 - sizeof(funcbuf),
139 - xecfg->find_func_priv);
140 - if (newfunclen >= 0) {
141 - funclen = newfunclen;
142 - break;
143 - }
144 - }
154 + get_func_line(xe, xecfg, &func_line,
155 + s1 - 1, funclineprev);
156 funclineprev = s1 - 1;
157 }
158 if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
148 - funcbuf, funclen, ecb) < 0)
159 + func_line.buf, func_line.len, ecb) < 0)
160 return -1;
161
162 /*