Giter Club home page Giter Club logo

metastring's Introduction

Compiler explorer License: MIT

metastring

This mini library provides interfaces for converting strings to types and working with those types at compile time.

Just include the single header and go!

Tested on gcc 9.2 with: g++ example.cpp -std=c++2a -o example.out -O3 -pedantic -Wall

Info

The meta::string type is essentially a variadic char template:

template <char... args> struct meta::string;

Therefore any operations can be performed on them using variadic template meta programming, this < 150 loc library does not aspire to provide many of these operations as they can be easily implemented in the standard way.

Instead metastring focuses on constructing meta::string types by 'exploding' string literals and makes passing strings as template parameters to custom classes easy.

Examples

Meta strings

Firstly you can store a string as a type using stom_t (string-to-meta-type):

using type_string = meta::stom_t<"abcd">;

//--$ type_string = meta::string<'a', 'b', 'c', 'd'>

Alternatively you can use an constexpr char array:

constexpr char char_s[] = "hi";

std::cout << meta::stom_v<char_s> << std::endl; 

//--$ hi

You can slice the type to obtain the characters:

constexpr auto char_a = type_string::c_str[0];

std::cout << char_a << std::endl;  

//--$ a

You can find the length of the string:

constexpr std::size_t len = type_string::len();

std::cout << len << std::endl;  

//--$ 4

You could also convert an integer to a meta::string type using itom_t (integer-to-meta-type):

using int_string = meta::itom_t<-8>;

//--$ int_string = meta::string<'-', '8'>

std::cout << int_string{} << std::endl;  

//--$ -8

You can instantiate your string types:

constexpr meta::string instance = type_string{};

And then implicitly convert it to a c_style string:

std::cout << instance << std::endl;  

//--$ abcd

// or explicitly:
constexpr char const* c_style = instance.c_str;

std::cout << c_style << std::endl;  

//--$ abcd

The implicit conversion to a c style string makes working with meta strings at run time or in a constexpr functions trivial.

You can perform compile time string comparisons:

constexpr int cmp = meta::compare(meta::stom_v<"less">, meta::stom_v<"more">);

std::cout << cmp << std::endl;  

//--$ -1

And you can perform compile time string concatenation:

constexpr meta::string cat = instance + meta::stom_v<"!">;

std::cout << cat << std::endl;  

//--$ abcd!

Custom classes

You can use metastring to make templates accepting string literals like meta::stom_t does by using the meta::wrap and meta::unwrap helpers:

template <meta::wrap Str>
struct my_class {
    // my_class holds the meta::string type
    using type = meta::unwrap_t<Str>;

    // my_class holds a meta::string instance.
    static constexpr meta::string value = meta::unwrap_v<Str>;
};

You can now pass string literals to your class:

using custom_t = my_class<"some str">;

//--$ custom_t::type = meta::string<'s', 'o', 'm', 'e', ' ', 's', 't', 'r'>

constexpr custom_t custom;

std::cout << custom.value << std::endl;  

//--$ some str

You can even use meta strings instead of string literals as template arguments:

constexpr auto custom_2 = my_class<instance>{};

std::cout << custom_2.value << std::endl;  //--$ abcd

metastring's People

Contributors

conorwilliams avatar gchoinka avatar

Stargazers

 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

Forkers

vhetration

metastring's Issues

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.