-
Notifications
You must be signed in to change notification settings - Fork 1
/
sh_sstream.h
437 lines (378 loc) · 12.6 KB
/
sh_sstream.h
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#ifndef __SH_STR_STREAM_H__
#define __SH_STR_STREAM_H__
#include <limits>
#include <string>
#include <assert.h>
#include <algorithm>
#ifdef __GNUC__
#include <stdint.h>
#endif
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#ifndef __GNUC__
#if _MSC_VER >= 1900
#else
#ifndef snprintf
#define snprintf _snprintf
#endif
#endif
#endif
namespace share
{
namespace sstream
{
template<bool> struct STATIC_CHECK;
template<> struct STATIC_CHECK<true>{};
// (C) Copyright Beman Dawes 1999-2003. 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)
// See http://www.boost.org/libs/utility for documentation.
// Private copy constructor and copy assignment ensure classes derived from
// class noncopyable cannot be copied.
// Contributed by Dave Abrahams
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};
template<typename Buffer> class StrStream;
static const int MAX_NUMERIC_SIZE = 32;
static STATIC_CHECK< (MAX_NUMERIC_SIZE - 12 > std::numeric_limits<double>::digits10) > __check3;
static STATIC_CHECK< (MAX_NUMERIC_SIZE - 12 > std::numeric_limits<long double>::digits10) > __check4;
static STATIC_CHECK< (MAX_NUMERIC_SIZE - 12 > std::numeric_limits<long>::digits10) > __check5;
static STATIC_CHECK< (MAX_NUMERIC_SIZE - 12 > std::numeric_limits<long long>::digits10) > __check6;
// 理论上 StreamBuf 支持字符串堆缓冲区最大长度 std::numeric_limits<int>::max()-1
// 栈缓冲取就比较小. 一般在要求效率,并且不需要很大的缓冲区情况下,优先使用栈缓冲区
template<int SIZE>
class StreamBuf : public noncopyable
{
friend class StrStream<StreamBuf>;
public:
StreamBuf() : _heap_data(NULL)
{
_reset();
}
~StreamBuf()
{
if (_heap_data) {
::free(_heap_data);
}
}
// 返回的字符串,会加上字符串结束符
const char* data() const
{
*(((StreamBuf*)this)->_cur)='\0';
return _data;
}
inline int length() const
{
return static_cast<int>(_cur - _data);
}
inline int avail() const
{
return static_cast<int>(end() - _cur);
}
void append(const char* buf, int len)
{
assert(len>=0);
if (avail() < len) {
if ( NULL == _malloc_heap(cur_capacity()*2+len) ) {
return;
}
}
::memcpy(_cur, buf, len);
_cur += len;
}
inline char* current()const
{
return _cur;
}
// 这是一个奇怪的接口,从右到左,减少 len给字符
inline void dec_length(int len)
{
if (len<1) return;
if (len >= static_cast<int>(_cur - _data)) {
_cur = _data;
return;
}
_cur -= len;
}
inline const char* end() const
{
return _data + cur_capacity();
}
inline int cur_capacity()const
{
return _capacity;
}
private:
// 使用者自己保证 len的有效性
inline void _inc_length(int len)
{
assert(len>=0);
_cur += len;
}
void _reset()
{
_stack_data[0] = 0;
::free(_heap_data);
_heap_data = NULL;
_data = _stack_data;
_cur = _data;
_capacity = SIZE;
}
// 申请成功返回 not NULL, 否则返回NULL
char* _malloc_heap(int new_size)
{
if (new_size <= _capacity) { // 申请新的空间应该要比原来的空间大
assert(false);
return NULL;
}
if (NULL == _heap_data) {
_heap_data = (char*)::malloc(new_size+1);
if (NULL == _heap_data) {
assert(false && "not enough memory");
return NULL;
}
_capacity = new_size;
int n = static_cast<int>(_cur - _data);
::memcpy(_heap_data, _data, n);
_data = _heap_data;
_cur = _data + n;
} else {
char* newbuf=(char*)::malloc(new_size+1);
if (NULL == newbuf){
assert(false && "not enough memory");
return NULL;
}
_capacity = new_size;
int n = static_cast<int>(_cur - _data);
::memcpy(newbuf, _data, n);
_data = newbuf;
_cur = _data + n;
::free(_heap_data);
_heap_data = newbuf;
}
return _data;
}
private:
char* _data;
char* _cur;
char _stack_data[SIZE+1]; // 栈空间
char*_heap_data; // 如果发生动态申请内存,这里指向申请到的内存首地址
int _capacity; // 当前的空间容纳量.
};
template<typename T>
size_t convert(char buf[], T value)
{
static const char digits[] = "9876543210123456789";
T i = value;
char* p = buf;
T lsd = 0;
do {
lsd = i % 10;
i /= 10;
*p++ = digits[lsd+9];
} while (i != 0);
if (value < 0) {
*p++ = '-';
}
*p = '\0';
std::reverse(buf, p);
return p - buf;
}
static size_t convert2hex(char buf[], uintptr_t value)
{
static const char digitsHex[] = "0123456789abcdef";
uintptr_t i = value;
char* p = buf;
uintptr_t lsd = 0;
do {// 这里 i 为无符号整数时,可以用位运算代替除法,取模, 效率更高
lsd = (i&(16-1)); //i % 16;
i = (i>>4);//i /= 16;
*p++ = digitsHex[lsd];
} while (i != 0);
*p = '\0';
std::reverse(buf, p);
return p - buf;
}
class StrMark
{
public:
StrMark(const char* str, int len):_str(str), _len(len)
{
assert(::strlen(str) == _len);
}
const char* const _str;
const size_t _len;
};
template<typename Buffer>
class StrStream : public noncopyable
{
public:
typedef StrStream self;
self& operator<<(bool v)
{
_buffer.append(v ? "1" : "0", 1);
return *this;
}
self& operator<<(char v)
{
_buffer.append(&v, 1);
return *this;
}
self& operator<<(unsigned char v)
{
*this << static_cast<unsigned int>(v);
return *this;
}
self& operator<<(short v)
{
*this << static_cast<int>(v);
return *this;
}
self& operator<<(unsigned short v)
{
*this << static_cast<unsigned int>(v);
return *this;
}
self& operator<<(int v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(unsigned int v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(long v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(unsigned long v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(long long v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(unsigned long long v)
{
_fmt_integer(v);
return *this;
}
self& operator<<(const void* p)
{
uintptr_t v = reinterpret_cast<uintptr_t>(p);
if (_buffer.avail() < MAX_NUMERIC_SIZE) {
if ( NULL == _buffer._malloc_heap(2*_buffer.cur_capacity()+MAX_NUMERIC_SIZE) ) {
assert(false);
return *this;
}
}
char* buf = _buffer.current();
buf[0] = '0';
buf[1] = 'x';
_buffer._inc_length(static_cast<int>(convert2hex(buf+2, v))+2);
return *this;
}
self& operator<<(float v)
{
*this << static_cast<double>(v);
return *this;
}
self& operator<<(double v)
{
if (_buffer.avail() < MAX_NUMERIC_SIZE) {
if ( NULL == _buffer._malloc_heap(2*_buffer.cur_capacity()+MAX_NUMERIC_SIZE) ) {
return *this;
}
}
int len = snprintf(_buffer.current(), MAX_NUMERIC_SIZE, "%.12g", v);
_buffer._inc_length(len);
return *this;
}
self& operator<<(long double v)
{
if (_buffer.avail() < MAX_NUMERIC_SIZE) {
if ( NULL == _buffer._malloc_heap(2*_buffer.cur_capacity()+MAX_NUMERIC_SIZE) ) {
return *this;
}
}
int len = snprintf(_buffer.current(), MAX_NUMERIC_SIZE, "%.12g", v);
_buffer._inc_length(len);
return *this;
}
self& operator<<(const StrMark& v)
{
_buffer.append(v._str, static_cast<int>(v._len));
return *this;
}
self& operator<<(const char* v)
{
_buffer.append(v, static_cast<int>(strlen(v)));
return *this;
}
self& operator<<(const std::string& v)
{
_buffer.append(v.c_str(), static_cast<int>(v.size()));
return *this;
}
void append(const char* data, int len)
{
_buffer.append(data, len);
}
// 返回的字符串,会加上字符串结束符
const char* data() const
{
return _buffer.data();
}
inline int length() const
{
return _buffer.length();
}
// return the string copy
std::string to_string() const
{
return std::string(_buffer.data(), _buffer.length());
}
// 返回的字符串,会加上字符串结束符
const char* c_str()const
{
return _buffer.data();
}
void reset_buffer()
{
_buffer._reset();
}
// 这是一个奇怪的接口,从右到左,减少 len给字符
void dec_length(int len)
{
_buffer.dec_length(len);
}
private:
template<typename T>
void _fmt_integer(T v)
{
if (_buffer.avail() < MAX_NUMERIC_SIZE) {
if ( NULL == _buffer._malloc_heap(2*_buffer.cur_capacity()+MAX_NUMERIC_SIZE) ) {
return ;
}
}
_buffer._inc_length(static_cast<int>(convert(_buffer.current(), v)));
}
Buffer _buffer;
};
}
}
#endif // __SH_STR_STREAM_H__