Porytiles
Loading...
Searching...
No Matches
source_locations.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <string_view>
5
6namespace porytiles2 {
7
8std::string extract_function_name(const std::source_location &location)
9{
10 std::string_view full_function_name = location.function_name();
11
12 // Handle empty input
13 if (full_function_name.empty()) {
14 return "";
15 }
16
17 // Find the opening parenthesis (if it exists, we have a GCC-style signature)
18 const auto paren_pos = full_function_name.find('(');
19 std::string_view qualified_name;
20
21 if (paren_pos != std::string_view::npos) {
22 // GCC-style: "return_type namespace::class::function(params) qualifiers"
23 // We need to extract "namespace::class::function"
24 qualified_name = full_function_name.substr(0, paren_pos);
25
26 // Trim trailing whitespace
27 while (!qualified_name.empty() && std::isspace(qualified_name.back())) {
28 qualified_name.remove_suffix(1);
29 }
30
31 // Now we need to find where the qualified name starts (after the return type)
32 // Look for the last space before the qualified name
33 const auto last_space = qualified_name.find_last_of(' ');
34 if (last_space != std::string_view::npos) {
35 qualified_name = qualified_name.substr(last_space + 1);
36 }
37 }
38 else {
39 // Clang-style: "namespace::class::function"
40 qualified_name = full_function_name;
41
42 // Trim any trailing whitespace
43 while (!qualified_name.empty() && std::isspace(qualified_name.back())) {
44 qualified_name.remove_suffix(1);
45 }
46 }
47
48 // Now extract the simple name (everything after the last ::)
49 const auto last_colon = qualified_name.rfind("::");
50 if (last_colon != std::string_view::npos) {
51 qualified_name = qualified_name.substr(last_colon + 2);
52 }
53
54 return std::string{qualified_name};
55}
56
57} // namespace porytiles2
std::string extract_function_name(const std::source_location &location=std::source_location::current())
Extracts the function name from a source location.