https://github.com/conorwilliams/metastring
Utilities for working with strings at compile time and passing strings as template parameters.
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (7.7%) to scientific vocabulary
Keywords
Repository
Utilities for working with strings at compile time and passing strings as template parameters.
Basic Info
Statistics
- Stars: 23
- Watchers: 3
- Forks: 1
- Open Issues: 1
- Releases: 0
Topics
Metadata Files
README.md
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:
c++
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 stomt (string-to-meta-type): ```c++ using typestring = meta::stom_t<"abcd">;
//--$ typestring = meta::string<'a', 'b', 'c', 'd'>
Alternatively you can use an constexpr char array:
c++
constexpr char chars[] = "hi";
std::cout << meta::stomv<chars> << std::endl;
//--$ hi
You can slice the type to obtain the characters:
c++
constexpr auto chara = typestring::c_str[0];
std::cout << char_a << std::endl;
//--$ a
You can find the length of the string:
c++
constexpr std::sizet len = typestring::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):
c++
using intstring = meta::itomt<-8>;
//--$ int_string = meta::string<'-', '8'>
std::cout << int_string{} << std::endl;
//--$ -8
You can instantiate your string types:
c++
constexpr meta::string instance = typestring{};
```
And then implicitly convert it to a cstyle string:
```c++
std::cout << instance << std::endl;
//--$ abcd
// or explicitly: constexpr char const* cstyle = instance.cstr;
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: ```c++ constexpr int cmp = meta::compare(meta::stomv<"less">, meta::stomv<"more">);
std::cout << cmp << std::endl;
//--$ -1
And you can perform compile time string concatenation:
c++
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::stomt does by using the meta::wrap and meta::unwrap helpers:
```c++
template
// 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:
c++
using customt = myclass<"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:
c++
constexpr auto custom2 = myclass
std::cout << custom_2.value << std::endl; //--$ abcd ```
Owner
- Name: Conor Williams
- Login: ConorWilliams
- Kind: user
- Company: myrtle.ai
- Website: https://conorwilliams.github.io/
- Repositories: 6
- Profile: https://github.com/ConorWilliams
PhD Physicist & Computer-Scientist. Open source enthusiast!
GitHub Events
Total
- Watch event: 2
Last Year
- Watch event: 2
Issues and Pull Requests
Last synced: over 1 year ago
All Time
- Total issues: 1
- Total pull requests: 1
- Average time to close issues: N/A
- Average time to close pull requests: 15 minutes
- Total issue authors: 1
- Total pull request authors: 1
- Average comments per issue: 0.0
- Average comments per pull request: 1.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
- gchoinka (1)