Skip to content
Snippets Groups Projects
Commit 350c1c05 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Allow demangling inside sentences

parent a1323ca2
No related branches found
No related tags found
No related merge requests found
...@@ -2,18 +2,46 @@ ...@@ -2,18 +2,46 @@
#include <cxxabi.h> #include <cxxabi.h>
#include <memory> #include <memory>
#include <sstream>
#warning remove
#include <iostream>
#include <rang.hpp>
std::string std::string
demangle(const std::string_view mangled) demangle(const std::string_view mangled)
{ {
std::string_view separators = " \t\r\n:";
std::stringstream demangled;
std::string_view string_data = mangled;
while (not string_data.empty()) {
auto begin = string_data.find_first_not_of(separators);
if (begin != 0) {
auto space = string_data.substr(0, begin);
demangled << space;
string_data.remove_prefix(begin);
begin = 0;
}
auto end = string_data.find_first_of(separators);
auto token = string_data.substr(0, end);
int status = -1; int status = -1;
char* cxa_demangled = abi::__cxa_demangle(mangled.data(), NULL, NULL, &status); char* cxa_demangled = abi::__cxa_demangle(std::string{token}.data(), nullptr, nullptr, &status);
if (status == 0) { if (status == 0) {
std::string demangled{cxa_demangled}; demangled << cxa_demangled;
free(cxa_demangled); free(cxa_demangled);
return demangled;
} else { } else {
return std::string{mangled}; demangled << token;
} }
if (end != std::string_view::npos) {
string_data.remove_prefix(end);
} else {
string_data = {};
}
}
return demangled.str();
} }
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp> #include <catch2/matchers/catch_matchers_all.hpp>
#include <algebra/TinyVector.hpp>
#include <utils/Demangle.hpp> #include <utils/Demangle.hpp>
#include <cxxabi.h> #include <cxxabi.h>
#include <sstream>
#warning remove
#include <iostream>
// clazy:excludeall=non-pod-global-static // clazy:excludeall=non-pod-global-static
...@@ -35,4 +40,17 @@ TEST_CASE("Demangle", "[utils]") ...@@ -35,4 +40,17 @@ TEST_CASE("Demangle", "[utils]")
REQUIRE((std::string{"not_mangled"} == demangle("not_mangled"))); REQUIRE((std::string{"not_mangled"} == demangle("not_mangled")));
} }
SECTION("demangle phrase")
{
std::stringstream mangled;
std::stringstream demangled;
mangled << " Sentence contains mangled text " << std::string{typeid(std::string).name()} << " and "
<< std::string{typeid(TinyVector<3>).name()} << " .";
demangled << " Sentence contains mangled text " << demangle(std::string{typeid(std::string).name()}) << " and "
<< demangle(std::string{typeid(TinyVector<3>).name()}) << " .";
REQUIRE((demangled.str() == demangle(mangled.str())));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment