-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.cpp
73 lines (58 loc) · 1.38 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
//
// Created by jackson on 11.04.16.
//
#include <assert.h>
#include "buffer.h"
#include "util.h"
//!caution! after (trim) result of get will be unpredictable
const char *buffer::get(int amount) {
assert(amount + offset <= data.length());
if (data.length() >= 10000) {
trim();
}
const char *res = data.c_str() + offset;
prev_offset = offset;
offset += amount;
last_method = GET;
return res;
}
void buffer::stash() {
if (last_method == GET) {
offset = prev_offset;
} else if (last_method == PUT) {
data = data.substr(0, prev_data_size);
} else {
throw new std::logic_error("Stashing not after put or get.");
}
last_method = STASH;
}
void buffer::put(const char *const data, size_t length) {
prev_data_size = this->data.size();
this->data += std::string(data, length);
last_method = PUT;
}
void buffer::set(const char *const data) {
clear();
put(data, strlen(data));
last_method = SET;
}
bool buffer::empty() {
return length() == 0;
}
int buffer::length() {
return (int) (data.length() - offset);
}
const std::string &buffer::string_data() {
trim();
return data;
}
void buffer::clear() {
std::string().swap(data);
offset = 0;
last_method = CLEAR;
}
void buffer::trim() {
data = data.substr(offset, data.length() - offset);
offset = 0;
last_method = TRIM;
}