2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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"
|
|
|
|
#include "td/utils/Slice-decl.h"
|
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class StringBuilder {
|
|
|
|
public:
|
2018-10-30 12:07:28 +01:00
|
|
|
explicit StringBuilder(MutableSlice slice, bool use_buffer = false)
|
2018-10-30 15:22:44 +01:00
|
|
|
: begin_ptr_(slice.begin()), current_ptr_(begin_ptr_), use_buffer_(use_buffer) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (slice.size() <= reserved_size) {
|
2018-10-30 12:07:28 +01:00
|
|
|
auto buffer_size = reserved_size + 100;
|
|
|
|
buffer_ = std::make_unique<char[]>(buffer_size);
|
|
|
|
begin_ptr_ = buffer_.get();
|
|
|
|
current_ptr_ = begin_ptr_;
|
|
|
|
end_ptr_ = begin_ptr_ + buffer_size - reserved_size;
|
2018-10-30 15:22:44 +01:00
|
|
|
} else {
|
|
|
|
end_ptr_ = slice.end() - reserved_size;
|
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
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableCSlice as_cslice() {
|
|
|
|
if (current_ptr_ >= end_ptr_ + reserved_size) {
|
|
|
|
std::abort(); // shouldn't happen
|
|
|
|
}
|
|
|
|
*current_ptr_ = 0;
|
|
|
|
return MutableCSlice(begin_ptr_, current_ptr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const {
|
|
|
|
return error_flag_;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(const char *str) {
|
|
|
|
return *this << Slice(str);
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:48:15 +02:00
|
|
|
StringBuilder &operator<<(const wchar_t *str) = delete;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
StringBuilder &operator<<(Slice slice) {
|
2018-10-30 12:07:28 +01:00
|
|
|
size_t size = slice.size();
|
|
|
|
if (unlikely(!reserve(size))) {
|
|
|
|
if (end_ptr_ < current_ptr_) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
auto available_size = static_cast<size_t>(end_ptr_ + reserved_size - 1 - current_ptr_);
|
|
|
|
if (size > available_size) {
|
|
|
|
error_flag_ = true;
|
|
|
|
size = available_size;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-30 12:07:28 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::memcpy(current_ptr_, slice.begin(), size);
|
|
|
|
current_ptr_ += size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
template <class T>
|
|
|
|
StringBuilder &operator<<(const T *ptr) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this << static_cast<const void *>(ptr);
|
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_;
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr size_t reserved_size = 30;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return reserve_inner(reserved_size);
|
|
|
|
}
|
|
|
|
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>
|
|
|
|
std::enable_if_t<std::is_arithmetic<T>::value, string> to_string(const T &x) {
|
|
|
|
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
|