Home

Awesome

ntbs: null-terminated byte strings

cat and cut: concatenate and slice<br>functions for splicing 'constexpr C strings'

<details><summary>Copyright &copy; 2019 Will Wray. Distributed under the Boost Software License, V1.0</summary>

Boost Software License - Version 1.0 - August 17th, 2003

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.

License

Also at boost.org and accompanying file LICENSE_1_0.txt

</details>

C++17. Targets GCC and Clang -std=c++17, MSVC /std:c++17
Namespace ltl::ntbs

Hello-world example: cat

 #include "ntbs.hpp"
 using ltl::ntbs;

 constexpr auto hello_world = cat(cat<',',' '>("Hello","world"),'!');

 static_assert( hello_world == "Hello, world!" );

Hello-world example continued: cut

 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):

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:

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.

Design notes

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.

Linux TravisWindows Appveyor
gcc-9, clang-7<br>-std=c++17MSVC 19.21.27702<br>/std:c++17
Build StatusBuild status