File: nonowning_buffer.cpp

package info (click to toggle)
libfilezilla 0.54.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,504 kB
  • sloc: cpp: 31,105; sh: 4,241; makefile: 375; xml: 37
file content (82 lines) | stat: -rw-r--r-- 1,395 bytes parent folder | download
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
#include "libfilezilla/nonowning_buffer.hpp"

#include <string.h>

namespace fz {

void nonowning_buffer::resize(size_t size)
{
	if (!size) {
		start_ = 0;
	}
	else if (size > capacity_) {
		abort();
	}
	else if (size > capacity_ - start_) {
		// If here, size_ is less than size
		memmove(buffer_, buffer_ + start_, size_);
		start_ = 0;
	}
	size_ = size;
}

uint8_t* nonowning_buffer::get(size_t bytes)
{
	if (capacity_ - size_ < bytes) {
		abort();
	}
	else if (capacity_ - size_ < bytes + start_) {
		memmove(buffer_, buffer_ + start_, size_);
		start_ = 0;
	}
	return buffer_ + start_ + size_;
}

void nonowning_buffer::add(size_t bytes)
{
	if (capacity_ - start_ - size_ < bytes) {
		abort();
	}
	size_ += bytes;
}

void nonowning_buffer::consume(size_t bytes)
{
	if (bytes > size_) {
		bytes = size_;
	}
	size_ -= bytes;
	if (!size_) {
		start_ = 0;
	}
	else {
		start_ += bytes;
	}
}

void nonowning_buffer::reset()
{
	buffer_ = nullptr;
	capacity_ = 0;
	size_ = 0;
	start_ = 0;
}

void nonowning_buffer::append(uint8_t const* data, size_t len)
{
	if (data && len) {
		memcpy(get(len), data, len);
		size_ += len;
	}
}

std::string_view nonowning_buffer::to_view() const
{
	return std::string_view(reinterpret_cast<char const*>(get()), size());
}

void nonowning_buffer::append(std::string_view const& str)
{
	return append(reinterpret_cast<uint8_t const*>(str.data()), str.size());
}
}