mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-29 06:13:20 +00:00
string: Reduce write() calls
The impact here depends on the command and how much output it produces. It's possible to get up to 1.5x - `string upper` being a good example, or a no-op `string match '*'`. But the more the command actually needs to do, the less of an effect this has.
This commit is contained in:
parent
7bc4c9674b
commit
e69be38235
1 changed files with 23 additions and 45 deletions
|
@ -714,10 +714,8 @@ static int string_escape(parser_t &parser, io_streams_t &streams, int argc, cons
|
||||||
int nesc = 0;
|
int nesc = 0;
|
||||||
arg_iterator_t aiter(argv, optind, streams);
|
arg_iterator_t aiter(argv, optind, streams);
|
||||||
while (const wcstring *arg = aiter.nextstr()) {
|
while (const wcstring *arg = aiter.nextstr()) {
|
||||||
streams.out.append(escape_string(*arg, flags, opts.escape_style));
|
wcstring sep = aiter.want_newline() ? L"\n" : L"";
|
||||||
if (aiter.want_newline()) {
|
streams.out.append(escape_string(*arg, flags, opts.escape_style) + sep);
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
nesc++;
|
nesc++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -740,11 +738,9 @@ static int string_unescape(parser_t &parser, io_streams_t &streams, int argc,
|
||||||
arg_iterator_t aiter(argv, optind, streams);
|
arg_iterator_t aiter(argv, optind, streams);
|
||||||
while (const wcstring *arg = aiter.nextstr()) {
|
while (const wcstring *arg = aiter.nextstr()) {
|
||||||
wcstring result;
|
wcstring result;
|
||||||
|
wcstring sep = aiter.want_newline() ? L"\n" : L"";
|
||||||
if (unescape_string(*arg, &result, flags, opts.escape_style)) {
|
if (unescape_string(*arg, &result, flags, opts.escape_style)) {
|
||||||
streams.out.append(result);
|
streams.out.append(result + sep);
|
||||||
if (aiter.want_newline()) {
|
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
nesc++;
|
nesc++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -822,8 +818,7 @@ static int string_length(parser_t &parser, io_streams_t &streams, int argc, cons
|
||||||
nnonempty++;
|
nnonempty++;
|
||||||
}
|
}
|
||||||
if (!opts.quiet) {
|
if (!opts.quiet) {
|
||||||
streams.out.append(to_string(max));
|
streams.out.append(to_string(max) + L"\n");
|
||||||
streams.out.append(L'\n');
|
|
||||||
} else if (nnonempty > 0) {
|
} else if (nnonempty > 0) {
|
||||||
return STATUS_CMD_OK;
|
return STATUS_CMD_OK;
|
||||||
}
|
}
|
||||||
|
@ -834,8 +829,7 @@ static int string_length(parser_t &parser, io_streams_t &streams, int argc, cons
|
||||||
nnonempty++;
|
nnonempty++;
|
||||||
}
|
}
|
||||||
if (!opts.quiet) {
|
if (!opts.quiet) {
|
||||||
streams.out.append(to_string(n));
|
streams.out.append(to_string(n) + L"\n");
|
||||||
streams.out.append(L'\n');
|
|
||||||
} else if (nnonempty > 0) {
|
} else if (nnonempty > 0) {
|
||||||
return STATUS_CMD_OK;
|
return STATUS_CMD_OK;
|
||||||
}
|
}
|
||||||
|
@ -901,8 +895,7 @@ class wildcard_matcher_t final : public string_matcher_t {
|
||||||
if (opts.index) {
|
if (opts.index) {
|
||||||
streams.out.append_format(L"1 %lu\n", arg.length());
|
streams.out.append_format(L"1 %lu\n", arg.length());
|
||||||
} else {
|
} else {
|
||||||
streams.out.append(arg);
|
streams.out.append(arg + L"\n");
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -985,8 +978,7 @@ class regex_matcher_t final : public string_matcher_t {
|
||||||
if (opts.index) {
|
if (opts.index) {
|
||||||
streams.out.append_format(L"1 %lu\n", arg.length());
|
streams.out.append_format(L"1 %lu\n", arg.length());
|
||||||
} else {
|
} else {
|
||||||
streams.out.append(arg);
|
streams.out.append(arg + L"\n");
|
||||||
streams.out.push_back(L'\n');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -996,8 +988,7 @@ class regex_matcher_t final : public string_matcher_t {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.entire && !opts.quiet) {
|
if (opts.entire && !opts.quiet) {
|
||||||
streams.out.append(arg);
|
streams.out.append(arg + L"\n");
|
||||||
streams.out.push_back(L'\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have groups-only, we skip the first match, which is the full one.
|
// If we have groups-only, we skip the first match, which is the full one.
|
||||||
|
@ -1006,11 +997,10 @@ class regex_matcher_t final : public string_matcher_t {
|
||||||
maybe_t<match_range_t> cg = this->regex_.group(match_data_, j);
|
maybe_t<match_range_t> cg = this->regex_.group(match_data_, j);
|
||||||
if (cg.has_value() && !opts.quiet) {
|
if (cg.has_value() && !opts.quiet) {
|
||||||
if (opts.index) {
|
if (opts.index) {
|
||||||
streams.out.append_format(L"%lu %lu", cg->begin + 1, cg->end - cg->begin);
|
streams.out.append_format(L"%lu %lu\n", cg->begin + 1, cg->end - cg->begin);
|
||||||
} else {
|
} else {
|
||||||
streams.out.append(arg.substr(cg->begin, cg->end - cg->begin));
|
streams.out.append(arg.substr(cg->begin, cg->end - cg->begin) + L"\n");
|
||||||
}
|
}
|
||||||
streams.out.push_back(L'\n');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1287,10 +1277,8 @@ bool literal_replacer_t::replace_matches(const wcstring &arg, bool want_newline)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!opts.quiet && (!opts.filter || replacement_occurred)) {
|
if (!opts.quiet && (!opts.filter || replacement_occurred)) {
|
||||||
streams.out.append(result);
|
wcstring sep = want_newline ? L"\n" : L"";
|
||||||
if (want_newline) {
|
streams.out.append(result + sep);
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -1317,10 +1305,8 @@ bool regex_replacer_t::replace_matches(const wcstring &arg, bool want_newline) {
|
||||||
} else {
|
} else {
|
||||||
bool replacement_occurred = repl_count > 0;
|
bool replacement_occurred = repl_count > 0;
|
||||||
if (!opts.quiet && (!opts.filter || replacement_occurred)) {
|
if (!opts.quiet && (!opts.filter || replacement_occurred)) {
|
||||||
streams.out.append(*result);
|
wcstring sep = want_newline ? L"\n" : L"";
|
||||||
if (want_newline) {
|
streams.out.append(*result + sep);
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
total_replaced += repl_count;
|
total_replaced += repl_count;
|
||||||
}
|
}
|
||||||
|
@ -1618,6 +1604,7 @@ static int string_sub(parser_t &parser, io_streams_t &streams, int argc, const w
|
||||||
using size_type = wcstring::size_type;
|
using size_type = wcstring::size_type;
|
||||||
size_type pos = 0;
|
size_type pos = 0;
|
||||||
size_type count = wcstring::npos;
|
size_type count = wcstring::npos;
|
||||||
|
wcstring sep = aiter.want_newline() ? L"\n" : L"";
|
||||||
|
|
||||||
if (opts.start > 0) {
|
if (opts.start > 0) {
|
||||||
pos = static_cast<size_type>(opts.start - 1);
|
pos = static_cast<size_type>(opts.start - 1);
|
||||||
|
@ -1647,10 +1634,7 @@ static int string_sub(parser_t &parser, io_streams_t &streams, int argc, const w
|
||||||
|
|
||||||
// Note that std::string permits count to extend past end of string.
|
// Note that std::string permits count to extend past end of string.
|
||||||
if (!opts.quiet) {
|
if (!opts.quiet) {
|
||||||
streams.out.append(s->substr(pos, count));
|
streams.out.append(s->substr(pos, count) + sep);
|
||||||
if (aiter.want_newline()) {
|
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
nsub++;
|
nsub++;
|
||||||
if (opts.quiet) return STATUS_CMD_OK;
|
if (opts.quiet) return STATUS_CMD_OK;
|
||||||
|
@ -1760,8 +1744,7 @@ static int string_shorten(parser_t &parser, io_streams_t &streams, int argc, con
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos == 0) {
|
if (pos == 0) {
|
||||||
streams.out.append(line);
|
streams.out.append(line + L"\n");
|
||||||
streams.out.append(L'\n');
|
|
||||||
} else {
|
} else {
|
||||||
// We have an ellipsis, construct our string and print it.
|
// We have an ellipsis, construct our string and print it.
|
||||||
nsub++;
|
nsub++;
|
||||||
|
@ -1804,8 +1787,7 @@ static int string_shorten(parser_t &parser, io_streams_t &streams, int argc, con
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos == line.size()) {
|
if (pos == line.size()) {
|
||||||
streams.out.append(line);
|
streams.out.append(line + L"\n");
|
||||||
streams.out.append(L'\n');
|
|
||||||
} else {
|
} else {
|
||||||
nsub++;
|
nsub++;
|
||||||
wcstring newl = line.substr(0, pos);
|
wcstring newl = line.substr(0, pos);
|
||||||
|
@ -1838,6 +1820,7 @@ static int string_trim(parser_t &parser, io_streams_t &streams, int argc, const
|
||||||
|
|
||||||
arg_iterator_t aiter(argv, optind, streams);
|
arg_iterator_t aiter(argv, optind, streams);
|
||||||
while (const wcstring *arg = aiter.nextstr()) {
|
while (const wcstring *arg = aiter.nextstr()) {
|
||||||
|
wcstring sep = aiter.want_newline() ? L"\n" : L"";
|
||||||
// Begin and end are respectively the first character to keep on the left, and first
|
// Begin and end are respectively the first character to keep on the left, and first
|
||||||
// character to trim on the right. The length is thus end - start.
|
// character to trim on the right. The length is thus end - start.
|
||||||
size_t begin = 0, end = arg->size();
|
size_t begin = 0, end = arg->size();
|
||||||
|
@ -1852,10 +1835,7 @@ static int string_trim(parser_t &parser, io_streams_t &streams, int argc, const
|
||||||
assert(begin <= end && end <= arg->size());
|
assert(begin <= end && end <= arg->size());
|
||||||
ntrim += arg->size() - (end - begin);
|
ntrim += arg->size() - (end - begin);
|
||||||
if (!opts.quiet) {
|
if (!opts.quiet) {
|
||||||
streams.out.append(wcstring(*arg, begin, end - begin));
|
streams.out.append(wcstring(*arg, begin, end - begin) + sep);
|
||||||
if (aiter.want_newline()) {
|
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
} else if (ntrim > 0) {
|
} else if (ntrim > 0) {
|
||||||
return STATUS_CMD_OK;
|
return STATUS_CMD_OK;
|
||||||
}
|
}
|
||||||
|
@ -1880,10 +1860,8 @@ static int string_transform(parser_t &parser, io_streams_t &streams, int argc, c
|
||||||
std::transform(transformed.begin(), transformed.end(), transformed.begin(), func);
|
std::transform(transformed.begin(), transformed.end(), transformed.begin(), func);
|
||||||
if (transformed != *arg) n_transformed++;
|
if (transformed != *arg) n_transformed++;
|
||||||
if (!opts.quiet) {
|
if (!opts.quiet) {
|
||||||
streams.out.append(transformed);
|
wcstring sep = aiter.want_newline() ? L"\n" : L"";
|
||||||
if (aiter.want_newline()) {
|
streams.out.append(transformed + sep);
|
||||||
streams.out.append(L'\n');
|
|
||||||
}
|
|
||||||
} else if (n_transformed > 0) {
|
} else if (n_transformed > 0) {
|
||||||
return STATUS_CMD_OK;
|
return STATUS_CMD_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue