2018-02-03 17:49:39 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-02-03 17:49:39 +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)
|
|
|
|
//
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2018-02-03 20:40:06 +01:00
|
|
|
#include "td/utils/misc.h"
|
2018-02-11 16:58:18 +01:00
|
|
|
#include "td/utils/port/thread_local.h"
|
2018-02-03 20:40:06 +01:00
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
#include <cstdio>
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <cstring>
|
2018-06-05 01:34:51 +02:00
|
|
|
#include <limits>
|
2018-02-03 20:40:06 +01:00
|
|
|
#include <locale>
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <memory>
|
2018-02-03 20:40:06 +01:00
|
|
|
#include <sstream>
|
2018-06-05 01:34:51 +02:00
|
|
|
#include <utility>
|
2018-02-03 17:49:39 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-12-11 21:18:58 +01:00
|
|
|
StringBuilder::StringBuilder(MutableSlice slice, bool use_buffer)
|
|
|
|
: begin_ptr_(slice.begin()), current_ptr_(begin_ptr_), use_buffer_(use_buffer) {
|
2020-06-16 04:10:16 +02:00
|
|
|
if (slice.size() <= RESERVED_SIZE) {
|
|
|
|
auto buffer_size = RESERVED_SIZE + 100;
|
2018-12-11 21:18:58 +01:00
|
|
|
buffer_ = std::make_unique<char[]>(buffer_size);
|
|
|
|
begin_ptr_ = buffer_.get();
|
|
|
|
current_ptr_ = begin_ptr_;
|
2020-06-16 04:10:16 +02:00
|
|
|
end_ptr_ = begin_ptr_ + buffer_size - RESERVED_SIZE;
|
2018-12-11 21:18:58 +01:00
|
|
|
} else {
|
2020-06-16 04:10:16 +02:00
|
|
|
end_ptr_ = slice.end() - RESERVED_SIZE;
|
2018-12-11 21:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(Slice slice) {
|
|
|
|
size_t size = slice.size();
|
|
|
|
if (unlikely(!reserve(size))) {
|
|
|
|
if (end_ptr_ < current_ptr_) {
|
|
|
|
return on_error();
|
|
|
|
}
|
2020-06-16 04:10:16 +02:00
|
|
|
auto available_size = static_cast<size_t>(end_ptr_ + RESERVED_SIZE - 1 - current_ptr_);
|
2018-12-11 21:18:58 +01:00
|
|
|
if (size > available_size) {
|
|
|
|
error_flag_ = true;
|
|
|
|
size = available_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(current_ptr_, slice.begin(), size);
|
|
|
|
current_ptr_ += size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-09-29 12:13:56 +02:00
|
|
|
void StringBuilder::append_char(size_t count, char c) {
|
|
|
|
if (unlikely(!reserve(count))) {
|
|
|
|
if (end_ptr_ < current_ptr_) {
|
|
|
|
on_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto available_size = static_cast<size_t>(end_ptr_ + RESERVED_SIZE - 1 - current_ptr_);
|
|
|
|
if (count > available_size) {
|
|
|
|
error_flag_ = true;
|
|
|
|
count = available_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice(current_ptr_, count).fill(c);
|
|
|
|
current_ptr_ += count;
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:34:51 +02:00
|
|
|
template <class T>
|
|
|
|
static char *print_uint(char *current_ptr, T x) {
|
|
|
|
if (x < 100) {
|
|
|
|
if (x < 10) {
|
|
|
|
*current_ptr++ = static_cast<char>('0' + x);
|
|
|
|
} else {
|
|
|
|
*current_ptr++ = static_cast<char>('0' + x / 10);
|
|
|
|
*current_ptr++ = static_cast<char>('0' + x % 10);
|
|
|
|
}
|
|
|
|
return current_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto begin_ptr = current_ptr;
|
|
|
|
do {
|
|
|
|
*current_ptr++ = static_cast<char>('0' + x % 10);
|
|
|
|
x /= 10;
|
|
|
|
} while (x > 0);
|
|
|
|
|
|
|
|
auto end_ptr = current_ptr - 1;
|
|
|
|
while (begin_ptr < end_ptr) {
|
|
|
|
std::swap(*begin_ptr++, *end_ptr--);
|
|
|
|
}
|
|
|
|
|
|
|
|
return current_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static char *print_int(char *current_ptr, T x) {
|
|
|
|
if (x < 0) {
|
|
|
|
if (x == std::numeric_limits<T>::min()) {
|
2020-09-20 21:51:03 +02:00
|
|
|
current_ptr = print_int(current_ptr, x + 1);
|
|
|
|
CHECK(current_ptr[-1] != '9');
|
|
|
|
current_ptr[-1]++;
|
|
|
|
return current_ptr;
|
2018-06-05 01:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*current_ptr++ = '-';
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return print_uint(current_ptr, x);
|
|
|
|
}
|
|
|
|
|
2018-10-30 12:07:28 +01:00
|
|
|
bool StringBuilder::reserve_inner(size_t size) {
|
|
|
|
if (!use_buffer_) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-30 15:22:44 +01:00
|
|
|
|
2018-10-30 12:07:28 +01:00
|
|
|
size_t old_data_size = current_ptr_ - begin_ptr_;
|
2020-06-16 04:10:16 +02:00
|
|
|
if (size >= std::numeric_limits<size_t>::max() - RESERVED_SIZE - old_data_size - 1) {
|
2018-10-30 15:22:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-30 12:07:28 +01:00
|
|
|
size_t need_data_size = old_data_size + size;
|
|
|
|
size_t old_buffer_size = end_ptr_ - begin_ptr_;
|
2020-06-16 04:10:16 +02:00
|
|
|
if (old_buffer_size >= (std::numeric_limits<size_t>::max() - RESERVED_SIZE) / 2 - 2) {
|
2018-10-30 15:22:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-30 12:07:28 +01:00
|
|
|
size_t new_buffer_size = (old_buffer_size + 1) * 2;
|
|
|
|
if (new_buffer_size < need_data_size) {
|
|
|
|
new_buffer_size = need_data_size;
|
|
|
|
}
|
|
|
|
if (new_buffer_size < 100) {
|
|
|
|
new_buffer_size = 100;
|
|
|
|
}
|
2020-06-16 04:10:16 +02:00
|
|
|
new_buffer_size += RESERVED_SIZE;
|
2018-10-30 15:22:44 +01:00
|
|
|
auto new_buffer = std::make_unique<char[]>(new_buffer_size);
|
|
|
|
std::memcpy(new_buffer.get(), begin_ptr_, old_data_size);
|
|
|
|
buffer_ = std::move(new_buffer);
|
2018-10-30 12:07:28 +01:00
|
|
|
begin_ptr_ = buffer_.get();
|
|
|
|
current_ptr_ = begin_ptr_ + old_data_size;
|
2020-06-16 04:10:16 +02:00
|
|
|
end_ptr_ = begin_ptr_ + new_buffer_size - RESERVED_SIZE;
|
2018-10-30 15:22:44 +01:00
|
|
|
CHECK(end_ptr_ > current_ptr_);
|
|
|
|
CHECK(static_cast<size_t>(end_ptr_ - current_ptr_) >= size);
|
2018-10-30 12:07:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
StringBuilder &StringBuilder::operator<<(int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_int(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(unsigned int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_uint(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_int(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long unsigned int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_uint(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long long int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_int(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long long unsigned int x) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-06-05 01:34:51 +02:00
|
|
|
current_ptr_ = print_uint(current_ptr_, x);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:38:08 +01:00
|
|
|
StringBuilder &StringBuilder::operator<<(FixedDouble x) {
|
2018-10-30 15:22:44 +01:00
|
|
|
if (unlikely(!reserve(std::numeric_limits<double>::max_exponent10 + x.precision + 4))) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2018-02-03 20:40:06 +01:00
|
|
|
|
|
|
|
static TD_THREAD_LOCAL std::stringstream *ss;
|
|
|
|
if (init_thread_local<std::stringstream>(ss)) {
|
2018-10-26 16:11:20 +02:00
|
|
|
auto previous_locale = ss->imbue(std::locale::classic());
|
2018-02-11 10:47:11 +01:00
|
|
|
ss->setf(std::ios_base::fixed, std::ios_base::floatfield);
|
2018-02-03 20:40:06 +01:00
|
|
|
} else {
|
|
|
|
ss->str(std::string());
|
|
|
|
ss->clear();
|
|
|
|
}
|
2018-02-11 12:38:08 +01:00
|
|
|
ss->precision(x.precision);
|
|
|
|
*ss << x.d;
|
2018-02-03 20:40:06 +01:00
|
|
|
|
2021-10-20 00:27:02 +02:00
|
|
|
auto len = narrow_cast<int>(static_cast<std::streamoff>(ss->tellp()));
|
2020-06-16 04:10:16 +02:00
|
|
|
auto left = end_ptr_ + RESERVED_SIZE - current_ptr_;
|
2018-02-03 17:49:39 +01:00
|
|
|
if (unlikely(len >= left)) {
|
|
|
|
error_flag_ = true;
|
2018-02-03 21:13:05 +01:00
|
|
|
len = left ? narrow_cast<int>(left - 1) : 0;
|
2018-02-03 17:49:39 +01:00
|
|
|
}
|
2018-02-03 20:40:06 +01:00
|
|
|
ss->read(current_ptr_, len);
|
|
|
|
current_ptr_ += len;
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(const void *ptr) {
|
2018-10-30 12:07:28 +01:00
|
|
|
if (unlikely(!reserve())) {
|
2018-02-03 17:49:39 +01:00
|
|
|
return on_error();
|
|
|
|
}
|
2020-06-16 04:10:16 +02:00
|
|
|
current_ptr_ += std::snprintf(current_ptr_, RESERVED_SIZE, "%p", ptr);
|
2018-02-03 17:49:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|