Skip to content

Commit f99f4b3

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 703f05a commit f99f4b3

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

xdiff/xemit.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,35 @@ static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
100100
return 0;
101101
}
102102

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+
103126
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
104127
xdemitconf_t const *xecfg) {
105128
long s1, s2, e1, e2, lctx;
106129
xdchange_t *xch, *xche;
107-
char funcbuf[80];
108-
long funclen = 0;
109130
long funclineprev = -1;
110-
find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;
131+
struct func_line func_line = { 0 };
111132

112133
if (xecfg->flags & XDL_EMIT_COMMON)
113134
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,
130151
*/
131152

132153
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);
145156
funclineprev = s1 - 1;
146157
}
147158
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)
149160
return -1;
150161

151162
/*

0 commit comments

Comments
 (0)