-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
StringBuilder.cpp
122 lines (105 loc) · 3.03 KB
/
StringBuilder.cpp
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
//
// 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"
#include "td/utils/misc.h"
#include <cstdio>
#include <locale>
#include <sstream>
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;
}
StringBuilder &StringBuilder::operator<<(double x) {
if (unlikely(end_ptr_ < current_ptr_)) {
return on_error();
}
static TD_THREAD_LOCAL std::stringstream *ss;
if (init_thread_local<std::stringstream>(ss)) {
ss->imbue(std::locale::classic());
} else {
ss->str(std::string());
ss->clear();
}
*ss << x;
int len = narrow_cast<int>(static_cast<std::streamoff>(ss->tellp()));
auto left = end_ptr_ + reserved_size - current_ptr_;
if (unlikely(len >= left)) {
error_flag_ = true;
len = left - 1;
}
ss->read(current_ptr_, len);
current_ptr_ += len;
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