2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2018-02-03 13:58:18 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-02-12 21:19:11 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-02-03 13:58:18 +01:00
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdlib>
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <type_traits>
|
2023-08-10 14:44:47 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class StringBuilder {
|
|
|
|
public:
|
2018-12-11 21:18:58 +01:00
|
|
|
explicit StringBuilder(MutableSlice slice, bool use_buffer = false);
|
2023-08-10 14:44:47 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
StringBuilder() : StringBuilder({}, true) {
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
current_ptr_ = begin_ptr_;
|
|
|
|
error_flag_ = false;
|
|
|
|
}
|
2018-02-03 17:49:39 +01:00
|
|
|
|
2021-07-27 08:41:11 +02:00
|
|
|
void pop_back() {
|
|
|
|
CHECK(current_ptr_ > begin_ptr_);
|
|
|
|
current_ptr_--;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:43:15 +01:00
|
|
|
void push_back(char c) {
|
|
|
|
if (unlikely(end_ptr_ <= current_ptr_)) {
|
|
|
|
if (!reserve_inner(RESERVED_SIZE)) {
|
2023-01-18 15:10:08 +01:00
|
|
|
error_flag_ = true;
|
2023-01-09 10:43:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*current_ptr_++ = c;
|
|
|
|
}
|
|
|
|
|
2023-09-29 12:13:56 +02:00
|
|
|
void append_char(size_t count, char c);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableCSlice as_cslice() {
|
2020-06-16 04:10:16 +02:00
|
|
|
if (current_ptr_ >= end_ptr_ + RESERVED_SIZE) {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::abort(); // shouldn't happen
|
|
|
|
}
|
|
|
|
*current_ptr_ = 0;
|
|
|
|
return MutableCSlice(begin_ptr_, current_ptr_);
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:43:15 +01:00
|
|
|
size_t size() {
|
|
|
|
return static_cast<size_t>(current_ptr_ - begin_ptr_);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_error() const {
|
|
|
|
return error_flag_;
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:28:00 +02:00
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<std::is_same<char *, std::remove_const_t<T>>::value, StringBuilder> &operator<<(T str) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return *this << Slice(str);
|
|
|
|
}
|
2020-08-20 21:28:00 +02:00
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<std::is_same<const char *, std::remove_const_t<T>>::value, StringBuilder> &operator<<(T str) {
|
|
|
|
return *this << Slice(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
StringBuilder &operator<<(char (&str)[N]) = delete;
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
StringBuilder &operator<<(const char (&str)[N]) {
|
|
|
|
return *this << Slice(str, N - 1);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-17 20:48:15 +02:00
|
|
|
StringBuilder &operator<<(const wchar_t *str) = delete;
|
|
|
|
|
2018-12-11 21:18:58 +01:00
|
|
|
StringBuilder &operator<<(Slice slice);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
StringBuilder &operator<<(bool b) {
|
|
|
|
return *this << (b ? Slice("true") : Slice("false"));
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(char c) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
*current_ptr_++ = c;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(unsigned char c) {
|
|
|
|
return *this << static_cast<unsigned int>(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(signed char c) {
|
|
|
|
return *this << static_cast<int>(c);
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(unsigned int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(long int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(long unsigned int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(long long int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &operator<<(long long unsigned int x);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-11 12:38:08 +01:00
|
|
|
struct FixedDouble {
|
|
|
|
double d;
|
|
|
|
int precision;
|
|
|
|
|
|
|
|
FixedDouble(double d, int precision) : d(d), precision(precision) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
StringBuilder &operator<<(FixedDouble x);
|
|
|
|
|
|
|
|
StringBuilder &operator<<(double x) {
|
|
|
|
return *this << FixedDouble(x, 6);
|
|
|
|
}
|
2018-02-03 17:49:39 +01:00
|
|
|
|
|
|
|
StringBuilder &operator<<(const void *ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-08-10 14:44:47 +02:00
|
|
|
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 << '}';
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
char *begin_ptr_;
|
|
|
|
char *current_ptr_;
|
|
|
|
char *end_ptr_;
|
|
|
|
bool error_flag_ = false;
|
2018-10-30 12:07:28 +01:00
|
|
|
bool use_buffer_ = false;
|
|
|
|
std::unique_ptr<char[]> buffer_;
|
2020-06-16 04:10:16 +02:00
|
|
|
static constexpr size_t RESERVED_SIZE = 30;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
StringBuilder &on_error() {
|
|
|
|
error_flag_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-10-30 12:07:28 +01:00
|
|
|
|
|
|
|
bool reserve() {
|
|
|
|
if (end_ptr_ > current_ptr_) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-16 04:10:16 +02:00
|
|
|
return reserve_inner(RESERVED_SIZE);
|
2018-10-30 12:07:28 +01:00
|
|
|
}
|
2023-01-09 10:43:15 +01:00
|
|
|
|
2018-10-30 12:07:28 +01:00
|
|
|
bool reserve(size_t size) {
|
|
|
|
if (end_ptr_ > current_ptr_ && static_cast<size_t>(end_ptr_ - current_ptr_) >= size) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return reserve_inner(size);
|
|
|
|
}
|
|
|
|
bool reserve_inner(size_t size);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2023-02-22 12:53:48 +01:00
|
|
|
std::enable_if_t<std::is_arithmetic<T>::value && !std::is_same<std::decay_t<T>, bool>::value, string> to_string(
|
|
|
|
const T &x) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const size_t buf_size = 1000;
|
|
|
|
auto buf = StackAllocator::alloc(buf_size);
|
|
|
|
StringBuilder sb(buf.as_slice());
|
|
|
|
sb << x;
|
|
|
|
return sb.as_cslice().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|