Add StringBuilder::append_char.

This commit is contained in:
levlam 2023-09-29 13:13:56 +03:00
parent 69b46d846c
commit 811f90da2f
2 changed files with 19 additions and 1 deletions

View File

@ -8,7 +8,6 @@
#include "td/utils/misc.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include <cstdio>
#include <cstring>
@ -51,6 +50,23 @@ StringBuilder &StringBuilder::operator<<(Slice slice) {
return *this;
}
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;
}
template <class T>
static char *print_uint(char *current_ptr, T x) {
if (x < 100) {

View File

@ -44,6 +44,8 @@ class StringBuilder {
*current_ptr_++ = c;
}
void append_char(size_t count, char c);
MutableCSlice as_cslice() {
if (current_ptr_ >= end_ptr_ + RESERVED_SIZE) {
std::abort(); // shouldn't happen