68cf4ae48f
This method is now const, as it should be.
209 lines
5.4 KiB
C++
209 lines
5.4 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2016-2024 Tom Zander <tom@flowee.org>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#include "ConstBuffer.h"
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
|
|
Streaming::ConstBuffer Streaming::ConstBuffer::create(const char *start, size_t size)
|
|
{
|
|
auto buf = std::shared_ptr<char>(new char[size], std::default_delete<char[]>());
|
|
memcpy(buf.get(), start, size);
|
|
return Streaming::ConstBuffer(buf, buf.get(), buf.get() + size);
|
|
}
|
|
|
|
Streaming::ConstBuffer Streaming::ConstBuffer::create(const std::vector<unsigned char> &vector)
|
|
{
|
|
auto buf = std::shared_ptr<char>(new char[vector.size()], std::default_delete<char[]>());
|
|
memcpy(buf.get(), &vector[0], vector.size());
|
|
return Streaming::ConstBuffer(buf, buf.get(), buf.get() + vector.size());
|
|
}
|
|
|
|
Streaming::ConstBuffer::ConstBuffer()
|
|
: m_buffer(nullptr),
|
|
m_start(nullptr),
|
|
m_stop(nullptr)
|
|
{
|
|
}
|
|
|
|
Streaming::ConstBuffer::ConstBuffer(std::shared_ptr<char> buffer, char const *start, char const *stop)
|
|
: m_buffer(buffer),
|
|
m_start(start),
|
|
m_stop(stop)
|
|
{
|
|
assert(stop >= start);
|
|
assert(start >= buffer.get());
|
|
assert(stop >= buffer.get());
|
|
}
|
|
|
|
char const* Streaming::ConstBuffer::begin() const
|
|
{
|
|
return m_start;
|
|
}
|
|
|
|
char const* Streaming::ConstBuffer::constData() const
|
|
{
|
|
return m_start;
|
|
}
|
|
|
|
char const* Streaming::ConstBuffer::end() const
|
|
{
|
|
return m_stop;
|
|
}
|
|
|
|
int Streaming::ConstBuffer::size() const
|
|
{
|
|
return m_stop - m_start;
|
|
}
|
|
|
|
|
|
|
|
std::string Streaming::ConstBuffer::toHex(Direction_ dir) const
|
|
{
|
|
std::string buf(size() * 2 + 1, 0);
|
|
char *pbuf = &buf.front();
|
|
for (int i = 0; i < size(); ++i) {
|
|
const uint8_t k = m_start[dir == Forward ? i : (size() - i - 1)];
|
|
sprintf(pbuf, "%02x", k);
|
|
pbuf += 2;
|
|
}
|
|
buf.pop_back(); // remove the last zero
|
|
return buf;
|
|
}
|
|
|
|
|
|
int Streaming::ConstBuffer::indexOf(char k, int offset) const
|
|
{
|
|
const char *x = m_start + offset;
|
|
while (x < m_stop) {
|
|
if (*x == k) return x - m_start;
|
|
++x;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
Streaming::ConstBuffer::operator boost::asio::const_buffer() const
|
|
{
|
|
return boost::asio::const_buffer(constData(), size());
|
|
}
|
|
|
|
std::shared_ptr<char> Streaming::ConstBuffer::internal_buffer() const
|
|
{
|
|
return m_buffer;
|
|
}
|
|
|
|
Streaming::ConstBuffer Streaming::ConstBuffer::mid(int offset, int length) const
|
|
{
|
|
if (length > 0) {
|
|
assert(m_start + offset + length <= m_stop);
|
|
return ConstBuffer(m_buffer, m_start + offset, m_start + offset + length);
|
|
}
|
|
if (length == 0)
|
|
return ConstBuffer();
|
|
assert(m_start + offset <= m_stop);
|
|
return ConstBuffer(m_buffer, m_start + offset, m_stop);
|
|
}
|
|
|
|
char Streaming::ConstBuffer::operator[](size_t idx) const
|
|
{
|
|
assert(begin() + idx < end());
|
|
return *(begin() + idx);
|
|
}
|
|
|
|
bool Streaming::ConstBuffer::startsWith(const Streaming::ConstBuffer &other) const
|
|
{
|
|
if (!other.isValid()) return false;
|
|
const auto otherSize = other.size();
|
|
if (otherSize == 0) return true;
|
|
if (otherSize > size()) return false;
|
|
if (isEmpty()) return false;
|
|
|
|
const char *s1 = m_start;
|
|
const char *s2 = other.begin();
|
|
while (s2 != other.end()) {
|
|
const uint8_t a = static_cast<uint8_t>(*s1);
|
|
const uint8_t b = static_cast<uint8_t>(*s2);
|
|
if (a != b)
|
|
return false;
|
|
s1++; s2++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Streaming::ConstBuffer::startsWith(const char byte) const
|
|
{
|
|
if (!isValid()) return false;
|
|
if (m_start == m_stop) return false;
|
|
|
|
return byte == *m_start;
|
|
}
|
|
|
|
bool Streaming::ConstBuffer::endsWith(const ConstBuffer &other) const
|
|
{
|
|
if (!other.isValid()) return false;
|
|
const auto otherSize = other.size();
|
|
if (otherSize == 0) return true;
|
|
if (otherSize > size()) return false;
|
|
if (isEmpty()) return false;
|
|
|
|
const char *s1 = m_stop - other.size();
|
|
const char *s2 = other.begin();
|
|
while (s2 != other.end()) {
|
|
const uint8_t a = static_cast<uint8_t>(*s1);
|
|
const uint8_t b = static_cast<uint8_t>(*s2);
|
|
if (a != b)
|
|
return false;
|
|
s1++; s2++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Streaming::ConstBuffer::endsWith(const char byte) const
|
|
{
|
|
if (!isValid()) return false;
|
|
if (m_start == m_stop) return false;
|
|
|
|
return byte == *(m_stop - 1);
|
|
}
|
|
|
|
bool Streaming::ConstBuffer::operator==(const Streaming::ConstBuffer &other) const
|
|
{
|
|
if (!other.isValid())
|
|
return !isValid();
|
|
if (!isValid())
|
|
return false;
|
|
if (size() != other.size())
|
|
return false;
|
|
const char *me = begin();
|
|
const char *them = other.begin();
|
|
while (me != end()) {
|
|
if (*me != *them)
|
|
return false;
|
|
++them;
|
|
++me;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Streaming::ConstBuffer::clear()
|
|
{
|
|
m_buffer = nullptr;
|
|
m_start = nullptr;
|
|
m_stop = nullptr;
|
|
}
|