Skip to content

Commit 32ae5b3

Browse files
committed
Merge branch 'rs/optim-text-wrap'
* rs/optim-text-wrap: utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text() utf8.c: remove strbuf_write() utf8.c: remove print_spaces() utf8.c: remove print_wrapped_text()
2 parents bd282f5 + 462749b commit 32ae5b3

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

builtin-shortlog.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,19 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
304304
return 0;
305305
}
306306

307+
static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
308+
const struct shortlog *log)
309+
{
310+
int col = strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
311+
if (col != log->wrap)
312+
strbuf_addch(sb, '\n');
313+
}
314+
307315
void shortlog_output(struct shortlog *log)
308316
{
309317
int i, j;
318+
struct strbuf sb = STRBUF_INIT;
319+
310320
if (log->sort_by_number)
311321
qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
312322
compare_by_number);
@@ -321,9 +331,9 @@ void shortlog_output(struct shortlog *log)
321331
const char *msg = onelines->items[j].string;
322332

323333
if (log->wrap_lines) {
324-
int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
325-
if (col != log->wrap)
326-
putchar('\n');
334+
strbuf_reset(&sb);
335+
add_wrapped_shortlog_msg(&sb, msg, log);
336+
fwrite(sb.buf, sb.len, 1, stdout);
327337
}
328338
else
329339
printf(" %s\n", msg);
@@ -337,6 +347,7 @@ void shortlog_output(struct shortlog *log)
337347
log->list.items[i].util = NULL;
338348
}
339349

350+
strbuf_release(&sb);
340351
log->list.strdup_strings = 1;
341352
string_list_clear(&log->list, 1);
342353
clear_mailmap(&log->mailmap);

utf8.c

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -280,22 +280,11 @@ int is_utf8(const char *text)
280280
return 1;
281281
}
282282

283-
static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
283+
static void strbuf_addchars(struct strbuf *sb, int c, size_t n)
284284
{
285-
if (sb)
286-
strbuf_insert(sb, sb->len, buf, len);
287-
else
288-
fwrite(buf, len, 1, stdout);
289-
}
290-
291-
static void print_spaces(struct strbuf *buf, int count)
292-
{
293-
static const char s[] = " ";
294-
while (count >= sizeof(s)) {
295-
strbuf_write(buf, s, sizeof(s) - 1);
296-
count -= sizeof(s) - 1;
297-
}
298-
strbuf_write(buf, s, count);
285+
strbuf_grow(sb, n);
286+
memset(sb->buf + sb->len, c, n);
287+
strbuf_setlen(sb, sb->len + n);
299288
}
300289

301290
static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
@@ -307,8 +296,8 @@ static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
307296
const char *eol = strchrnul(text, '\n');
308297
if (*eol == '\n')
309298
eol++;
310-
print_spaces(buf, indent);
311-
strbuf_write(buf, text, eol - text);
299+
strbuf_addchars(buf, ' ', indent);
300+
strbuf_add(buf, text, eol - text);
312301
text = eol;
313302
indent = indent2;
314303
}
@@ -335,16 +324,21 @@ static size_t display_mode_esc_sequence_len(const char *s)
335324
* consumed (and no extra indent is necessary for the first line).
336325
*/
337326
int strbuf_add_wrapped_text(struct strbuf *buf,
338-
const char *text, int indent, int indent2, int width)
327+
const char *text, int indent1, int indent2, int width)
339328
{
340-
int w = indent, assume_utf8 = is_utf8(text);
341-
const char *bol = text, *space = NULL;
329+
int indent, w, assume_utf8 = 1;
330+
const char *bol, *space, *start = text;
331+
size_t orig_len = buf->len;
342332

343333
if (width <= 0) {
344-
strbuf_add_indented_text(buf, text, indent, indent2);
334+
strbuf_add_indented_text(buf, text, indent1, indent2);
345335
return 1;
346336
}
347337

338+
retry:
339+
bol = text;
340+
w = indent = indent1;
341+
space = NULL;
348342
if (indent < 0) {
349343
w = -indent;
350344
space = text;
@@ -366,8 +360,8 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
366360
if (space)
367361
start = space;
368362
else
369-
print_spaces(buf, indent);
370-
strbuf_write(buf, start, text - start);
363+
strbuf_addchars(buf, ' ', indent);
364+
strbuf_add(buf, start, text - start);
371365
if (!c)
372366
return w;
373367
space = text;
@@ -376,40 +370,41 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
376370
else if (c == '\n') {
377371
space++;
378372
if (*space == '\n') {
379-
strbuf_write(buf, "\n", 1);
373+
strbuf_addch(buf, '\n');
380374
goto new_line;
381375
}
382376
else if (!isalnum(*space))
383377
goto new_line;
384378
else
385-
strbuf_write(buf, " ", 1);
379+
strbuf_addch(buf, ' ');
386380
}
387381
w++;
388382
text++;
389383
}
390384
else {
391385
new_line:
392-
strbuf_write(buf, "\n", 1);
386+
strbuf_addch(buf, '\n');
393387
text = bol = space + isspace(*space);
394388
space = NULL;
395389
w = indent = indent2;
396390
}
397391
continue;
398392
}
399-
if (assume_utf8)
393+
if (assume_utf8) {
400394
w += utf8_width(&text, NULL);
401-
else {
395+
if (!text) {
396+
assume_utf8 = 0;
397+
text = start;
398+
strbuf_setlen(buf, orig_len);
399+
goto retry;
400+
}
401+
} else {
402402
w++;
403403
text++;
404404
}
405405
}
406406
}
407407

408-
int print_wrapped_text(const char *text, int indent, int indent2, int width)
409-
{
410-
return strbuf_add_wrapped_text(NULL, text, indent, indent2, width);
411-
}
412-
413408
int is_encoding_utf8(const char *name)
414409
{
415410
if (!name)

utf8.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ int utf8_strwidth(const char *string);
88
int is_utf8(const char *text);
99
int is_encoding_utf8(const char *name);
1010

11-
int print_wrapped_text(const char *text, int indent, int indent2, int len);
1211
int strbuf_add_wrapped_text(struct strbuf *buf,
1312
const char *text, int indent, int indent2, int width);
1413

0 commit comments

Comments
 (0)