Move vector and pair formatting functions to StringBuilder.

This commit is contained in:
levlam 2023-08-10 15:44:47 +03:00
parent f64268c50d
commit ed00732cd2
2 changed files with 32 additions and 10 deletions

View File

@ -13,12 +13,14 @@
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <utility>
namespace td {
class StringBuilder {
public:
explicit StringBuilder(MutableSlice slice, bool use_buffer = false);
StringBuilder() : StringBuilder({}, true) {
}
@ -126,6 +128,36 @@ class StringBuilder {
StringBuilder &operator<<(const void *ptr);
template <class A, class B>
StringBuilder &operator<<(const std::pair<A, B> &p) {
return *this << '[' << p.first << ';' << p.second << ']';
}
template <class T>
StringBuilder &operator<<(const vector<T> &v) {
*this << '{';
if (!v.empty()) {
*this << v[0];
size_t len = v.size();
for (size_t i = 1; i < len; i++) {
*this << ", " << v[i];
}
}
return *this << '}';
}
StringBuilder &operator<<(const vector<bool> &v) {
*this << '{';
if (!v.empty()) {
*this << v[0];
size_t len = v.size();
for (size_t i = 1; i < len; i++) {
*this << ", " << static_cast<bool>(v[i]);
}
}
return *this << '}';
}
private:
char *begin_ptr_;
char *current_ptr_;

View File

@ -325,14 +325,4 @@ Lambda<LambdaT> lambda(const LambdaT &lambda) {
using format::tag;
template <class A, class B>
StringBuilder &operator<<(StringBuilder &sb, const std::pair<A, B> &p) {
return sb << '[' << p.first << ';' << p.second << ']';
}
template <class T>
StringBuilder &operator<<(StringBuilder &stream, const vector<T> &vec) {
return stream << format::as_array(vec);
}
} // namespace td