Skip to content

Commit 751a475

Browse files
committed
Refactor type extraction logic in Demangle for improved clarity and correctness
Optimized namespace extraction by refining `kFunc` substring logic and replacing redundant conditions. Improved handling of compiler-specific type demangling to ensure consistent and accurate results.
1 parent 68f93bb commit 751a475

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

modules/core/util/include/util.hpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
2-
#include <stdbool.h>
3-
#include <stdlib.h>
42

53
#include <cstdint>
4+
#include <cstdlib>
65
#include <string>
76
#include <string_view>
87

@@ -40,13 +39,24 @@ constexpr std::string_view GetNamespace() {
4039
constexpr std::string_view kKey = "T = ";
4140

4241
auto start = kFunc.find(kKey);
43-
if (start == std::string_view::npos) return {};
42+
if (start == std::string_view::npos) {
43+
return {};
44+
}
4445
start += kKey.size();
4546

46-
auto end = kFunc.rfind("::");
47-
if (end == std::string_view::npos || end <= start) return {};
47+
auto end = kFunc.find_first_of(";]> ,", start);
48+
if (end == std::string_view::npos) {
49+
return {};
50+
}
51+
52+
auto full_type = kFunc.substr(start, end - start);
4853

49-
return kFunc.substr(start, end - start);
54+
auto ns_end = full_type.rfind("::");
55+
if (ns_end == std::string_view::npos) {
56+
return {};
57+
}
58+
59+
return full_type.substr(0, ns_end);
5060

5161
#elif defined(_MSC_VER)
5262
constexpr std::string_view kFunc = __FUNCSIG__;
@@ -64,10 +74,15 @@ constexpr std::string_view GetNamespace() {
6474
}
6575
}
6676

67-
auto end = kFunc.rfind("::");
68-
if (end == std::string_view::npos || end <= start) return {};
77+
auto end = kFunc.find('>', start);
78+
if (end == std::string_view::npos) return {};
79+
80+
auto full_type = kFunc.substr(start, end - start);
81+
82+
auto ns_end = full_type.rfind("::");
83+
if (ns_end == std::string_view::npos) return {};
6984

70-
return kFunc.substr(start, end - start);
85+
return full_type.substr(0, ns_end);
7186

7287
#else
7388
static_assert([] { return false; }(), "Unsupported compiler");

0 commit comments

Comments
 (0)