2018-02-03 17:49:39 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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-03 17:49:39 +01:00
|
|
|
#include <cstdio>
|
2018-02-03 20:40:06 +01:00
|
|
|
#include <locale>
|
|
|
|
#include <sstream>
|
2018-02-03 17:49:39 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// TODO: optimize
|
|
|
|
StringBuilder &StringBuilder::operator<<(int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%d", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(unsigned int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%u", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%ld", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long unsigned int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%lu", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long long int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%lld", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &StringBuilder::operator<<(long long unsigned int x) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%llu", x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:38:08 +01:00
|
|
|
StringBuilder &StringBuilder::operator<<(FixedDouble x) {
|
2018-02-03 17:49:39 +01:00
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
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)) {
|
|
|
|
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
|
|
|
|
|
|
|
int len = narrow_cast<int>(static_cast<std::streamoff>(ss->tellp()));
|
2018-02-03 17:49:39 +01:00
|
|
|
auto left = end_ptr_ + reserved_size - current_ptr_;
|
|
|
|
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) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
current_ptr_ += std::snprintf(current_ptr_, reserved_size, "%p", ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::vprintf(const char *fmt, va_list list) {
|
|
|
|
if (unlikely(end_ptr_ < current_ptr_)) {
|
|
|
|
on_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto left = end_ptr_ + reserved_size - current_ptr_;
|
|
|
|
int len = std::vsnprintf(current_ptr_, left, fmt, list);
|
|
|
|
if (unlikely(len >= left)) {
|
|
|
|
error_flag_ = true;
|
|
|
|
current_ptr_ += left - 1;
|
|
|
|
} else {
|
|
|
|
current_ptr_ += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::printf(const char *fmt, ...) {
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
vprintf(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|