separated_buffer_t to stop being a template

Now that we no longer construct wide separated buffers, it doesn't have
to be templatized.
This commit is contained in:
ridiculousfish 2021-02-04 15:32:11 -08:00
parent 7d494eab5c
commit 032467f338
2 changed files with 13 additions and 25 deletions

View File

@ -337,7 +337,7 @@ shared_ptr<const io_data_t> io_chain_t::io_for_fd(int fd) const {
return nullptr; return nullptr;
} }
void output_stream_t::append_narrow_buffer(const separated_buffer_t<std::string> &buffer) { void output_stream_t::append_narrow_buffer(const separated_buffer_t &buffer) {
for (const auto &rhs_elem : buffer.elements()) { for (const auto &rhs_elem : buffer.elements()) {
append_with_separation(str2wcstring(rhs_elem.contents), rhs_elem.separation); append_with_separation(str2wcstring(rhs_elem.contents), rhs_elem.separation);
} }

View File

@ -55,14 +55,13 @@ enum class separation_type_t {
/// A separated_buffer_t contains a list of elements, some of which may be separated explicitly and /// A separated_buffer_t contains a list of elements, some of which may be separated explicitly and
/// others which must be separated further by the user (e.g. via IFS). /// others which must be separated further by the user (e.g. via IFS).
template <typename StringType>
class separated_buffer_t { class separated_buffer_t {
public: public:
struct element_t { struct element_t {
StringType contents; std::string contents;
separation_type_t separation; separation_type_t separation;
element_t(StringType contents, separation_type_t sep) element_t(std::string contents, separation_type_t sep)
: contents(std::move(contents)), separation(sep) {} : contents(std::move(contents)), separation(sep) {}
bool is_explicitly_separated() const { return separation == separation_type_t::explicitly; } bool is_explicitly_separated() const { return separation == separation_type_t::explicitly; }
@ -103,8 +102,6 @@ class separated_buffer_t {
void operator=(const separated_buffer_t &) = delete; void operator=(const separated_buffer_t &) = delete;
public: public:
using CharType = typename StringType::value_type;
/// Construct a separated_buffer_t with the given buffer limit \p limit, or 0 for no limit. /// Construct a separated_buffer_t with the given buffer limit \p limit, or 0 for no limit.
separated_buffer_t(size_t limit) : buffer_limit_(limit) {} separated_buffer_t(size_t limit) : buffer_limit_(limit) {}
@ -126,8 +123,8 @@ class separated_buffer_t {
/// Serialize the contents to a single string, where explicitly separated elements have a /// Serialize the contents to a single string, where explicitly separated elements have a
/// newline appended. /// newline appended.
StringType newline_serialized() const { std::string newline_serialized() const {
StringType result; std::string result;
result.reserve(size()); result.reserve(size());
for (const auto &elem : elements_) { for (const auto &elem : elements_) {
result.append(elem.contents); result.append(elem.contents);
@ -142,32 +139,23 @@ class separated_buffer_t {
const std::vector<element_t> &elements() const { return elements_; } const std::vector<element_t> &elements() const { return elements_; }
/// Append an element with range [begin, end) and the given separation type \p sep. /// Append an element with range [begin, end) and the given separation type \p sep.
void append(const CharType *begin, const CharType *end, void append(const char *begin, const char *end,
separation_type_t sep = separation_type_t::inferred) { separation_type_t sep = separation_type_t::inferred) {
if (!try_add_size(std::distance(begin, end))) return; if (!try_add_size(end - begin)) return;
// Try merging with the last element. // Try merging with the last element.
if (sep == separation_type_t::inferred && !elements_.empty() && if (sep == separation_type_t::inferred && !elements_.empty() &&
!elements_.back().is_explicitly_separated()) { !elements_.back().is_explicitly_separated()) {
elements_.back().contents.append(begin, end); elements_.back().contents.append(begin, end);
} else { } else {
elements_.emplace_back(StringType(begin, end), sep); elements_.emplace_back(std::string(begin, end), sep);
} }
} }
/// Append a string \p str with the given separation type \p sep. /// Append a string \p str with the given separation type \p sep.
void append(const StringType &str, separation_type_t sep = separation_type_t::inferred) { void append(const std::string &str, separation_type_t sep = separation_type_t::inferred) {
const CharType *cstr = str.c_str(); const char *cstr = str.c_str();
append(cstr, cstr + str.size(), sep); append(cstr, cstr + str.size(), sep);
} }
// Given that this is a narrow stream, convert a wide stream \p rhs to narrow and then append
// it.
template <typename RHSStringType>
void append_wide_buffer(const separated_buffer_t<RHSStringType> &rhs) {
for (const auto &rhs_elem : rhs.elements()) {
append(wcs2string(rhs_elem.contents), rhs_elem.separation);
}
}
}; };
/// Describes what type of IO operation an io_data_t represents. /// Describes what type of IO operation an io_data_t represents.
@ -306,7 +294,7 @@ class io_buffer_t {
friend io_bufferfill_t; friend io_bufferfill_t;
/// Buffer storing what we have read. /// Buffer storing what we have read.
separated_buffer_t<std::string> buffer_; separated_buffer_t buffer_;
/// Atomic flag indicating our fillthread should shut down. /// Atomic flag indicating our fillthread should shut down.
relaxed_atomic_bool_t shutdown_fillthread_{false}; relaxed_atomic_bool_t shutdown_fillthread_{false};
@ -341,7 +329,7 @@ class io_buffer_t {
/// Access the underlying buffer. /// Access the underlying buffer.
/// This requires that the background fillthread be none. /// This requires that the background fillthread be none.
const separated_buffer_t<std::string> &buffer() const { const separated_buffer_t &buffer() const {
assert(!fillthread_running() && "Cannot access buffer during background fill"); assert(!fillthread_running() && "Cannot access buffer during background fill");
return buffer_; return buffer_;
} }
@ -446,7 +434,7 @@ class output_stream_t {
void push_back(wchar_t c) { append(c); } void push_back(wchar_t c) { append(c); }
// Append data from a narrow buffer, widening it. // Append data from a narrow buffer, widening it.
void append_narrow_buffer(const separated_buffer_t<std::string> &buffer); void append_narrow_buffer(const separated_buffer_t &buffer);
/// Append a format string. /// Append a format string.
void append_format(const wchar_t *format, ...) { void append_format(const wchar_t *format, ...) {