diff --git a/include/Void/Csv/Csv.hpp b/include/Void/Csv/Csv.hpp new file mode 100644 index 0000000..b47b191 --- /dev/null +++ b/include/Void/Csv/Csv.hpp @@ -0,0 +1,138 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Void::Csv +{ + +using Row = std::vector; +using Table = std::vector; + +// ── 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 + 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 + 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 + 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 diff --git a/include/Void/Ini/Ini.hpp b/include/Void/Ini/Ini.hpp new file mode 100644 index 0000000..f2c5bb0 --- /dev/null +++ b/include/Void/Ini/Ini.hpp @@ -0,0 +1,163 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +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; + + struct ParseError + { + std::size_t line; + std::string message; + }; + + // Parse from a string. Returns error on malformed input. + [[nodiscard]] static std::optional + 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 + 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 sections() const + { + std::vector 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 data_; +}; + +} // namespace Void::Ini diff --git a/include/Void/Semver/Semver.hpp b/include/Void/Semver/Semver.hpp new file mode 100644 index 0000000..af74290 --- /dev/null +++ b/include/Void/Semver/Semver.hpp @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +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 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(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 diff --git a/tests/standalone/Csv/Csv.cpp b/tests/standalone/Csv/Csv.cpp new file mode 100644 index 0000000..c4c3666 --- /dev/null +++ b/tests/standalone/Csv/Csv.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include +#include + +// ── parse ───────────────────────────────────────────────────────────────────── + +Void::Testing::Test csv_parse_simple() +{ + const auto t = Void::Csv::parse("a,b,c\n1,2,3\n"); + Void::Testing::expect_eq(t.size(), std::size_t{2}); + Void::Testing::expect_eq(t[0][0], std::string{"a"}); + Void::Testing::expect_eq(t[1][2], std::string{"3"}); +} + +Void::Testing::Test csv_parse_quoted_field() +{ + const auto t = Void::Csv::parse("\"hello, world\",42\n"); + Void::Testing::expect_eq(t[0][0], std::string{"hello, world"}); + Void::Testing::expect_eq(t[0][1], std::string{"42"}); +} + +Void::Testing::Test csv_parse_doubled_quote() +{ + const auto t = Void::Csv::parse("\"say \"\"hi\"\"\"\n"); + Void::Testing::expect_eq(t[0][0], std::string{"say \"hi\""}); +} + +Void::Testing::Test csv_parse_empty_fields() +{ + const auto t = Void::Csv::parse(",b,\n"); + Void::Testing::expect_eq(t[0].size(), std::size_t{3}); + Void::Testing::expect(t[0][0].empty()); + Void::Testing::expect(t[0][2].empty()); +} + +Void::Testing::Test csv_parse_custom_delimiter() +{ + const auto t = Void::Csv::parse("a;b;c\n", ';'); + Void::Testing::expect_eq(t[0].size(), std::size_t{3}); + Void::Testing::expect_eq(t[0][1], std::string{"b"}); +} + +Void::Testing::Test csv_parse_crlf() +{ + const auto t = Void::Csv::parse("a,b\r\n1,2\r\n"); + Void::Testing::expect_eq(t.size(), std::size_t{2}); +} + +Void::Testing::Test csv_parse_empty_input() +{ + const auto t = Void::Csv::parse(""); + Void::Testing::expect(t.empty()); +} + +// ── serialize ───────────────────────────────────────────────────────────────── + +Void::Testing::Test csv_serialize_simple() +{ + const Void::Csv::Table t = {{"a","b","c"},{"1","2","3"}}; + const auto s = Void::Csv::serialize(t); + Void::Testing::expect(s.find("a,b,c") != std::string::npos); + Void::Testing::expect(s.find("1,2,3") != std::string::npos); +} + +Void::Testing::Test csv_serialize_quotes_commas() +{ + const Void::Csv::Table t = {{"hello, world"}}; + const auto s = Void::Csv::serialize(t); + Void::Testing::expect(s.find('"') != std::string::npos); +} + +Void::Testing::Test csv_round_trip() +{ + const Void::Csv::Table original = { + {"Name","Score"},{"Alice","100"},{"Bob","90"}}; + const auto serialized = Void::Csv::serialize(original); + const auto parsed = Void::Csv::parse(serialized); + Void::Testing::expect_eq(parsed.size(), original.size()); + Void::Testing::expect_eq(parsed[0][0], original[0][0]); +} + +// ── parse_with_header ───────────────────────────────────────────────────────── + +Void::Testing::Test csv_with_header_get_column() +{ + const auto csv = Void::Csv::parse_with_header("Name,Score\nAlice,100\n"); + Void::Testing::expect(csv.has_value()); + const auto score = csv->get(csv->rows[0], "Score"); + Void::Testing::expect(score.has_value()); + Void::Testing::expect_eq(*score, std::string_view{"100"}); +} + +Void::Testing::Test csv_with_header_unknown_col() +{ + const auto csv = Void::Csv::parse_with_header("Name\nAlice\n"); + Void::Testing::expect(csv.has_value()); + Void::Testing::expect(!csv->get(csv->rows[0], "Missing").has_value()); +} + +Void::Testing::Test csv_with_header_col_index() +{ + const auto csv = Void::Csv::parse_with_header("A,B,C\n1,2,3\n"); + Void::Testing::expect(csv.has_value()); + Void::Testing::expect_eq(*csv->col_index("B"), std::size_t{1}); +} diff --git a/tests/standalone/Ini/Ini.cpp b/tests/standalone/Ini/Ini.cpp new file mode 100644 index 0000000..266efea --- /dev/null +++ b/tests/standalone/Ini/Ini.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include + +static constexpr const char* SAMPLE = R"( +; comment +[database] +host = localhost +port = 5432 +name = "my db" + +[server] +# another comment +host = 0.0.0.0 +port = 8080 +)"; + +// ── parse ───────────────────────────────────────────────────────────────────── + +Void::Testing::Test ini_parse_sections() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + Void::Testing::expect(f.has_section("database")); + Void::Testing::expect(f.has_section("server")); +} + +Void::Testing::Test ini_parse_values() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + const auto host = f.get("database", "host"); + Void::Testing::expect(host.has_value()); + Void::Testing::expect_eq(*host, std::string{"localhost"}); +} + +Void::Testing::Test ini_parse_quoted_value() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + const auto name = f.get("database", "name"); + Void::Testing::expect(name.has_value()); + Void::Testing::expect_eq(*name, std::string{"my db"}); +} + +Void::Testing::Test ini_missing_key_returns_nullopt() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + Void::Testing::expect(!f.get("database", "password").has_value()); +} + +Void::Testing::Test ini_missing_section_returns_nullopt() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + Void::Testing::expect(!f.get("auth", "token").has_value()); +} + +Void::Testing::Test ini_get_or_fallback() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + Void::Testing::expect_eq(f.get_or("database", "timeout", "30"), std::string{"30"}); +} + +Void::Testing::Test ini_has_key() +{ + auto f = Void::Ini::IniFile::from_string(SAMPLE); + Void::Testing::expect(f.has_key("server", "port")); + Void::Testing::expect(!f.has_key("server", "ssl")); +} + +// ── set / serialize ─────────────────────────────────────────────────────────── + +Void::Testing::Test ini_set_and_get() +{ + Void::Ini::IniFile f; + f.set("app", "version", "1.0"); + const auto v = f.get("app", "version"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(*v, std::string{"1.0"}); +} + +Void::Testing::Test ini_serialize_round_trip() +{ + Void::Ini::IniFile f; + f.set("section", "key", "value"); + const auto serialized = f.serialize(); + auto f2 = Void::Ini::IniFile::from_string(serialized); + const auto v = f2.get("section", "key"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(*v, std::string{"value"}); +} + +// ── error handling ──────────────────────────────────────────────────────────── + +Void::Testing::Test ini_unclosed_section_is_error() +{ + Void::Ini::IniFile f; + const auto err = Void::Ini::IniFile::parse(f, "[unclosed"); + Void::Testing::expect(err.has_value()); +} diff --git a/tests/standalone/Semver/Semver.cpp b/tests/standalone/Semver/Semver.cpp new file mode 100644 index 0000000..0327092 --- /dev/null +++ b/tests/standalone/Semver/Semver.cpp @@ -0,0 +1,161 @@ +#include +#include +#include + +#include + +// ── parse ───────────────────────────────────────────────────────────────────── + +Void::Testing::Test semver_parse_simple() +{ + const auto v = Void::Semver::parse("1.2.3"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->major, 1u); + Void::Testing::expect_eq(v->minor, 2u); + Void::Testing::expect_eq(v->patch, 3u); +} + +Void::Testing::Test semver_parse_with_v_prefix() +{ + const auto v = Void::Semver::parse("v2.0.0"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->major, 2u); +} + +Void::Testing::Test semver_parse_pre_release() +{ + const auto v = Void::Semver::parse("1.0.0-alpha.1"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->pre_release, std::string{"alpha.1"}); +} + +Void::Testing::Test semver_parse_build_metadata() +{ + const auto v = Void::Semver::parse("1.0.0+build.123"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->build, std::string{"build.123"}); +} + +Void::Testing::Test semver_parse_full() +{ + const auto v = Void::Semver::parse("2.3.4-beta.2+20240101"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->major, 2u); + Void::Testing::expect_eq(v->pre_release, std::string{"beta.2"}); + Void::Testing::expect_eq(v->build, std::string{"20240101"}); +} + +Void::Testing::Test semver_parse_invalid() +{ + Void::Testing::expect(!Void::Semver::parse("not-a-version").has_value()); + Void::Testing::expect(!Void::Semver::parse("1.2").has_value()); +} + +// ── to_string ───────────────────────────────────────────────────────────────── + +Void::Testing::Test semver_to_string_simple() +{ + Void::Semver::Version v{1, 2, 3}; + Void::Testing::expect_eq(v.to_string(), std::string{"1.2.3"}); +} + +Void::Testing::Test semver_to_string_with_pre_release() +{ + Void::Semver::Version v{1, 0, 0}; + v.pre_release = "alpha"; + Void::Testing::expect_eq(v.to_string(), std::string{"1.0.0-alpha"}); +} + +Void::Testing::Test semver_round_trip() +{ + const auto v = Void::Semver::parse("3.1.4-rc.1+build1"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect_eq(v->to_string(), std::string{"3.1.4-rc.1+build1"}); +} + +// ── comparison ─────────────────────────────────────────────────────────────── + +Void::Testing::Test semver_compare_major() +{ + const auto a = *Void::Semver::parse("2.0.0"); + const auto b = *Void::Semver::parse("1.9.9"); + Void::Testing::expect(a > b); +} + +Void::Testing::Test semver_compare_minor() +{ + const auto a = *Void::Semver::parse("1.2.0"); + const auto b = *Void::Semver::parse("1.1.9"); + Void::Testing::expect(a > b); +} + +Void::Testing::Test semver_compare_patch() +{ + const auto a = *Void::Semver::parse("1.0.1"); + const auto b = *Void::Semver::parse("1.0.0"); + Void::Testing::expect(a > b); +} + +Void::Testing::Test semver_release_gt_pre_release() +{ + const auto a = *Void::Semver::parse("1.0.0"); + const auto b = *Void::Semver::parse("1.0.0-alpha"); + Void::Testing::expect(a > b); +} + +Void::Testing::Test semver_equal() +{ + const auto a = *Void::Semver::parse("1.2.3"); + const auto b = *Void::Semver::parse("1.2.3"); + Void::Testing::expect(a == b); +} + +// ── bump ───────────────────────────────────────────────────────────────────── + +Void::Testing::Test semver_bump_major() +{ + const auto v = Void::Semver::Version{1, 2, 3}; + const auto b = v.bumped_major(); + Void::Testing::expect_eq(b.major, 2u); + Void::Testing::expect_eq(b.minor, 0u); + Void::Testing::expect_eq(b.patch, 0u); +} + +Void::Testing::Test semver_bump_minor() +{ + const auto v = Void::Semver::Version{1, 2, 3}; + const auto b = v.bumped_minor(); + Void::Testing::expect_eq(b.minor, 3u); + Void::Testing::expect_eq(b.patch, 0u); +} + +Void::Testing::Test semver_bump_patch() +{ + const auto v = Void::Semver::Version{1, 2, 3}; + Void::Testing::expect_eq(v.bumped_patch().patch, 4u); +} + +// ── compatible_with ─────────────────────────────────────────────────────────── + +Void::Testing::Test semver_compatible_same_major() +{ + const auto a = *Void::Semver::parse("1.3.0"); + const auto req = *Void::Semver::parse("1.2.0"); + Void::Testing::expect(a.compatible_with(req)); +} + +Void::Testing::Test semver_incompatible_different_major() +{ + const auto a = *Void::Semver::parse("2.0.0"); + const auto req = *Void::Semver::parse("1.9.9"); + Void::Testing::expect(!a.compatible_with(req)); +} + +Void::Testing::Test semver_is_stable() +{ + Void::Testing::expect(Void::Semver::Version{1,0,0}.is_stable()); + Void::Testing::expect(!Void::Semver::Version{0,9,0}.is_stable()); + Void::Semver::Version pre{1,0,0}; + pre.pre_release = "alpha"; + Void::Testing::expect(!pre.is_stable()); +}