-
Notifications
You must be signed in to change notification settings - Fork 5
/
Buffer.cpp
executable file
·104 lines (85 loc) · 1.65 KB
/
Buffer.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
#include "Buffer.h"
#include "Util.h"
#include <algorithm>
#include <cstring>
#include <iostream>
namespace khaki{
Buffer::Buffer() : buf_(NULL), begin_(0), end_(0), cap_(0), size_(0) {
}
Buffer::Buffer(std::string& str)
{
append(str.data(), str.size());
}
Buffer::Buffer(const Buffer& buf)
{
buf_ = new char[buf.size()];
memcpy(buf_, buf.buf_, (size_t)buf.size());
begin_ = 0;
end_ = buf.size();
cap_ = buf.size();
size_ = buf.size();
}
Buffer::~Buffer() {
if (buf_) delete[] buf_;
}
void Buffer::clear() {
size_ = 0;
begin_ = 0;
end_ = 0;
}
bool Buffer::empty() const {
return (end_ - begin_ > 0) ? false : true;
}
uint32_t Buffer::size() const {
return end_ - begin_;
}
char* Buffer::begin() const {
return buf_ + begin_;
}
char* Buffer::end() const {
return buf_ + end_;
}
char* Buffer::data() {
return buf_ + begin_;
}
void Buffer::move()
{
std::copy(begin(), end(), buf_);
end_ -= begin_;
begin_ = 0;
}
void Buffer::append(const char* buf, uint32_t len)
{
move();
if (end_ + len > cap_) alloc(end_ + len);
std::copy(buf, buf + len, end());
end_ += len;
}
void Buffer::alloc(uint32_t len)
{
char* bufTemp = new char[2 * len];
memset(bufTemp, 0, 2 * len);
std::copy(begin(), end(), bufTemp);
delete[] buf_;
buf_ = bufTemp;
end_ -= begin_;
begin_ = 0;
cap_ = 2 * len;
}
void Buffer::addBegin(uint32_t len)
{
if ( len >= size() ) {
begin_ += size();
return;
}
begin_ += len;
if ( begin_ == end_ ) {
begin_ = 0;
end_ = 0;
}
}
std::string Buffer::show()
{
return util::string_format("[%d %d %d]", begin_, end_, cap_);
}
}