-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringbuffer.hpp
36 lines (30 loc) · 880 Bytes
/
stringbuffer.hpp
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
#pragma once
#ifndef STRINGBUFFER_HPP_BE7PHSAO
#define STRINGBUFFER_HPP_BE7PHSAO
#include "appendbuffer.hpp"
#include "stringref.hpp"
#include "string.hpp"
namespace ftl {
class StringBuffer : public AppendBuffer<char> {
public:
String to_string() const;
size_t length() const { return size(); }
StringBuffer& operator<<(const StringRef& str);
};
inline StringBuffer& StringBuffer::operator<<(const StringRef& str) {
str.each([&](char c){ this->push(c); });
return *this;
}
inline String StringBuffer::to_string() const {
String ret;
ret._length = length();
ret._data = new char[ret._length];
size_t written = 0;
_pages.each([&](const AppendBuffer<char>::Page& page) {
copy_range(ret._data + written, page.data(), page.size());
written += page.size();
});
return ret;
}
}
#endif /* end of include guard: STRINGBUFFER_HPP_BE7PHSAO */