Skip to content

Commit

Permalink
Improve: C++ version macros naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Dec 8, 2024
1 parent 6d61c21 commit 19c2ae9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"cheminformatics",
"cibuildwheel",
"CONCAT",
"constexpr",
"copydoc",
"Corasick",
"cptr",
Expand Down Expand Up @@ -82,6 +83,7 @@
"Merkle-Damgård",
"Mersenne",
"MODINIT",
"MSVC",
"napi",
"nargsf",
"ndim",
Expand Down Expand Up @@ -120,7 +122,7 @@
"startswith",
"STL",
"stringzilla",
"stringzillite",
"stringzilla_bare",
"Strs",
"strzl",
"substr",
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#
# Tests for different C++ standards:
#
# - stringzilla_test_cpp11: A test executable for C++11.
# - stringzilla_test_cpp14: A test executable for C++14.
# - stringzilla_test_cpp17: A test executable for C++17.
# - stringzilla_test_cpp20: A test executable for C++20.
# - stringzilla_test_cpp11: C++11 baseline support.
# - stringzilla_test_cpp14: C++14 support with `std::less<std::string>`-like function objects.
# - stringzilla_test_cpp17: C++17 support with `std::string_view` compatibility.
# - stringzilla_test_cpp20: C++20 support with `<=>` operator and more `constexpr` features.
#
# Tests for different SIMD architectures:
#
Expand Down
26 changes: 13 additions & 13 deletions include/stringzilla/stringzilla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@
/* We need to detect the version of the C++ language we are compiled with.
* This will affect recent features like `operator<=>` and tests against STL.
*/
#define SZ_DETECT_CPP_23 (__cplusplus >= 202101L)
#define SZ_DETECT_CPP20 (__cplusplus >= 202002L)
#define SZ_DETECT_CPP_17 (__cplusplus >= 201703L)
#define SZ_DETECT_CPP14 (__cplusplus >= 201402L)
#define SZ_DETECT_CPP_11 (__cplusplus >= 201103L)
#define SZ_DETECT_CPP_98 (__cplusplus >= 199711L)
#define _SZ_IS_CPP23 (__cplusplus >= 202101L)
#define _SZ_IS_CPP20 (__cplusplus >= 202002L)
#define _SZ_IS_CPP17 (__cplusplus >= 201703L)
#define _SZ_IS_CPP14 (__cplusplus >= 201402L)
#define _SZ_IS_CPP11 (__cplusplus >= 201103L)
#define _SZ_IS_CPP98 (__cplusplus >= 199711L)

/**
* @brief The `constexpr` keyword has different applicability scope in different C++ versions.
* Useful for STL conversion operators, as several `std::string` members are `constexpr` in C++20.
*/
#if SZ_DETECT_CPP20
#if _SZ_IS_CPP20
#define sz_constexpr_if_cpp20 constexpr
#else
#define sz_constexpr_if_cpp20
Expand All @@ -50,7 +50,7 @@
#include <bitset>
#include <string>
#include <vector>
#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view
#include <string_view>
#endif
#endif
Expand Down Expand Up @@ -398,7 +398,7 @@ struct end_sentinel_type {};
struct include_overlaps_type {};
struct exclude_overlaps_type {};

#if SZ_DETECT_CPP_17
#if _SZ_IS_CPP17
inline static constexpr end_sentinel_type end_sentinel;
inline static constexpr include_overlaps_type include_overlaps;
inline static constexpr exclude_overlaps_type exclude_overlaps;
Expand Down Expand Up @@ -1265,7 +1265,7 @@ class basic_string_slice {
return os.write(str.data(), str.size());
}

#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view

template <typename sfinae_ = char_type, typename std::enable_if<std::is_const<sfinae_>::value, int>::type = 0>
sz_constexpr_if_cpp20 basic_string_slice(std::string_view const &other) noexcept
Expand Down Expand Up @@ -1496,7 +1496,7 @@ class basic_string_slice {
sz_equal(data() + other.first.size(), other.second.data(), other.second.size()) == sz_true_k;
}

#if SZ_DETECT_CPP20
#if _SZ_IS_CPP20

/** @brief Computes the lexicographic ordering between this and the ::other string. */
std::strong_ordering operator<=>(string_view other) const noexcept {
Expand Down Expand Up @@ -2175,7 +2175,7 @@ class basic_string {
return os.write(str.data(), str.size());
}

#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view

basic_string(std::string_view other) noexcept(false) : basic_string(other.data(), other.size()) {}
basic_string &operator=(std::string_view other) noexcept(false) { return assign({other.data(), other.size()}); }
Expand Down Expand Up @@ -2421,7 +2421,7 @@ class basic_string {
bool operator==(string_view other) const noexcept { return view() == other; }
bool operator==(const_pointer other) const noexcept { return view() == string_view(other); }

#if SZ_DETECT_CPP20
#if _SZ_IS_CPP20

/** @brief Computes the lexicographic ordering between this and the ::other string. */
std::strong_ordering operator<=>(basic_string const &other) const noexcept { return view() <=> other.view(); }
Expand Down
6 changes: 3 additions & 3 deletions include/stringzilla/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,12 @@ SZ_PUBLIC void sz_sequence_from_u64tape( //
#define SZ_CACHE_LINE_WIDTH (64) // bytes

/**
* @brief Similar to `assert`, the `_sz_assert` is used in the SZ_DEBUG mode
* to check the invariants of the library. It's a no-op in the SZ_RELEASE mode.
* @brief Similar to `assert`, the `_sz_assert` is used in the `SZ_DEBUG` mode
* to check the invariants of the library. It's a no-op in the "Release" mode.
* @note If you want to catch it, put a breakpoint at @b `__GI_exit`
*/
#if SZ_DEBUG && defined(SZ_AVOID_LIBC) && !SZ_AVOID_LIBC && !defined(SZ_PIC)
#include <stdio.h> // `fprintf`
#include <stdio.h> // `fprintf`, `stderr`
#include <stdlib.h> // `EXIT_FAILURE`
SZ_PUBLIC void _sz_assert_failure(char const *condition, char const *file, int line) {
fprintf(stderr, "Assertion failed: %s, in file %s, line %d\n", condition, file, line);
Expand Down
20 changes: 10 additions & 10 deletions scripts/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <string> // Baseline
#include <string_view> // Baseline

#if !SZ_DETECT_CPP_11
#if !_SZ_IS_CPP11
#error "This test requires C++11 or later."
#endif

Expand All @@ -52,7 +52,7 @@ using sz::literals::operator""_sz;
* Instantiate all the templates to make the symbols visible and also check
* for weird compilation errors on uncommon paths.
*/
#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view
template class std::basic_string_view<char>;
#endif
template class sz::basic_string_slice<char>;
Expand Down Expand Up @@ -412,7 +412,7 @@ static void test_stl_compatibility_for_reads() {
assert(str("b") >= str("a"));
assert(str("a") < str("aa"));

#if SZ_DETECT_CPP20 && __cpp_lib_three_way_comparison
#if _SZ_IS_CPP20 && __cpp_lib_three_way_comparison
// Spaceship operator instead of conventional comparions.
assert((str("a") <=> str("b")) == std::strong_ordering::less);
assert((str("b") <=> str("a")) == std::strong_ordering::greater);
Expand Down Expand Up @@ -455,7 +455,7 @@ static void test_stl_compatibility_for_reads() {
assert(str("hello world").compare(6, 5, "worlds", 5) == 0); // Substring "world" in both strings
assert(str("hello world").compare(6, 5, "worlds", 6) < 0); // Substring "world" is less than "worlds"

#if SZ_DETECT_CPP20 && __cpp_lib_starts_ends_with
#if _SZ_IS_CPP20 && __cpp_lib_starts_ends_with
// Prefix and suffix checks against strings.
assert(str("https://cppreference.com").starts_with(str("http")) == true);
assert(str("https://cppreference.com").starts_with(str("ftp")) == false);
Expand All @@ -475,7 +475,7 @@ static void test_stl_compatibility_for_reads() {
assert(str("string_view").ends_with("View") == false);
#endif

#if SZ_DETECT_CPP_23 && __cpp_lib_string_contains
#if _SZ_IS_CPP23 && __cpp_lib_string_contains
// Checking basic substring presence.
assert(str("hello").contains(str("ell")) == true);
assert(str("hello").contains(str("oll")) == false);
Expand Down Expand Up @@ -506,7 +506,7 @@ static void test_stl_compatibility_for_reads() {
assert(std::hash<str> {}("hello") != 0);
assert_scoped(std::ostringstream os, os << str("hello"), os.str() == "hello");

#if SZ_DETECT_CPP14
#if _SZ_IS_CPP14
// Comparison function objects are a C++14 feature.
assert(std::equal_to<str> {}("hello", "world") == false);
assert(std::less<str> {}("hello", "world") == true);
Expand Down Expand Up @@ -660,7 +660,7 @@ static void test_stl_conversions() {
sz_unused(sz);
sz_unused(szv);
}
#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view
// From STL `string_view` to StringZilla and vice-versa.
{
std::string_view stl {"hello"};
Expand Down Expand Up @@ -1179,7 +1179,7 @@ static void test_search() {
assert(rsplits[4] == "");
}

#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view

/**
* Evaluates the correctness of a "matcher", searching for all the occurrences of the `needle_stl`
Expand Down Expand Up @@ -1582,7 +1582,7 @@ int main(int argc, char const **argv) {
test_replacements();

// Compatibility with STL
#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view
test_stl_compatibility_for_reads<std::string_view>();
#endif
test_stl_compatibility_for_reads<std::string>();
Expand All @@ -1607,7 +1607,7 @@ int main(int argc, char const **argv) {
test_stl_conversions();
test_comparisons();
test_search();
#if SZ_DETECT_CPP_17 && __cpp_lib_string_view
#if _SZ_IS_CPP17 && __cpp_lib_string_view
test_search_with_misaligned_repetitions();
#endif

Expand Down

0 comments on commit 19c2ae9

Please sign in to comment.