dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::tsv_document Class Reference

A parsed tab-separated file. More...

#include <tsv.hpp>

Public Member Functions

const std::vector< std::string > & columns () const noexcept
 The column names, in file order.
 
std::optional< std::size_t > column_index (std::string_view name) const
 The index of a column.
 
bool has_column (std::string_view name) const
 Whether a column exists.
 
std::size_t size () const noexcept
 Number of data rows.
 
bool empty () const noexcept
 Whether there are no data rows.
 
tsv_row row (std::size_t i) const
 A row.
 
std::vector< tsv_row > rows () const
 All rows.
 
const std::string & source () const noexcept
 The source name used in errors.
 
std::uint64_t content_hash () const noexcept
 Hash of the parsed content: columns and cells, without comments, blank lines, line-end style or byte order mark.
 

Static Public Member Functions

static tsv_document parse (std::string_view text, std::string source)
 Parse text whose first line names the columns.
 
static tsv_document parse (std::string_view text, std::string source, std::vector< std::string > columns)
 Parse headerless text with the given column names.
 
static tsv_document load (const std::filesystem::path &path, std::vector< std::string > columns={})
 Read and parse a file.
 

Detailed Description

A parsed tab-separated file.

Copies share the text, so rows taken from one copy stay valid while any copy is alive.

Definition at line 423 of file tsv.hpp.

Member Function Documentation

◆ column_index()

std::optional< std::size_t > dasmig::core::tsv_document::column_index ( std::string_view  name) const
inline

The index of a column.

Parameters
nameColumn name.
Returns
The 0-based index, or empty when there is no such column.

Definition at line 486 of file tsv.hpp.

◆ columns()

const std::vector< std::string > & dasmig::core::tsv_document::columns ( ) const
inlinenoexcept

The column names, in file order.

Returns
The columns.

Definition at line 478 of file tsv.hpp.

◆ content_hash()

std::uint64_t dasmig::core::tsv_document::content_hash ( ) const
inlinenoexcept

Hash of the parsed content: columns and cells, without comments, blank lines, line-end style or byte order mark.

The same data checked out with LF or CRLF line ends hashes the same.

Returns
64-bit FNV-1a of the canonical content.

Definition at line 537 of file tsv.hpp.

◆ empty()

bool dasmig::core::tsv_document::empty ( ) const
inlinenoexcept

Whether there are no data rows.

Returns
True when empty.

Definition at line 505 of file tsv.hpp.

◆ has_column()

bool dasmig::core::tsv_document::has_column ( std::string_view  name) const
inline

Whether a column exists.

Parameters
nameColumn name.
Returns
True when present.

Definition at line 494 of file tsv.hpp.

◆ load()

static tsv_document dasmig::core::tsv_document::load ( const std::filesystem::path &  path,
std::vector< std::string >  columns = {} 
)
inlinestatic

Read and parse a file.

Parameters
pathFile to read; it is also the source name in errors.
columnsColumn names for a headerless file; empty when the first line names them.
Returns
The document.
Exceptions
tsv_errorIf the file cannot be read, or as parse().

Definition at line 460 of file tsv.hpp.

◆ parse() [1/2]

static tsv_document dasmig::core::tsv_document::parse ( std::string_view  text,
std::string  source 
)
inlinestatic

Parse text whose first line names the columns.

Parameters
textUTF-8 text.
sourceName used in errors (a file name, or a description such as "layer from host").
Returns
The document.
Exceptions
tsv_errorOn invalid UTF-8, no header, an empty or repeated column name, or a row with more cells than columns.

Definition at line 433 of file tsv.hpp.

◆ parse() [2/2]

static tsv_document dasmig::core::tsv_document::parse ( std::string_view  text,
std::string  source,
std::vector< std::string >  columns 
)
inlinestatic

Parse headerless text with the given column names.

Parameters
textUTF-8 text.
sourceName used in errors.
columnsColumn names.
Returns
The document.
Exceptions
tsv_errorAs the other overload.

Definition at line 444 of file tsv.hpp.

◆ row()

tsv_row dasmig::core::tsv_document::row ( std::size_t  i) const
inline

A row.

Parameters
i0-based row index.
Returns
The row.
Exceptions
std::out_of_rangeIf i >= size().

Definition at line 511 of file tsv.hpp.

◆ rows()

std::vector< tsv_row > dasmig::core::tsv_document::rows ( ) const
inline

All rows.

Returns
The rows, in file order.

Definition at line 519 of file tsv.hpp.

◆ size()

std::size_t dasmig::core::tsv_document::size ( ) const
inlinenoexcept

Number of data rows.

Returns
The row count.

Definition at line 501 of file tsv.hpp.

◆ source()

const std::string & dasmig::core::tsv_document::source ( ) const
inlinenoexcept

The source name used in errors.

Returns
File name or description.

Definition at line 529 of file tsv.hpp.


The documentation for this class was generated from the following file: