mirror of
https://github.com/azahar-emu/sirit
synced 2025-11-06 23:19:59 +01:00
Like the other overloads, we can insert the whole string within one operation instead of doing a byte-by-byte append. We only do byte-by-byte appending when padding is necessary.
44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
/* This file is part of the sirit project.
|
|
* Copyright (c) 2018 ReinUsesLisp
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
* Lesser General Public License version 3 or any later version.
|
|
*/
|
|
|
|
#include "stream.h"
|
|
|
|
namespace Sirit {
|
|
|
|
Stream::Stream(std::vector<u8>& bytes) : bytes(bytes) {}
|
|
|
|
Stream::~Stream() = default;
|
|
|
|
void Stream::Write(std::string_view string) {
|
|
bytes.insert(bytes.end(), string.begin(), string.end());
|
|
|
|
const auto size{string.size()};
|
|
for (std::size_t i = 0; i < 4 - size % 4; i++) {
|
|
Write(static_cast<u8>(0));
|
|
}
|
|
}
|
|
|
|
void Stream::Write(u64 value) {
|
|
const auto* const mem = reinterpret_cast<const u8*>(&value);
|
|
bytes.insert(bytes.end(), mem, mem + sizeof(u64));
|
|
}
|
|
|
|
void Stream::Write(u32 value) {
|
|
const auto* const mem = reinterpret_cast<const u8*>(&value);
|
|
bytes.insert(bytes.end(), mem, mem + sizeof(u32));
|
|
}
|
|
|
|
void Stream::Write(u16 value) {
|
|
const auto* const mem{reinterpret_cast<const u8*>(&value)};
|
|
bytes.insert(bytes.end(), mem, mem + sizeof(u16));
|
|
}
|
|
|
|
void Stream::Write(u8 value) {
|
|
bytes.push_back(value);
|
|
}
|
|
|
|
} // namespace Sirit
|