Add a string_output_stream_t to collect builtin output

This is used when creating a function; this breaks a dependency on the
more complicated buffered_output_stream_t to ease refactoring.
This commit is contained in:
ridiculousfish 2021-02-04 14:12:14 -08:00
parent fc97151aec
commit cd9a035f02
3 changed files with 18 additions and 4 deletions

View file

@ -373,6 +373,8 @@ void fd_output_stream_t::append(const wchar_t *s, size_t amt) {
void null_output_stream_t::append(const wchar_t *, size_t) {}
void string_output_stream_t::append(const wchar_t *s, size_t amt) { contents_.append(s, amt); }
void buffered_output_stream_t::append(const wchar_t *s, size_t amt) { buffer_.append(s, s + amt); }
void buffered_output_stream_t::append_with_separation(const wchar_t *s, size_t len,

View file

@ -475,6 +475,19 @@ class fd_output_stream_t final : public output_stream_t {
bool errored_{false};
};
/// A simple output stream which buffers into a wcstring.
class string_output_stream_t final : public output_stream_t {
public:
string_output_stream_t() = default;
void append(const wchar_t *s, size_t amt) override;
/// \return the wcstring containing the output.
const wcstring &contents() const { return contents_; }
private:
wcstring contents_;
};
/// An output stream for builtins which buffers into a separated buffer.
class buffered_output_stream_t final : public output_stream_t {
public:

View file

@ -390,9 +390,8 @@ end_execution_reason_t parse_execution_context_t::run_function_statement(
return result;
}
trace_if_enabled(*parser, L"function", arguments);
// no limit on the amount of output from builtin_function()
buffered_output_stream_t outs(0);
buffered_output_stream_t errs(0);
null_output_stream_t outs;
string_output_stream_t errs;
io_streams_t streams(outs, errs);
int err_code = 0;
maybe_t<int> err = builtin_function(*parser, streams, arguments, pstree, statement);
@ -402,7 +401,7 @@ end_execution_reason_t parse_execution_context_t::run_function_statement(
parser->set_last_statuses(statuses_t::just(err_code));
}
wcstring errtext = errs.contents();
const wcstring &errtext = errs.contents();
if (!errtext.empty()) {
return this->report_error(err_code, header, L"%ls", errtext.c_str());
}