From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
4.3 KiB
C++
167 lines
4.3 KiB
C++
#include "log_gate.hpp"
|
|
#include <limits>
|
|
#include <vector>
|
|
namespace burstlog {
|
|
bool valid_time(std::uint64_t now)
|
|
{
|
|
return now <= max_time;
|
|
}
|
|
bool valid_config(const Config& config)
|
|
{
|
|
if (config.interval == 0) {
|
|
return false;
|
|
}
|
|
if (config.interval > max_interval) {
|
|
return false;
|
|
}
|
|
return config.burst <= max_burst;
|
|
}
|
|
bool window_due(std::uint64_t now, std::uint64_t deadline)
|
|
{
|
|
return now >= deadline;
|
|
}
|
|
std::uint64_t saturating_add(std::uint64_t left, std::uint64_t right)
|
|
{
|
|
const auto largest = std::numeric_limits<std::uint64_t>::max();
|
|
if (right > largest - left) {
|
|
return largest;
|
|
}
|
|
return left + right;
|
|
}
|
|
int parse_decimal(std::string_view text, std::uint64_t limit,
|
|
std::uint64_t* output)
|
|
{
|
|
if (output == nullptr || text.empty()) {
|
|
return -1;
|
|
}
|
|
std::uint64_t value = 0;
|
|
for (unsigned char byte : text) {
|
|
if (byte < '0' || byte > '9') {
|
|
return -1;
|
|
}
|
|
std::uint64_t digit = byte - '0';
|
|
if (digit > limit || value > (limit - digit) / 10) {
|
|
return -1;
|
|
}
|
|
value = value * 10 + digit;
|
|
}
|
|
*output = value;
|
|
return 0;
|
|
}
|
|
namespace {
|
|
bool separator(unsigned char byte)
|
|
{
|
|
return byte == ' ' || byte == '\t';
|
|
}
|
|
std::vector<std::string_view> tokens(std::string_view text)
|
|
{
|
|
std::vector<std::string_view> result;
|
|
std::size_t position = 0;
|
|
while (position < text.size()) {
|
|
while (position < text.size() && separator(text[position])) {
|
|
++position;
|
|
}
|
|
if (position == text.size()) {
|
|
break;
|
|
}
|
|
const auto begin = position;
|
|
while (position < text.size() && !separator(text[position])) {
|
|
++position;
|
|
}
|
|
result.push_back(text.substr(begin, position - begin));
|
|
}
|
|
return result;
|
|
}
|
|
int argument_count(Kind kind)
|
|
{
|
|
switch (kind) {
|
|
case Kind::init:
|
|
case Kind::configure:
|
|
return 5;
|
|
case Kind::log:
|
|
return 4;
|
|
case Kind::advance:
|
|
case Kind::reset:
|
|
return 3;
|
|
case Kind::snapshot:
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
int parse_command(std::string_view text, Command* output)
|
|
{
|
|
if (output == nullptr || text.size() > max_line) {
|
|
return -1;
|
|
}
|
|
const auto parts = tokens(text);
|
|
if (parts.empty()) {
|
|
return -1;
|
|
}
|
|
Command temporary;
|
|
if (parts[0] == "INIT") {
|
|
temporary.kind = Kind::init;
|
|
} else if (parts[0] == "LOG") {
|
|
temporary.kind = Kind::log;
|
|
} else if (parts[0] == "ADV") {
|
|
temporary.kind = Kind::advance;
|
|
} else if (parts[0] == "SET") {
|
|
temporary.kind = Kind::configure;
|
|
} else if (parts[0] == "RESET") {
|
|
temporary.kind = Kind::reset;
|
|
} else if (parts[0] == "SNAP") {
|
|
temporary.kind = Kind::snapshot;
|
|
} else {
|
|
return -1;
|
|
}
|
|
if (parts.size() != static_cast<std::size_t>(argument_count(temporary.kind))) {
|
|
return -1;
|
|
}
|
|
if (parse_decimal(parts[1], 1, &temporary.object) != 0) {
|
|
return -1;
|
|
}
|
|
if (temporary.kind != Kind::snapshot &&
|
|
parse_decimal(parts[2], max_time, &temporary.time) != 0) {
|
|
return -1;
|
|
}
|
|
if (temporary.kind == Kind::log &&
|
|
parse_decimal(parts[3], max_burst, &temporary.count) != 0) {
|
|
return -1;
|
|
}
|
|
if (temporary.kind == Kind::init || temporary.kind == Kind::configure) {
|
|
if (parse_decimal(parts[3], max_interval, &temporary.config.interval) != 0 ||
|
|
parse_decimal(parts[4], max_burst, &temporary.config.burst) != 0 ||
|
|
!valid_config(temporary.config)) {
|
|
return -1;
|
|
}
|
|
}
|
|
*output = temporary;
|
|
return 0;
|
|
}
|
|
std::string snapshot_json(const Snapshot& value)
|
|
{
|
|
std::string result = "[";
|
|
for (std::size_t index = 0; index < value.size(); ++index) {
|
|
if (index != 0) {
|
|
result += ',';
|
|
}
|
|
result += std::to_string(value[index]);
|
|
}
|
|
result += ']';
|
|
return result;
|
|
}
|
|
std::string result_json(const Result& value)
|
|
{
|
|
std::string result = "[";
|
|
result += std::to_string(value.allowed);
|
|
result += ',';
|
|
result += std::to_string(value.blocked);
|
|
result += ',';
|
|
result += std::to_string(value.flushed);
|
|
result += ',';
|
|
result += std::to_string(value.windows);
|
|
result += ']';
|
|
return result;
|
|
}
|
|
}
|