This commit is contained in:
2026-08-14 16:03:30 -06:00
parent d0b17d48e7
commit ffdc4975ed
2 changed files with 31 additions and 45 deletions
+17 -19
View File
@@ -25,16 +25,15 @@ namespace ckitty {
/**
* @brief Checks whether a given index falls within bounds.
*/
[[nodiscard]] constexpr inline bool fits(const isize index, const isize length) noexcept {
return index >= 0 && index < length;
[[nodiscard]] constexpr inline bool fits(const usize index, const usize length) noexcept {
return index < length;
}
/**
* @brief Checks if a requested payload size fits starting at `offset`.
*/
[[nodiscard]] constexpr inline bool fits(const isize offset, const isize length, const isize test_length) noexcept {
return offset >= 0 && test_length >= 0 && length >= 0 &&
offset <= length && test_length <= (length - offset);
[[nodiscard]] constexpr inline bool fits(const usize offset, const usize length, const usize test_length) noexcept {
return offset <= length && test_length <= (length - offset);
}
/**
@@ -73,14 +72,14 @@ namespace ckitty {
* @param[out] out_index Set to the match start index if found.
* @return true if pattern was found, false otherwise.
*/
[[nodiscard]] inline bool find(const data_view data, const data_view test, isize& out_index) noexcept {
[[nodiscard]] inline bool find(const data_view data, const data_view test, usize& out_index) noexcept {
if (test.empty()) {
out_index = 0;
return true;
}
const isize max_offset = data.length() - test.length();
for (isize off = 0; off <= max_offset; ++off) {
const usize max_offset = data.length() - test.length();
for (usize off = 0; off <= max_offset; ++off) {
if (data.sub_off(off, test.length()) == test) {
out_index = off;
return true;
@@ -92,7 +91,7 @@ namespace ckitty {
/**
* @brief Checks if an index is within [min, max] inclusively.
*/
[[nodiscard]] constexpr inline bool in_range(const isize min_val, const isize index, const isize max_val) noexcept {
[[nodiscard]] constexpr inline bool in_range(const usize min_val, const usize index, const usize max_val) noexcept {
return min_val <= index && index <= max_val;
}
@@ -100,7 +99,7 @@ namespace ckitty {
* @brief Moves buffer ownership out while replacing the source buffer with a fresh allocation.
*/
[[nodiscard]] inline buffer move_and_reallocate(buffer& buff) {
const isize len = buff.length();
const usize len = buff.length();
buffer old_buffer = std::move(buff);
buff = buffer(len);
return old_buffer;
@@ -109,8 +108,7 @@ namespace ckitty {
/**
* @brief Copies data from a data_view into a buffer at `index`, advancing `index`.
*/
inline void copy(buffer& buff, isize& index, const data_view src) noexcept {
assert(index >= 0 && "Index cannot be negative");
inline void copy(buffer& buff, usize& index, const data_view src) noexcept {
assert(index + src.length() <= buff.length() && "Buffer overflow during copy operation");
std::copy(src.begin(), src.end(), buff.begin() + index);
@@ -137,8 +135,8 @@ namespace ckitty {
}
// Calculate address width in hex digits integer-wise
isize max_addr = view.length();
int addr_width = 2;
usize max_addr = view.length();
u32 addr_width = 2;
while (max_addr > 0xFF) {
addr_width += 2;
max_addr >>= 8;
@@ -148,16 +146,16 @@ namespace ckitty {
os << prefix << std::string(addr_width, ' ')
<< " X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF | ASCII\n";
const isize total = view.length();
for (isize i = 0; i < total; i += 16) {
const usize total = view.length();
for (usize i = 0; i < total; i += 16) {
os << prefix;
os << std::hex << std::setfill('0') << std::setw(addr_width) << i << std::dec << " ";
os << std::hex << std::setfill('0') << std::setw(int(addr_width)) << i << std::dec << " ";
std::string ascii_part;
ascii_part.reserve(16);
for (isize j = 0; j < 16; ++j) {
const isize idx = i + j;
for (usize j = 0; j < 16; ++j) {
const usize idx = i + j;
if (idx < total) {
const u8 byte = view[idx];
os << hex_lookup[(byte >> 4) & 0x0F] << hex_lookup[byte & 0x0F];
+14 -26
View File
@@ -16,17 +16,6 @@ namespace ckitty {
* Lightweight wrapper equivalent to a std::span<const u8>.
*/
class data_view {
public:
using value_type = u8;
using size_type = isize;
using difference_type = isize;
using reference = const u8&;
using const_reference = const u8&;
using pointer = const u8*;
using const_pointer = const u8*;
using iterator = const u8*;
using const_iterator = const u8*;
private:
const u8* _begin{ nullptr };
const u8* _end{ nullptr };
@@ -40,9 +29,8 @@ namespace ckitty {
assert((_begin <= _end) && "Invalid range: begin > end");
}
constexpr data_view(const u8* data, const isize length) noexcept
: _begin(data), _end(data&& length > 0 ? data + length : data) {
assert(length >= 0 && "Length cannot be negative");
constexpr data_view(const u8* data, const usize length) noexcept
: _begin(data), _end(data + length) {
}
// String / String View interoperability
@@ -51,13 +39,13 @@ namespace ckitty {
_end(reinterpret_cast<const u8*>(str.data() + str.size())) {
}
constexpr data_view(std::string_view sv) noexcept
data_view(std::string_view sv) noexcept
: _begin(reinterpret_cast<const u8*>(sv.data())),
_end(reinterpret_cast<const u8*>(sv.data() + sv.size())) {
}
// Construct from char array / C-string with explicit length
constexpr data_view(const char* data, const isize length) noexcept
data_view(const char* data, const usize length) noexcept
: data_view(reinterpret_cast<const u8*>(data), length) {
}
@@ -74,8 +62,8 @@ namespace ckitty {
~data_view() = default;
// Element Access
[[nodiscard]] constexpr u8 operator[](const isize i) const noexcept {
assert(i >= 0 && i < length() && "Index out of bounds");
[[nodiscard]] constexpr u8 operator[](const usize i) const noexcept {
assert(i < length() && "Index out of bounds");
return _begin[i];
}
@@ -88,25 +76,25 @@ namespace ckitty {
[[nodiscard]] constexpr const u8* cend() const noexcept { return _end; }
// Capacity
[[nodiscard]] constexpr isize length() const noexcept { return static_cast<isize>(_end - _begin); }
[[nodiscard]] constexpr isize size() const noexcept { return length(); }
[[nodiscard]] constexpr usize length() const noexcept { return static_cast<usize>(_end - _begin); }
[[nodiscard]] constexpr usize size() const noexcept { return length(); }
[[nodiscard]] constexpr bool empty() const noexcept { return _begin == _end; }
// Subviews
/**
* @brief Returns a subview starting at `offset` with `length` bytes.
*/
[[nodiscard]] constexpr data_view sub_off(const isize offset, const isize length) const noexcept {
assert(offset >= 0 && offset <= this->length() && "Offset out of bounds");
assert(length >= 0 && (offset + length) <= this->length() && "Length out of bounds");
[[nodiscard]] constexpr data_view sub_off(const usize offset, const usize length) const noexcept {
assert(offset <= this->length() && "Offset out of bounds");
assert((offset + length) <= this->length() && "Length out of bounds");
return data_view(_begin + offset, length);
}
/**
* @brief Returns a subview in range [_begin + start, _begin + end).
*/
[[nodiscard]] constexpr data_view sub_abs(const isize start, const isize end) const noexcept {
assert(start >= 0 && start <= length() && "Start index out of bounds");
[[nodiscard]] constexpr data_view sub_abs(const usize start, const usize end) const noexcept {
assert(start <= length() && "Start index out of bounds");
assert(end >= start && end <= length() && "End index out of bounds");
return data_view(_begin + start, _begin + end);
}
@@ -114,7 +102,7 @@ namespace ckitty {
// Comparison Operators
[[nodiscard]] friend constexpr bool operator==(const data_view& lhs, const data_view& rhs) noexcept {
if (lhs.length() != rhs.length()) return false;
for (isize i = 0; i < lhs.length(); ++i) {
for (usize i = 0; i < lhs.length(); ++i) {
if (lhs[i] != rhs[i]) return false;
}
return true;