Skip to content
Snippets Groups Projects
Commit 3282040c authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Convert demangle function to string_view to reduce string copy and rewrite to avoid using a loop

parent 1f6df523
No related branches found
No related tags found
No related merge requests found
......@@ -58,8 +58,8 @@ class LLContextStrings
class LLScopedContextString
{
public:
LLScopedContextString(const std::string& str):
m_str(str)
LLScopedContextString(const std::string str):
m_str(std::move(str))
{
LLContextStrings::addContextString(m_str);
}
......
......@@ -44,6 +44,7 @@
# include <io.h>
#endif // !LL_WINDOWS
#include <vector>
#include <string_view>
#include "string.h"
#include <absl/container/flat_hash_map.h>
......@@ -316,7 +317,7 @@ namespace
namespace LLError
{
std::string Log::demangle(const char* mangled)
std::string Log::demangle(const std::string_view mangled)
{
#ifdef __GNUC__
// GCC: type_info::name() returns a mangled class name,st demangle
......@@ -329,21 +330,25 @@ namespace LLError
return result;
#elif LL_WINDOWS
using namespace std::literals;
// Visual Studio: type_info::name() includes the text "class " at the start
std::string name = mangled;
for (const auto& prefix : std::vector<std::string>{ "class ", "struct " })
static constexpr auto class_prefix = "class "sv;
static constexpr auto struct_prefix = "struct "sv;
if (0 == mangled.compare(0, class_prefix.length(), class_prefix))
{
if (0 == name.compare(0, prefix.length(), prefix))
{
return name.substr(prefix.length());
}
return std::string(mangled.substr(class_prefix.length()));
}
else if (0 == mangled.compare(0, struct_prefix.length(), struct_prefix))
{
return std::string(mangled.substr(struct_prefix.length()));
}
// huh, that's odd, we should see one or the other prefix -- but don't
// try to log unless logging is already initialized
// in Python, " or ".join(vector) -- but in C++, a PITB
LL_DEBUGS() << "Did not see 'class' or 'struct' prefix on '"
<< name << "'" << LL_ENDL;
return name;
<< mangled << "'" << LL_ENDL;
return std::string(mangled);
#else // neither GCC nor Visual Studio
return mangled;
......
......@@ -203,7 +203,7 @@ namespace LLError
static std::ostringstream* out();
static void flush(std::ostringstream* out, char* message);
static void flush(std::ostringstream*, const CallSite&);
static std::string demangle(const char* mangled);
static std::string demangle(const std::string_view mangled);
/// classname<TYPE>()
template <typename T>
static std::string classname() { return demangle(typeid(T).name()); }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment