GithubHelp home page GithubHelp logo

unterumarmung / fixed_string Goto Github PK

View Code? Open in Web Editor NEW
99.0 3.0 12.0 1.08 MB

C++17 string with fixed size

Home Page: https://unterumarmung.github.io/fixed_string/

License: MIT License

CMake 5.53% C++ 91.86% Python 2.61%
cpp17 cpp20 cpp-library cpp17-library constexpr cpp-templates cplusplus header-only single-file

fixed_string's Introduction

fixed_string

C++ library that provides a basic_fixed_string template that combines std::array fixed-size semantic and std::string semantic together

Features

  • C++17 or higher
  • Header-only
  • Dependency-free
  • No dynamic allocations
  • Fully constexpr
  • Can be used as class non-type template parameter (since C++20)

Examples

  • Construction
constexpr fixstr::fixed_string foo = "foo";
  • Concatenation
using namespace fixstr;
constexpr fixed_string first = "Hello, ";
constexpr fixed_string second = "World!";
constexpr auto result = first + second; // "Hello, World!"
  • Comparison
using namespace fixstr;
constexpr fixed_string first = "Hello, ";
constexpr fixed_string second = "World!";
static_assert(first == second); // false
static_assert(first != second); // true
static_assert(first < second);  // true
static_assert(first <= second); // true
static_assert(first > second);  // false 
static_assert(first >= second); // false
static_assert(first <=> second != 0); // true
  • Non-type template parameter
template <fixstr::fixed_string Foo>
void bar()
{
    static_assert(Foo == "foo"sv);
}

void foo()
{
    bar<"foo">();
}

Integration

Since it's a header only library, you need just copy fixed_string.hpp to your project.

If you are using vcpkg on your project for external dependencies, then you can use the fixed-string package.

If you are using Conan on your project for external dependencies, then you can use the Conan recipe located in the root of the repository.

Compiler compatibility

  • GCC >= 7.3
  • Clang >= 5
  • ICC >= 19.0.1
  • MSVC >= 14.28 / Visual Studio 2019 (I don't have access to older VS versions right now, so it can work on older versions too)

Using basic_fixed_string as class non-type template parameter full available in GCC >= 10 and VS 2019 16.9 or newer

fixed_string's People

Contributors

dependabot[bot] avatar dymok93 avatar georgyfirsov avatar malibushko avatar unterumarmung 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  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  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

fixed_string's Issues

Add `operator+=`

The current version of the fixed_string doesn't have operator+= overload but it has to.
Additionally, after implementing operator+= we can refactor operator+ overload for basic_fixed_string argument.

Revise default constructor implementation

Current implementation of default constructor looks like this:

constexpr basic_fixed_string() noexcept { details::fill(_data.begin(), _data.end(), static_cast<value_type>(0)); }

It can be reimplemented like this

constexpr basic_fixed_string() = default;

or even it can be not user-provided if compiler is able to generate it

Move `data` definition

In the current version data member function is in string operations section but it should be in element access one

// string operations
[[nodiscard]] constexpr pointer data() noexcept { return _data.data(); }
[[nodiscard]] constexpr const_pointer data() const noexcept { return _data.data(); }

Update cNTTP support

Since November 2020 all compilers started to support class non-type template parameters.

Conan recipe.

For easy integration with projects, it is good to have a conan recipe.

Documentation

API Reference

  • Constructors
  • operator=
  • at
  • front
  • back
  • empty
  • size & length
  • max_size
  • begin & cbegin
  • end & cend
  • rbegin & crbegin
  • rend & crend
  • data
  • c_str
  • String view conversion operator
  • substr
  • find
  • rfind
  • find_first_of
  • find_last_of
  • find_first_not_of
  • find_last_not_of
  • compare
  • starts_with
  • ends_with
  • contains
  • swap
  • Comparison operators
  • operator+
  • Hashing

Add `.c_str()`

It can just call .data()

  • Add implementation
  • Add documentation
  • Add tests

Support for C++11

It would be nice if you could make this class C++11-compatible. Many of us are stuck using older versions of the language standard...

Obviously, this will mean some constexpr members may need to become CONSTEXPR_SINCE_CPP14 or CONSTEXPR_SINCE_CPP17; and you may need to drop std::array which is pre-constexpr'ed in C++11. But it's still worth it IMHO :-) ... a bit of preprocessor magic can make it happen.

Implement strings for specific TChar types without inheritance

Using type aliases instead of inheritance makes it possible to generalize the algorithm templates for all specializations of fixstr::basic_fixed_string.

For example (see also p1406r1):

namespace std
{
template <typename TChar,
          std::size_t N,
          typename TTraits>
struct hash<fixstr::basic_fixed_string<TChar, N, TTraits>>
{
    using argument_type = fixstr::basic_fixed_string<TChar, N, TTraits>;
    size_t operator()(const argument_type& str) const
    {
        using sv_t = typename argument_type::string_view_type;
        return std::hash<sv_t>()(static_cast<sv_t>(str));
    }
};
} // namespace std

Refactor tests

Currently tests are placed in one file which is not very convenient.

The proposed solution is to separate tests to files by some logical pieces: for example, a file per a member function or a piece of functionality (e.g. hash support, string operations, etc.)

constructor from any const char* and std::string

You can't construct a fixed string from anything other than fixed strings or char[N] where N is the same size as the fixed string's size.
It would be great to have convenience constructors that take any string and just truncate them.

Add numeric conversions

Suggested API:

    // numeric conversions
    template <size_t N>
    constexpr int stoi(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr unsigned stou(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr long stol(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr unsigned long stoul(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr long long stoll(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr unsigned long long stoull(const fixed_string<N>& str, int base = 10);
    
    template <size_t N>
    constexpr float stof(const fixed_string<N>& str);
    
    template <size_t N>
    constexpr double stod(const fixed_string<N>& str);
    
    template <size_t N>
    constexpr long double stold(const fixed_string<N>& str);
    
    template <auto val> // where decltype(val) is an integral type but not any of char types
    constexpr fixed_string</*...*/> to_fixed_string() noexcept;

Proposed implementation: using to_chars and from_chars from <charconv>
Known issues: cannot be really constexpr

  • Create implementation
  • Create tests
  • Add docs

Try Rule of 0

Try to implement Rule of 0:

  • Delete copy constructor and operator= (only for basic_fixed_string).

If not possible - implement Rule of 5:

  • Add if (&other != this) check for copy operator= to eliminate excessive copies
  • Make move constructor and operator= explicitly defaulted
  • Make destructor explicitly defaulted

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.