mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Adds pattern scanning functionality
Implements a pattern scanner for byte sequence parsing.
Introduces `omath::PatternScanner` to parse pattern strings, which represent byte sequences.
Adds support for wildcard characters ('?' and '?') to represent optional bytes in the pattern.
Includes unit tests for basic read operations to ensure proper functionality.
This commit is contained in:
51
source/utility/pattern_scan.cpp
Normal file
51
source/utility/pattern_scan.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by Vlad on 10/4/2025.
|
||||
//
|
||||
#include "omath/utility/pattern_scan.hpp"
|
||||
#include <charconv>
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
std::expected<std::vector<std::optional<std::byte>>, PatternScanError>
|
||||
PatternScanner::parse_pattern(const std::string_view& pattern_string)
|
||||
{
|
||||
std::vector<std::optional<std::byte>> pattern;
|
||||
|
||||
auto start = pattern_string.cbegin();
|
||||
|
||||
while (start != pattern_string.cend())
|
||||
{
|
||||
const auto end = std::find(start, pattern_string.cend(), ' ');
|
||||
|
||||
const auto sting_view_start = std::distance(pattern_string.cbegin(), start);
|
||||
const auto sting_view_end = std::distance(start, end);
|
||||
|
||||
const std::string_view byte_str = pattern_string.substr(sting_view_start, sting_view_end);
|
||||
|
||||
if (byte_str.empty())
|
||||
continue;
|
||||
|
||||
if (byte_str == "?" || byte_str == "??")
|
||||
{
|
||||
pattern.emplace_back(std::nullopt);
|
||||
|
||||
start = end != pattern_string.end() ? std::next(end) : end;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::uint8_t value = 0;
|
||||
// ReSharper disable once CppTooWideScopeInitStatement
|
||||
const auto [_, error_code] = std::from_chars(byte_str.data(), byte_str.data()+byte_str.size(), value, 16);
|
||||
|
||||
if (error_code != std::errc{})
|
||||
return std::unexpected(PatternScanError::INVALID_PATTERN_STRING);
|
||||
|
||||
pattern.emplace_back(static_cast<std::byte>(value));
|
||||
|
||||
|
||||
start = end != pattern_string.end() ? std::next(end) : end;
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
} // namespace omath
|
||||
Reference in New Issue
Block a user