repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <iosfwd>
5#include <string>
6#include <string_view>
7#include <utility>
8
10#include <librepr/macro/util.h>
11
12namespace librepr::detail {
15 StringBuffer() = default;
16
18 // We can hopefully avoid reallocations
19 // by doing this ahead of time.
20 buffer.reserve(in.size() * 2);
21 }
22
23 void write(const char* beg, const char* end) {
24 LIBREPR_ASSERT(end >= beg, "Invalid write. (end < beg)");
25 auto& target = (cursor == std::string::npos) ? buffer : inplace_buffer;
26 if(auto len = (end - beg); len > 0) {
27 auto lpos = target.size();
28 target.resize(lpos + len);
29 std::memcpy(target.data() + lpos, beg, len);
30 }
31 }
32
34 auto* beg = sv.data();
35 this->write(beg, beg + sv.size());
36 }
37
39 auto& target = (cursor == std::string::npos) ? buffer : inplace_buffer;
40 target.push_back(c);
41 }
42
44 flush();
45 // buf.shrink_to_fit();
46 return std::move(buffer);
47 }
48
49 explicit operator std::string() {
50 flush();
51 return buffer;
52 }
53
55 return &buffer;
56 }
57
59 buf.write(sv);
60 return buf;
61 }
62
63 friend StringBuffer& operator<<(StringBuffer& buf, char c){
64 buf.write(c);
65 return buf;
66 }
67
69 buf.flush();
70 return stream << buf.buffer;
71 }
72
74 return buffer.size() + inplace_buffer.size();
75 }
76
77 void set_cursor(std::size_t index = std::string::npos) {
78 flush();
79 cursor = index;
80 }
81
82 void flush() {
83 if (!inplace_buffer.empty()) {
84 // flush inplace buffer if it's not empty
85 buffer.insert(cursor, inplace_buffer);
86 inplace_buffer.clear();
87 }
88 }
89
90private:
91 std::string buffer{};
92 std::string inplace_buffer{};
93 std::size_t cursor{std::string::npos};
94};
95} // namespace librepr::detail
#define LIBREPR_ASSERT(cond,...)
Checked assertion, for constraint enforcement.
Definition assert.h:67
#define LIBREPR_HINT_INLINE
Definition macro/util.h:56
T memcpy(T... args)
Definition assert.h:89
std::string code_for()
Definition repr:39
Wrapper around std::string.
Definition buffer.h:14
friend StringBuffer & operator<<(StringBuffer &buf, char c)
Definition buffer.h:63
LIBREPR_HINT_INLINE void write(std::string_view sv)
Definition buffer.h:33
LIBREPR_HINT_INLINE void write(char c)
Definition buffer.h:38
friend StringBuffer & operator<<(StringBuffer &buf, std::string_view sv)
Definition buffer.h:58
void set_cursor(std::size_t index=std::string::npos)
Definition buffer.h:77
LIBREPR_HINT_INLINE std::string && extract()
Definition buffer.h:43
friend std::ostream & operator<<(std::ostream &stream, StringBuffer &buf)
Definition buffer.h:68
StringBuffer(std::string_view in)
Definition buffer.h:17
std::size_t size()
Definition buffer.h:73
void write(const char *beg, const char *end)
Definition buffer.h:23
void flush()
Definition buffer.h:82
LIBREPR_HINT_INLINE const std::string * operator->() const
Definition buffer.h:54