-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamBuf.cpp
52 lines (41 loc) · 1.14 KB
/
StreamBuf.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
#include <cstring>
#include <limits>
#include <stdexcept>
#include "StreamBuf.h"
using shitty::StreamBuf;
void StreamBuf::advance(size_t amount) {
if (amount > size())
throw std::length_error("StreamBuf::advance out of range");
if (amount == size()) {
clear();
return;
}
// probably faster than std::copy().
in_use_ -= amount;
memmove(buf_.data(), buf_.data() + amount, in_use_);
}
void StreamBuf::grow() {
size_t newcap;
static const size_t GROWTH_FACTOR = 4;
if (size() < 4096 / GROWTH_FACTOR) {
newcap = 4096;
} else if (size() > capacity() / 2) {
if (capacity() > std::numeric_limits<size_t>::max() / GROWTH_FACTOR)
newcap = std::numeric_limits<size_t>::max();
else
newcap = capacity() * GROWTH_FACTOR;
} else {
return; // plenty of space still
}
reserve(newcap);
}
void StreamBuf::write(const void *data, size_t len) {
ensure(len);
memcpy(tail(), data, len);
addTailContent(len);
}
void StreamBuf::writeOctet(uint8_t octet) {
ensure(1);
*tail() = static_cast<char>(octet);
addTailContent(1);
}