From 290d1f2cd6252455ac62b8d16f99a91d26be6624 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 11 Jan 2021 15:36:54 -0800 Subject: [PATCH] Mild refactoring of builtin_string repeat Preparation for fixing issue 5988; no behavior change expected here. --- src/builtin_string.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 3dce746e4..d8ae564d5 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -1558,7 +1558,7 @@ static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, wcha int retval = parse_opts(&opts, &optind, 0, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; - bool is_empty = true; + bool all_empty = true; arg_iterator_t aiter(argv, optind, streams); if (const wcstring *word = aiter.nextstr()) { @@ -1567,17 +1567,24 @@ static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, wcha !opts.count; const wcstring repeated = limit_repeat ? wcsrepeat_until(*word, opts.max) : wcsrepeat(*word, opts.count); - is_empty = repeated.empty(); - - if (!opts.quiet && !is_empty) { + if (!repeated.empty()) { + all_empty = false; + if (opts.quiet) { + // Early out if we can - see #7495. + return STATUS_CMD_OK; + } + } + if (!opts.quiet) { streams.out.append(repeated); - if (!opts.no_newline) streams.out.append(L"\n"); - } else if (opts.quiet && !is_empty) { - return STATUS_CMD_OK; } } - return !is_empty ? STATUS_CMD_OK : STATUS_CMD_ERROR; + // Historical behavior is to never append a newline if all strings were empty. + if (!opts.quiet && !opts.no_newline && !all_empty) { + streams.out.append(L'\n'); + } + + return all_empty ? STATUS_CMD_ERROR : STATUS_CMD_OK; } static int string_sub(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {