Add Ini, Csv, Semver modules

Void::Ini — IniFile parser supporting [sections], key=value pairs, ; and #
comments, quoted values, error reporting with line numbers, set/get, has_key,
sections(), serialize().

Void::Csv — RFC 4180 CSV parse/serialize with quoted fields, doubled-quote
escaping, custom delimiter, CRLF support; CsvWithHeader for named column access.

Void::Semver — Version struct with major/minor/patch/pre-release/build; parse()
(accepts optional v-prefix); to_string(); spaceship comparison per semver spec
(release > pre-release, build metadata ignored); compatible_with(), is_stable(),
bumped_major/minor/patch().

47/47 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 23:02:29 +03:00
1 parent b54f2156c5
commit cb4dd58500
6 files changed
+801

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
#pragma once
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Csv
{
using Row = std::vector<std::string>;
using Table = std::vector<Row>;
// ── parse ─────────────────────────────────────────────────────────────────────
// RFC 4180-compatible parser. Fields may be quoted ("..."). CRLF and LF
// both accepted as row terminators.
[[nodiscard]] inline Table parse(std::string_view text, char delim = ',')
{
Table result;
Row row;
std::string field;
bool in_quote = false;
for (std::size_t i = 0; i < text.size(); ++i) {
const char c = text[i];
if (in_quote) {
if (c == '"') {
// Peek for doubled quote
if (i + 1 < text.size() && text[i + 1] == '"') {
field += '"'; ++i;
} else {
in_quote = false;
}
} else {
field += c;
}
} else {
if (c == '"') {
in_quote = true;
} else if (c == delim) {
row.push_back(std::move(field));
field.clear();
} else if (c == '\n') {
row.push_back(std::move(field));
field.clear();
result.push_back(std::move(row));
row.clear();
} else if (c == '\r') {
// skip CR in CRLF
} else {
field += c;
}
}
}
// Flush last field/row
row.push_back(std::move(field));
if (!row.empty() && !(row.size() == 1 && row[0].empty())) {
result.push_back(std::move(row));
}
return result;
}
// ── serialize ─────────────────────────────────────────────────────────────────
// Produces RFC 4180 CSV. Fields that contain the delimiter, quote, or newline
// are wrapped in double quotes; embedded quotes are doubled.
[[nodiscard]] inline std::string serialize(const Table& table,
char delim = ',')
{
std::string out;
for (const auto& row : table) {
for (std::size_t i = 0; i < row.size(); ++i) {
const auto& field = row[i];
const bool needs_quote =
field.find(delim) != std::string::npos ||
field.find('"') != std::string::npos ||
field.find('\n') != std::string::npos ||
field.find('\r') != std::string::npos;
if (needs_quote) {
out += '"';
for (char c : field) {
if (c == '"') out += '"';
out += c;
}
out += '"';
} else {
out += field;
}
if (i + 1 < row.size()) out += delim;
}
out += '\n';
}
return out;
}
// ── with_header convenience ───────────────────────────────────────────────────
struct CsvWithHeader
{
Row header;
Table rows;
[[nodiscard]] std::optional<std::size_t>
col_index(std::string_view name) const
{
for (std::size_t i = 0; i < header.size(); ++i) {
if (header[i] == name) return i;
}
return std::nullopt;
}
[[nodiscard]] std::optional<std::string_view>
get(const Row& row, std::string_view name) const
{
auto idx = col_index(name);
if (!idx || *idx >= row.size()) return std::nullopt;
return row[*idx];
}
};
[[nodiscard]] inline std::optional<CsvWithHeader>
parse_with_header(std::string_view text, char delim = ',')
{
auto table = parse(text, delim);
if (table.empty()) return std::nullopt;
CsvWithHeader result;
result.header = std::move(table[0]);
result.rows.assign(std::make_move_iterator(table.begin() + 1),
std::make_move_iterator(table.end()));
return result;
}
} // namespace Void::Csv
+163
View File
@@ -0,0 +1,163 @@
#pragma once
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace Void::Ini
{
// ── IniFile ───────────────────────────────────────────────────────────────────
// Parses .ini / .cfg files with sections ([section]) and key=value pairs.
// Comments start with ';' or '#'. Inline comments are NOT stripped.
// Values may be quoted ("hello world") — quotes are stripped.
class IniFile
{
public:
using Section = std::unordered_map<std::string, std::string>;
struct ParseError
{
std::size_t line;
std::string message;
};
// Parse from a string. Returns error on malformed input.
[[nodiscard]] static std::optional<ParseError>
parse(IniFile& out, std::string_view text)
{
out.data_.clear();
std::string current_section;
std::size_t lineno = 0;
auto sv_trim = [](std::string_view s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) s.remove_prefix(1);
while (!s.empty() && (s.back() == ' ' || s.back() == '\t' ||
s.back() == '\r')) s.remove_suffix(1);
return s;
};
std::string_view remaining = text;
while (!remaining.empty()) {
++lineno;
const auto nl = remaining.find('\n');
std::string_view line = sv_trim(
nl == std::string_view::npos ? remaining
: remaining.substr(0, nl));
remaining = nl == std::string_view::npos
? std::string_view{}
: remaining.substr(nl + 1);
if (line.empty() || line[0] == ';' || line[0] == '#') continue;
if (line[0] == '[') {
const auto close = line.find(']');
if (close == std::string_view::npos) {
return ParseError{lineno, "unclosed '['"};
}
current_section = std::string{line.substr(1, close - 1)};
out.data_[current_section]; // ensure section exists
continue;
}
const auto eq = line.find('=');
if (eq == std::string_view::npos) {
if (!line.empty()) {
return ParseError{lineno, std::string{"expected '=' in: "} + std::string{line}};
}
continue;
}
std::string key{sv_trim(line.substr(0, eq))};
std::string_view val_sv = sv_trim(line.substr(eq + 1));
// Strip optional quotes
std::string val;
if (val_sv.size() >= 2 && val_sv.front() == '"' && val_sv.back() == '"') {
val = std::string{val_sv.substr(1, val_sv.size() - 2)};
} else {
val = std::string{val_sv};
}
out.data_[current_section][std::move(key)] = std::move(val);
}
return std::nullopt;
}
[[nodiscard]] static IniFile from_string(std::string_view text)
{
IniFile f;
parse(f, text);
return f;
}
[[nodiscard]] std::optional<std::string>
get(std::string_view section, std::string_view key) const
{
auto sec_it = data_.find(std::string{section});
if (sec_it == data_.end()) return std::nullopt;
auto key_it = sec_it->second.find(std::string{key});
if (key_it == sec_it->second.end()) return std::nullopt;
return key_it->second;
}
[[nodiscard]] std::string
get_or(std::string_view section, std::string_view key,
std::string_view fallback) const
{
return get(section, key).value_or(std::string{fallback});
}
void set(std::string_view section, std::string_view key, std::string value)
{
data_[std::string{section}][std::string{key}] = std::move(value);
}
[[nodiscard]] bool has_section(std::string_view section) const
{
return data_.contains(std::string{section});
}
[[nodiscard]] bool has_key(std::string_view section,
std::string_view key) const
{
return get(section, key).has_value();
}
[[nodiscard]] std::vector<std::string> sections() const
{
std::vector<std::string> result;
result.reserve(data_.size());
for (const auto& [k, _] : data_) result.push_back(k);
return result;
}
[[nodiscard]] const Section* section(std::string_view name) const
{
auto it = data_.find(std::string{name});
return it == data_.end() ? nullptr : &it->second;
}
// Serialize back to INI text.
[[nodiscard]] std::string serialize() const
{
std::string out;
for (const auto& [sec, kv] : data_) {
out += '['; out += sec; out += "]\n";
for (const auto& [k, v] : kv) {
out += k; out += " = "; out += v; out += '\n';
}
out += '\n';
}
return out;
}
private:
std::unordered_map<std::string, Section> data_;
};
} // namespace Void::Ini
+132
View File
@@ -0,0 +1,132 @@
#pragma once
#include <compare>
#include <cstdlib>
#include <format>
#include <optional>
#include <string>
#include <string_view>
namespace Void::Semver
{
// ── Version ───────────────────────────────────────────────────────────────────
struct Version
{
unsigned major{0};
unsigned minor{0};
unsigned patch{0};
std::string pre_release; // e.g. "alpha.1"
std::string build; // e.g. "20240101"
[[nodiscard]] std::string to_string() const
{
std::string s = std::format("{}.{}.{}", major, minor, patch);
if (!pre_release.empty()) { s += '-'; s += pre_release; }
if (!build.empty()) { s += '+'; s += build; }
return s;
}
// Precedence comparison per semver spec (build metadata ignored).
[[nodiscard]] std::strong_ordering
operator<=>(const Version& o) const noexcept
{
if (auto c = major <=> o.major; c != 0) return c;
if (auto c = minor <=> o.minor; c != 0) return c;
if (auto c = patch <=> o.patch; c != 0) return c;
// No pre-release > has pre-release
const bool l_pre = !pre_release.empty();
const bool r_pre = !o.pre_release.empty();
if (!l_pre && r_pre) return std::strong_ordering::greater;
if (l_pre && !r_pre) return std::strong_ordering::less;
// Both have pre-release — lexicographic (simplified)
if (auto c = pre_release <=> o.pre_release; c != 0) {
return c < 0 ? std::strong_ordering::less
: std::strong_ordering::greater;
}
return std::strong_ordering::equal;
}
[[nodiscard]] bool operator==(const Version& o) const noexcept
{
return major == o.major && minor == o.minor && patch == o.patch &&
pre_release == o.pre_release;
}
// Is this version compatible with `required`? (same major, >= minor.patch)
[[nodiscard]] bool compatible_with(const Version& required) const noexcept
{
if (major != required.major) return false;
return (*this <=> required) >= 0;
}
[[nodiscard]] bool is_stable() const noexcept
{
return major >= 1 && pre_release.empty();
}
[[nodiscard]] Version bumped_major() const noexcept
{
return {major + 1, 0, 0};
}
[[nodiscard]] Version bumped_minor() const noexcept
{
return {major, minor + 1, 0};
}
[[nodiscard]] Version bumped_patch() const noexcept
{
return {major, minor, patch + 1};
}
};
// ── Parsing ───────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::optional<Version> parse(std::string_view s)
{
Version v;
auto parse_uint = [](std::string_view sv, unsigned& out) -> bool {
if (sv.empty()) return false;
out = 0;
for (char c : sv) {
if (c < '0' || c > '9') return false;
out = out * 10 + static_cast<unsigned>(c - '0');
}
return true;
};
// Optional leading 'v'
if (!s.empty() && (s[0] == 'v' || s[0] == 'V')) s.remove_prefix(1);
// Split build metadata first
const auto plus = s.find('+');
if (plus != std::string_view::npos) {
v.build = std::string{s.substr(plus + 1)};
s = s.substr(0, plus);
}
// Split pre-release
const auto dash = s.find('-');
if (dash != std::string_view::npos) {
v.pre_release = std::string{s.substr(dash + 1)};
s = s.substr(0, dash);
}
// Major.minor.patch
const auto d1 = s.find('.');
if (d1 == std::string_view::npos) return std::nullopt;
if (!parse_uint(s.substr(0, d1), v.major)) return std::nullopt;
s = s.substr(d1 + 1);
const auto d2 = s.find('.');
if (d2 == std::string_view::npos) return std::nullopt;
if (!parse_uint(s.substr(0, d2), v.minor)) return std::nullopt;
if (!parse_uint(s.substr(d2 + 1), v.patch)) return std::nullopt;
return v;
}
} // namespace Void::Semver