Giter VIP home page Giter VIP logo

unformat's Introduction

Source available on GitHub

Unformat

Parsing and extraction of original data from brace style "{}" formatted strings. It basically unformats what you thought was formatted for good.

Quick Example

Unformat is simple to use and works on all basic types. See the below example for extracting a std::string and an 'int'

std::string name;
int age;

unformat("Harry is 18 years old.", "{} is {} years old.", name, age);
// name == "Harry" and age == 18

As an optimisation, if the format string is known at compile time, it can be parsed into a constant expression by making use of ay::make_format. In tests, this increases runtime speed by a factor of 3 or 4.

std::string name;
int age;

constexpr auto format = ay::make_format("{} is {} years old.");
unformat("Harry is 18 years old.", format, name, age);
// name == "Harry" and age == 18

How do I use this library?

Unformat is a single-file header only library so integration is easy. All you need to do is copy unformat.h into your project, and away you go.

Public Domain

This software is completely open source and in the public domain. See LICENSE for details.

Contributing

Pull requests are very welcome. You may also create Issues and I will have a look into it as soon as I can.

Speed

Unformat is super awesome back to the future style lightning fast compared to traditional parsing methods. Below is the output from Google Benchmark on unformat_benchmark.cpp. Great Scott!

Run on (16 X 2993 MHz CPU s)
03/13/19 18:10:57
--------------------------------------------------------------------
Benchmark                             Time           CPU Iterations
--------------------------------------------------------------------
Unformat                             72 ns         71 ns    8960000
Unformat_ConstChar                   69 ns         70 ns    8960000
Unformat_ConstexprMakeFormat         36 ns         35 ns   19478261
StdStringStream                     844 ns        854 ns     896000
StdRegex                           9975 ns      10010 ns      64000
StdScanf                           1716 ns       1726 ns     407273

unformat's People

Contributors

adamyaxley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

unformat's Issues

Benchmarking script

Create a script to automatically benchmark performance against similar tools such as std::regex and std::stringstream.

Perhaps Google Benchmark is a good option.

Optionally parse the format as constexpr

The input format is highly likely to be passed in as a string literal, and if that's the case we can use constexpr to remove the need to use std::string::find on the format at runtime, saving precious cycles.

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.