mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +00:00
Use std::string_view in logging code
This commit is contained in:
parent
ac11a14509
commit
b8b99d5cf1
7 changed files with 61 additions and 54 deletions
|
@ -20,10 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <functional>
|
||||
|
||||
template<int BufferLength, typename Emitter = std::function<void(const std::string &)> >
|
||||
template<int BufferLength, typename Emitter = std::function<void(std::string_view)> >
|
||||
class StringStreamBuffer : public std::streambuf {
|
||||
public:
|
||||
StringStreamBuffer(Emitter emitter) : m_emitter(emitter) {
|
||||
|
@ -38,19 +38,19 @@ public:
|
|||
void push_back(char c) {
|
||||
if (c == '\n' || c == '\r') {
|
||||
if (buffer_index)
|
||||
m_emitter(std::string(buffer, buffer_index));
|
||||
m_emitter(std::string_view(buffer, buffer_index));
|
||||
buffer_index = 0;
|
||||
} else {
|
||||
buffer[buffer_index++] = c;
|
||||
if (buffer_index >= BufferLength) {
|
||||
m_emitter(std::string(buffer, buffer_index));
|
||||
m_emitter(std::string_view(buffer, buffer_index));
|
||||
buffer_index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::streamsize xsputn(const char *s, std::streamsize n) {
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (std::streamsize i = 0; i < n; ++i)
|
||||
push_back(s[i]);
|
||||
return n;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue