aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/StringBuilder.h
blob: 359cd8cc45601f198c814697944d4a9f2bfdc39f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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

#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/StackAllocator.h"

#include <cstdlib>
#include <memory>
#include <type_traits>
#include <utility>

namespace td {

class StringBuilder {
 public:
  explicit StringBuilder(MutableSlice slice, bool use_buffer = false);

  StringBuilder() : StringBuilder({}, true) {
  }

  void clear() {
    current_ptr_ = begin_ptr_;
    error_flag_ = false;
  }

  void pop_back() {
    CHECK(current_ptr_ > begin_ptr_);
    current_ptr_--;
  }

  void push_back(char c) {
    if (unlikely(end_ptr_ <= current_ptr_)) {
      if (!reserve_inner(RESERVED_SIZE)) {
        error_flag_ = true;
        return;
      }
    }
    *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
    }
    *current_ptr_ = 0;
    return MutableCSlice(begin_ptr_, current_ptr_);
  }

  size_t size() {
    return static_cast<size_t>(current_ptr_ - begin_ptr_);
  }

  bool is_error() const {
    return error_flag_;
  }

  template <class T>
  std::enable_if_t<std::is_same<char *, std::remove_const_t<T>>::value, StringBuilder> &operator<<(T str) {
    return *this << Slice(str);
  }
  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);
  }

  StringBuilder &operator<<(const wchar_t *str) = delete;

  StringBuilder &operator<<(Slice slice);

  StringBuilder &operator<<(bool b) {
    return *this << (b ? Slice("true") : Slice("false"));
  }

  StringBuilder &operator<<(char c) {
    if (unlikely(!reserve())) {
      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);
  }

  StringBuilder &operator<<(int x);

  StringBuilder &operator<<(unsigned int x);

  StringBuilder &operator<<(long int x);

  StringBuilder &operator<<(long unsigned int x);

  StringBuilder &operator<<(long long int x);

  StringBuilder &operator<<(long long unsigned int x);

  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);
  }

  StringBuilder &operator<<(const void *ptr);

  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 << '}';
  }

 private:
  char *begin_ptr_;
  char *current_ptr_;
  char *end_ptr_;
  bool error_flag_ = false;
  bool use_buffer_ = false;
  std::unique_ptr<char[]> buffer_;
  static constexpr size_t RESERVED_SIZE = 30;

  StringBuilder &on_error() {
    error_flag_ = true;
    return *this;
  }

  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);
};

template <class T>
std::enable_if_t<std::is_arithmetic<T>::value && !std::is_same<std::decay_t<T>, bool>::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