Copyright © 2019 Will Wray. Distributed under the Boost Software License, V1.0
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Also at boost.org and accompanying file LICENSE_1_0.txt
C++17. Targets GCC and Clang -std=c++17, MSVC /std:c++17
Namespaceltl::ntbs
#include "ntbs.hpp"
using ltl::ntbs;
constexpr auto hello_world = cat(cat<',',' '>("Hello","world"),'!');
static_assert( hello_world == "Hello, world!" );
constexpr auto hello_comma = cut<0,6>(hello_world);
constexpr auto world_exclaim = cut<-7>(hello_world);
#include <cstdio>
int main() { puts( cat<' '>(hello_comma, world_exclaim) ); }
Outputs "Hello, world!
", spliced back together from the sliced words.
Note that cut
indices B,E
are signed integers specifying range [B,E)
:
- Positive values index forward from begin index 0 as usual
- Negative values index backward from the end of the char array
Here, -1 serves as end index (actually the null-terminator index for NTBS)
so cut<-7>(hello_world)
is equivalent to cut<-7,-1>(hello_world)
Results are returned in a constexpr C string type; a char[N]
array wrapped in a class
and understood to contain N-1
characters followed by a terminating zero character.
This class acts like a copyable / returnable char[N]
, call it Char<N>
(the actual type,
ltl::ntbs::array<N>
, is an implementation detail, not meant for direct use).
Generic access is provided by free-function 'size' and 'data' overloads:
size(Char<N>{})
returnsN
, the array size (including null terminator)data(a)
returns aconst char*
(or array ref) for the arraybegin
Char<N>
has implicit conversion to const char(&)[N]
, decaying to const char*
.
operator==
and !=
are provided for comparisons, up to full static size
including the terminating character and any embedded null characters.
ntbs::cmp
is a constexpr version of strcmp
for lexicographic comparison.
Constexpr std::string, as proposed for C++2a, will likely replace many use cases.
C++2a constexpr string splicing will also benefit from more constexpr algorithms.
C++17 std::string_view is a good companion but constexpr size is lost in passing
to a constexpr function.
ntbs
is not intended for run-time so compile-time is the appropriate metric- Originally implemented with
index_sequence
then switched to looping
(experimented with Clang 8 constexpr builtin memcpy but saw no benefit). - Originally held non-null-terminated data, then switched to null termination -
having a null terminator is convenient in more use cases. - ntbs size is limited by the constexpr loop iteration count limit
- Compile times increase to seconds for cats around 256K chars
- Run-time use cases are constrained by the need for static size,
for example 3-char currency-pair stitching "USD"+"CAD" = "USDCAD"
Linux Travis | Windows Appveyor |
---|---|
gcc-9, clang-7 -std=c++17 |
MSVC 19.21.27702 /std:c++17 |