Implementation of P1402R0 for a cstring_view
.
Mimics (and delegates most of its functionality to) a std::basic_string_view
, but removes all functions that could
potentially break the invariant of null termination.
-
The conversion from a
std::string
to abasic_cstring_view
is implemented using a special constructor, since it is not possible to add a function to thestd::basic_string
class. -
Moved the
null_terminated_t
into thebasic_cstring_view
class definition. -
Enabled all
char8_t
functionality only conditionally if the feature test macro__cpp_char8_t
is defined. -
Renamed the suffix literal for a
basic_cstring_view
fromcsv
to_csv
since only standard library suffixes can omit the_
. -
Corrected
constexpr string_view_type substr(size_type pos = 0, size_type n) const;
and removed the default parameter from the first parameter. -
Enabled
starts_with
andends_with
delegation tostd::string_view
only if the feature test macro__cpp_lib_starts_ends_with
is defined, otherwise a custom implementation based on cppreference is used. -
Added
contains()
functions proposed for C++23. -
Added custom
assert()
to various functions. -
Implemented the comparison functions using
operator==
andoperator<=>
if the feature test macros__cpp_impl_three_way_comparison
and__cpp_lib_three_way_comparison
are defined. -
Added ranges helper templates, which are only enabled if the feature test macro
__cpp_lib_ranges
is defined. -
Added support for
std::format
if the feature test macro__cpp_lib_format
is defined. -
Added
[[nodiscard]]
to almost all functions.
Any compiler supporting C++17
should be sufficient (for more information see Compiler Support).
Additionally, at least CMake 3.30
is required.
The tests are implemented using Catch2, which gets shipped as single header file with this repository.
Building with GCC, Clang, or MSVC can be done using CMake presets.
git clone [email protected]:breyerml/cstring_view.git
cd cstring_view
cmake --preset [preset] -DCPP_UTIL_BUILD_EXAMPLES .
cmake --build --preset [preset]
./build/cstring_view_examples
Where [preset]
is one of gcc
, clang
, or msvc
.
To additionally build the tests and run them use:
cmake --preset [preset] -DCPP_UTIL_ENABLE_TESTS .
cmake --build --preset [preset]
ctest --preset [preset]
Tests for all supported C++
standards starting with C++17
for the currently used compiler are generated.
The cpp_util::cstring_view
has been tested with the following compilers, all installed using the respective package
manager and GitHub actions (other compilers or compiler versions may also be supported):
Compiler | Versions |
---|---|
GCC | 9.5.0, 10.5.0, 11.4.0, 12.3.0 |
Clang | 11.1.0, 12.0.1, 13.0.1, 14.0.0, 15.0.7 |
MSVC | 19.29.30154.0 |
All tests were run using the following compiler flags:
-
GCC:
-Wall -Wextra -Wpedantic -Werror
-
Clang:
-Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-unknown-warning-option -Wno-zero-as-null-pointer-constant -Wno-reserved-identifier -Werror
-Wno-c++98-compat -Wno-c++98-compat-pedantic
: sinceC++98
is not supported-Wno-unknown-warning-option
: due toclang-11.1.0
andclang-12.0.1
not recognizing the Wno-reserved-identifier option-Wno-zero-as-null-pointer-constant
: due to a warning generated byclang11.1.0
in the Catch2 header-Wno-reserved-identifier
: due to a warning generated byclang-13.0.1
when using the custom_csv
literals
-
MSVC:
/W4 /EHsc /WX
-
C++17
:- the minimum required standard version enabling all functions
-
C++20
:- the implementation of the
starts_with
andends_with
functions is now delegated to the respectivestd::string_view
implementation - the relational operators are now implemented in terms of the three-way comparison operator
operator<=>(const basic_cstring_view&, const basic_cstring_view&)
- add support for
char8_t
string views - add support for std::ranges
- the implementation of the
The actual features are enabled using the specific features test macros and not the __cplusplus
macro.