1 #ifndef GONG_IO_LINEBUFFER_HPP_
2 #define GONG_IO_LINEBUFFER_HPP_
12 template<std::size_t size>
16 explicit LineBuffer(char term = '\n') noexcept
21 char *begin() noexcept {
24 const char *begin() const noexcept {
27 char *end() noexcept {
30 const char *end() const noexcept {
34 /// extract one line from the buffer, terminator not included
35 /// @return false if the buffer does not contain a complete line
36 bool Extract(std::string &line) {
37 char *line_end = std::find(begin(), end(), term);
38 if (line_end == end()) {
41 line.assign(begin(), line_end);
43 std::move(line_end, end(), begin());
44 head -= std::distance(begin(), line_end);
48 /// get a pointer to append data to the buffer
49 /// it is safe to write at most Remain() bytes
50 char *WriteHead() noexcept {
54 // call when data has been written to WriteHead()
55 void Update(std::size_t written) {
56 assert(written <= Remain());
60 std::size_t Remain() const noexcept {
61 return std::distance(end(), buffer + size);