Skip to content

Commit 2c148ef

Browse files
authored
Make it work with c++17 (#3)
Fixes #2 , make astr compile and test with C++17
1 parent e897af0 commit 2c148ef

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

CMakeLists.txt

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11

22
cmake_minimum_required(VERSION 3.27)
33

4-
project(astr VERSION 0.2.1 LANGUAGES CXX)
4+
project(astr VERSION 0.3.0 LANGUAGES CXX)
55

66
if(PROJECT_IS_TOP_LEVEL)
77
# make git ignore the build directory
88
file(WRITE ${CMAKE_BINARY_DIR}/.gitignore "*")
9-
set(CMAKE_CXX_STANDARD 20)
10-
set(CMAKE_CXX_EXTENSIONS OFF)
11-
SET(CMAKE_COMPILE_WARNING_AS_ERROR ON) # turn off with --compile-no-warning-as-error
129
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
13-
1410
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
1511
include(setup_compiler)
1612
include(default_flags)

cmake/setup_compiler.cmake

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ endif(NOT isMultiConfig)
2323

2424
# Off, we do not want to annoy potential packager
2525
# option(WARN_ERROR "Thread warnings as errors. Default OFF" OFF)
26-
27-
set(CMAKE_CXX_STANDARD 20)
26+
set(CMAKE_CXX_STANDARD 17)
27+
set(CXX_STANDARD_REQUIRED ON)
2828
set(CMAKE_CXX_EXTENSIONS OFF)
2929
SET(CMAKE_COMPILE_WARNING_AS_ERROR ON) # turn off with --compile-no-warning-as-error
3030

31+
3132
if(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
3233
# https://stackoverflow.com/questions/77153800/xcode-15-c-compilation-errors
3334
# TODO ...

include/a4z/astr.hpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ namespace a4z {
5050
"Wanted sized T cannot be greater than actual size N");
5151
astr<T + 1> result;
5252
constexpr std::size_t start = N - T - 1; // N > T is asserted above
53-
std::copy(data + start, data + N, result.data);
53+
// cpp20, not yet
54+
// std::copy(data + start, data + N, result.data);
55+
for (size_t i = start; i < N; ++i) {
56+
result.data[i - start] = data[i];
57+
}
5458
return result;
5559
}
5660

@@ -60,7 +64,11 @@ namespace a4z {
6064
"Wanted sized T cannot be greater than actual size N");
6165
astr<T + 1> result;
6266
constexpr std::size_t last = T;
63-
std::copy(data, data + last, result.data);
67+
// cpp20, not yet
68+
// std::copy(data, data + last, result.data);
69+
for (size_t i = 0; i < last; ++i) {
70+
result.data[i] = data[i];
71+
}
6472
return result;
6573
}
6674
};
@@ -149,7 +157,7 @@ namespace a4z {
149157
return npos; // Not found
150158
}
151159

152-
consteval bool on_windows() {
160+
constexpr bool on_windows() {
153161
#ifdef _WIN32
154162
return true;
155163
#else

include/a4z/filename.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace a4z {
1414
constexpr char slash = on_windows() ? '\\' : '/';
1515

1616
#define a4z_file_name \
17-
[]() consteval { \
17+
[]() constexpr { \
1818
constexpr const char* str = __FILE__; \
1919
constexpr auto len = a4z::cstr_len(str); \
2020
constexpr auto start{a4z::find_nth_r_occurrence(__FILE__, a4z::slash, 2)}; \
2121
static_assert(start != a4z::npos); \
2222
constexpr std::size_t astr_len = len - start; \
23-
char data[astr_len]; \
23+
char data[astr_len] = {0}; \
2424
for (std::size_t i = 0; i < astr_len; ++i) { \
2525
data[i] = *(str + start + i + 1); \
2626
} \

tests/base/filename_test.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,22 @@ SCENARIO("Test with filename macro") {
8282
constexpr auto fn = a4z_file_name();
8383

8484
WHEN("checking if it starts with base") {
85-
const auto starts_with_base = std::string(fn.c_str()).starts_with("base");
85+
// cpp20
86+
// const auto starts_with_base =
87+
// std::string(fn.c_str()).starts_with("base");
88+
const char* base = "base";
89+
auto start_str = fn.first_n<4>();
90+
const auto starts_with_base = a4z::equal(base, start_str.c_str());
8691
THEN("we find it at the expected position") {
8792
CHECK(starts_with_base);
8893
}
8994
}
9095
AND_WHEN("checking if it ends with ") {
91-
const auto ends_with_filename =
92-
std::string(fn.c_str()).ends_with("filename_test.cpp");
96+
const char* filename = "filename_test.cpp";
97+
auto end_str = fn.last_n<17>();
98+
const auto ends_with_filename = a4z::equal(filename, end_str.c_str());
99+
// cpp20
100+
// std::string(fn.c_str()).ends_with("filename_test.cpp");
93101
THEN("we find it at the expected position") {
94102
CHECK(ends_with_filename);
95103
}

0 commit comments

Comments
 (0)