Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user