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>
118 lines
3.2 KiB
C++
118 lines
3.2 KiB
C++
#include "capture.hpp"
|
|
#include <limits>
|
|
|
|
namespace capture {
|
|
namespace {
|
|
constexpr std::int64_t billion = 1000000000;
|
|
|
|
bool canonical(const Stamp& value)
|
|
{
|
|
return value.nsec >= 0 && value.nsec < billion;
|
|
}
|
|
|
|
bool fits_i64(__int128 value)
|
|
{
|
|
return value >= std::numeric_limits<std::int64_t>::min() &&
|
|
value <= std::numeric_limits<std::int64_t>::max();
|
|
}
|
|
|
|
__int128 nanoseconds(const Stamp& value)
|
|
{
|
|
return static_cast<__int128>(value.sec) * billion + value.nsec;
|
|
}
|
|
}
|
|
|
|
Status normalize(std::int64_t sec, std::int64_t nsec, Stamp* out)
|
|
{
|
|
if (out == nullptr) {
|
|
return Status::invalid;
|
|
}
|
|
__int128 whole = static_cast<__int128>(sec) + nsec / billion;
|
|
std::int64_t remainder = nsec % billion;
|
|
if (remainder < 0) {
|
|
remainder += billion;
|
|
--whole;
|
|
}
|
|
if (!fits_i64(whole)) {
|
|
return Status::range;
|
|
}
|
|
Stamp result;
|
|
result.sec = static_cast<std::int64_t>(whole);
|
|
result.nsec = static_cast<std::int32_t>(remainder);
|
|
*out = result;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status delta_microseconds(const Stamp& later, const Stamp& earlier,
|
|
std::int64_t* out)
|
|
{
|
|
if (out == nullptr || !canonical(later) || !canonical(earlier)) {
|
|
return Status::invalid;
|
|
}
|
|
__int128 result = nanoseconds(later) / 1000 - nanoseconds(earlier) / 1000;
|
|
if (!fits_i64(result)) {
|
|
return Status::range;
|
|
}
|
|
*out = static_cast<std::int64_t>(result);
|
|
return Status::ok;
|
|
}
|
|
|
|
Status compare_stamp(const Stamp& left, const Stamp& right, int* out)
|
|
{
|
|
if (out == nullptr || !canonical(left) || !canonical(right)) {
|
|
return Status::invalid;
|
|
}
|
|
int result = 0;
|
|
if (left.sec < right.sec) {
|
|
result = -1;
|
|
} else if (left.sec > right.sec) {
|
|
result = 1;
|
|
} else if (left.nsec < right.nsec) {
|
|
result = -1;
|
|
} else if (left.nsec > right.nsec) {
|
|
result = 1;
|
|
}
|
|
*out = result;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status parse_i64(const std::string& text, std::int64_t* out)
|
|
{
|
|
if (out == nullptr || text.empty()) {
|
|
return Status::invalid;
|
|
}
|
|
std::size_t cursor = 0;
|
|
bool negative = false;
|
|
if (text[cursor] == '+' || text[cursor] == '-') {
|
|
negative = text[cursor] == '-';
|
|
++cursor;
|
|
}
|
|
if (cursor == text.size()) {
|
|
return Status::invalid;
|
|
}
|
|
for (std::size_t i = cursor; i < text.size(); ++i) {
|
|
if (text[i] < '0' || text[i] > '9') {
|
|
return Status::invalid;
|
|
}
|
|
}
|
|
std::uint64_t limit = negative ? (std::uint64_t(1) << 63) :
|
|
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());
|
|
std::uint64_t magnitude = 0;
|
|
for (; cursor < text.size(); ++cursor) {
|
|
unsigned digit = static_cast<unsigned>(text[cursor] - '0');
|
|
if (magnitude > (limit - digit) / 10) {
|
|
return Status::range;
|
|
}
|
|
magnitude = magnitude * 10 + digit;
|
|
}
|
|
if (negative && magnitude == (std::uint64_t(1) << 63)) {
|
|
*out = std::numeric_limits<std::int64_t>::min();
|
|
} else if (negative) {
|
|
*out = -static_cast<std::int64_t>(magnitude);
|
|
} else {
|
|
*out = static_cast<std::int64_t>(magnitude);
|
|
}
|
|
return Status::ok;
|
|
}
|
|
}
|