Skip to content

Commit

Permalink
perf(skymp5-server): replace stringstream with standard C I/O functio…
Browse files Browse the repository at this point in the history
…ns (#1672)
  • Loading branch information
barkinlove authored Sep 11, 2023
1 parent b15a885 commit b33abd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
51 changes: 39 additions & 12 deletions skymp5-server/cpp/server_guest_lib/FormDesc.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
#include "FormDesc.h"
#include <sstream>
#include <cstdio>

std::string FormDesc::ToString(char delimiter) const
{
std::stringstream ss;
ss << std::hex << shortFormId;
if (!file.empty())
ss << delimiter << file;
return ss.str();
auto fullFmt = "%0x%c%s";
auto idFmt = "%0x";
size_t size = !file.empty()
? std::snprintf(nullptr, 0, fullFmt, shortFormId, delimiter, file.c_str())
: std::snprintf(nullptr, 0, idFmt, shortFormId);

std::string buffer;
buffer.resize(size);

if (!file.empty()) {
std::sprintf(buffer.data(), fullFmt, shortFormId, delimiter, file.c_str());
} else {
std::sprintf(buffer.data(), idFmt, shortFormId);
}
return buffer;
}

FormDesc FormDesc::FromString(std::string str, char delimiter)
{
for (auto& ch : str)
if (ch == delimiter)
ch = ' ';

std::istringstream ss(str);
FormDesc res;
ss >> std::hex >> res.shortFormId >> res.file;
std::string id, file;

if (str.find(delimiter) == std::string::npos) {
std::sscanf(str.data(), "%x", &res.shortFormId);
return res;
}

for (auto it = str.begin(); it != str.end(); ++it) {
if (*it == delimiter) {
id = { str.begin(), it };
res.file = { it + 1, str.end() };
break;
}
}

std::sscanf(id.data(), "%x", &res.shortFormId);
return res;
}

Expand Down Expand Up @@ -75,3 +95,10 @@ FormDesc FormDesc::FromFormId(uint32_t formId,
}
return res;
}

static const FormDesc kTamriel = FormDesc::FromString("3c:Skyrim.esm");

FormDesc FormDesc::Tamriel()
{
return kTamriel;
}
2 changes: 1 addition & 1 deletion skymp5-server/cpp/server_guest_lib/FormDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FormDesc
std::make_tuple(right.shortFormId, right.file);
}

static FormDesc Tamriel() { return FromString("3c:Skyrim.esm"); }
static FormDesc Tamriel();

uint32_t shortFormId = 0;
std::string file;
Expand Down

0 comments on commit b33abd2

Please sign in to comment.