Git Product home page Git Product logo

rsv's Introduction

Creating a separated values parser is a straightforward task, but one that meets these demanding criteria:

  • Fast performance
  • Dynamic column specification at runtime
  • Users to preview column names prior to parsing
  • Supports position-independent columns
  • Manages line breaks within cells
  • Allows user-defined data types

is considerably more difficult. This implementation successfully satisfies all these requirements.

Compiling the example:

c++ main.cpp rsv.cpp -std=c++20 -g

Basic usage

auto sequence  = std::vector<std::string>();
auto charge    = std::vector<int>();
auto ascesions = std::vector<std::string>();
auto score     = std::vector<float>();

auto [file, econd] = rsv::open("tst.tsv");

if(econd)
{
    std::cerr << econd.message() << '\n';
    std::exit(1);
}

auto schema = rsv::schema({
    rsv::f("charge"   , charge),
    rsv::f("ascesions", ascesions),
    rsv::f("sequence" , sequence),
    rsv::f("score"    , score)}
);

rsv::read(file, schema, '\t');

How to read based on column positions instead of column names?

You build the schema with the positions instead:

auto schema = rsv::schema({
    rsv::f(1, charge),
    rsv::f(2, ascesions),
    rsv::f(0, sequence),
    rsv::f(3, score)}
);

Just like with column name-based schema, the order in which the fields are presented in the schema does not matter.

How to inspect column names before calling rsv::read ?

You can call rsv::columns when, for instance, you want to inspect the column names before building the schema:

auto cols = rsv::columns(file, '\t');

Since rsv::read expects all names in the schema to be present in the file, you are expected to call this function to perform the necessary checks.

How to read values to a custom type?

This can be achieved by providing a custom field handler. A field handler is a callback function that processes the cell string and stores the result.

In this example, we convert a string representing a point in time to std::time_point:

...
auto delegate_time = [](const std::string_view& src, void* dst)
{
    auto* casted = static_cast<std::vector<time_point<system_clock>>*>(dst);
    auto [success, value] = str_to_time(src);
    casted->push_back(value);
    return success;
};

auto time = std::vector<time_point<system_clock>();

auto schema = rsv::schema({
    rsv::f("time", time, delegate_time),
    ...
);

Where str_to_time converts a std::string to a std::time_point.

What happens when values are missing?

When using the default handler the missing values are set to: an empty string if type is std::string; std::numeric_limits<T>::quiet_NaN() if T is a floating value type; std::numeric_limits<T>::max() if T is integer (bool not included).

rsv's People

Contributors

eleobert avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.