Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake ninja-build clang-format
uses: actions/checkout@v7

- name: Configure CMake
run: cmake --preset ninja
Expand All @@ -35,19 +30,13 @@ jobs:
- os: ubuntu-latest
cmake_preset: ninja
- os: windows-latest
cmake_preset: msvc22
cmake_preset: msvc26
- os: macos-latest
cmake_preset: xcode

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies on Ubuntu
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y cmake ninja-build
uses: actions/checkout@v7

- name: Configure CMake
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.vscode
.idea
.cache
compile_commands.json
2 changes: 1 addition & 1 deletion cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (FETCH_DOCTEST)
FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/onqtam/doctest
GIT_TAG v2.4.12
GIT_TAG v2.5.2
OVERRIDE_FIND_PACKAGE
)
endif()
Expand Down
18 changes: 11 additions & 7 deletions include/a4z/filename.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ namespace a4z {

constexpr char slash = on_windows() && !using_clang() ? '\\' : '/';

#define a4z_file_name \
#define a4z_file_name_from(filename) \
[]() constexpr { \
constexpr const char* str = __FILE__; \
constexpr const char* str = filename; \
constexpr auto len = a4z::cstr_len(str); \
constexpr auto start{a4z::find_nth_r_occurrence(__FILE__, a4z::slash, 2)}; \
static_assert(start != a4z::npos); \
constexpr std::size_t astr_len = len - start; \
constexpr auto start{a4z::find_nth_r_occurrence(filename, a4z::slash, 2)}; \
constexpr std::size_t offset = (start == a4z::npos ? 0 : start); \
constexpr std::size_t offset_add = (start == a4z::npos ? 0 : 1); \
constexpr std::size_t astr_len = \
(start == a4z::npos ? len + 1 : len - start); \
char data[astr_len] = {0}; \
for (std::size_t i = 0; i < astr_len; ++i) { \
data[i] = *(str + start + i + 1); \
data[i] = *(str + offset + offset_add + i); \
} \
return a4z::astr<astr_len>{data}; \
}
}()

#define a4z_file_name() a4z_file_name_from(__FILE__)

} // namespace a4z

Expand Down
28 changes: 28 additions & 0 deletions tests/base/filename_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ SCENARIO("Truncate a astr") {
}
}

SCENARIO("Test a4z_file_name_from with path delimiter") {
GIVEN("a path string with two delimiters") {
using a4z::slash;
static constexpr char path[] = {'a', slash, 'b', slash, 'c', '\0'};
constexpr auto fn = a4z_file_name_from(path);
WHEN("requesting the result") {
static constexpr char expected[] = {'b', a4z::slash, 'c', '\0'};
const auto matches = a4z::equal(expected, fn.c_str());
THEN("it contains the segment after the second-to-last delimiter") {
CHECK(matches);
}
}
}
}

SCENARIO("Test a4z_file_name_from without path delimiter") {
GIVEN("a path string with no delimiter") {
static constexpr char path[] = {'a', 'b', 'c', '\0'};
constexpr auto fn = a4z_file_name_from(path);
WHEN("requesting the result") {
const auto matches = a4z::equal("abc", fn.c_str());
THEN("it returns the whole string") {
CHECK(matches);
}
}
}
}

SCENARIO("Test with filename macro") {
GIVEN("the current filename") {
constexpr auto fn = a4z_file_name();
Expand Down