#include "attribute.hpp" #include #include #include #include namespace attr { bool valid_name(const std::string& name) { if (name.empty()) { return false; } if (name.size() > 24) { return false; } for (unsigned char ch : name) { if (ch >= 'a' && ch <= 'z') { continue; } if (ch >= '0' && ch <= '9') { continue; } if (ch == '_') { continue; } return false; } return true; } int parse_value(const std::string& bytes, int32_t& output) { if (bytes.empty() || bytes.size() > 4096) { return invalid; } size_t end = bytes.size(); if (bytes[end - 1] == '\n') { --end; } if (end == 0) { return invalid; } size_t first = 0; bool negative = false; if (bytes[first] == '+' || bytes[first] == '-') { negative = bytes[first] == '-'; ++first; } if (first == end) { return invalid; } for (size_t i = first; i < end; ++i) { unsigned char ch = static_cast(bytes[i]); if (ch < '0' || ch > '9') { return invalid; } } int64_t accumulated = 0; int64_t lower = negative ? -2147483648LL : -2147483647LL; for (size_t i = first; i < end; ++i) { int digit = bytes[i] - '0'; if (accumulated < (lower + digit) / 10) { return range; } accumulated = accumulated * 10 - digit; if (accumulated < lower) { return range; } } output = static_cast(negative ? accumulated : -accumulated); return 0; } int format_value(int32_t value, char* output, size_t capacity) { if (output == nullptr) { return invalid; } std::string formatted = std::to_string(value); formatted.push_back('\n'); if (capacity <= formatted.size()) { return full; } std::memcpy(output, formatted.data(), formatted.size()); output[formatted.size()] = '\0'; return static_cast(formatted.size()); } }