Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String conversion perf improvements + minor unrelated changes #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions src/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,16 @@ bool Guid::isValid() const
// convert to string using std::snprintf() and std::string
std::string Guid::str() const
{
char one[10], two[6], three[6], four[6], five[14];

snprintf(one, 10, "%02x%02x%02x%02x",
_bytes[0], _bytes[1], _bytes[2], _bytes[3]);
snprintf(two, 6, "%02x%02x",
_bytes[4], _bytes[5]);
snprintf(three, 6, "%02x%02x",
_bytes[6], _bytes[7]);
snprintf(four, 6, "%02x%02x",
_bytes[8], _bytes[9]);
snprintf(five, 14, "%02x%02x%02x%02x%02x%02x",
constexpr size_t guidBufferSize = sizeof("01234567-0123-0123-0123-0123456789ab");
char buffer[guidBufferSize];
snprintf(buffer, guidBufferSize, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
_bytes[0], _bytes[1], _bytes[2], _bytes[3],
_bytes[4], _bytes[5],
_bytes[6], _bytes[7],
_bytes[8], _bytes[9],
_bytes[10], _bytes[11], _bytes[12], _bytes[13], _bytes[14], _bytes[15]);
const std::string sep("-");
std::string out(one);

out += sep + two;
out += sep + three;
out += sep + four;
out += sep + five;

return out;
return std::string(buffer, guidBufferSize - 1);
}

// conversion operator for std::string
Expand All @@ -157,7 +146,7 @@ Guid::Guid(std::array<unsigned char, 16> &&bytes) : _bytes(std::move(bytes))
{ }

// converts a single hex char to a number (0 - 15)
unsigned char hexDigitToChar(char ch)
static unsigned char hexDigitToChar(char ch)
{
// 0-9
if (ch > 47 && ch < 58)
Expand All @@ -174,7 +163,7 @@ unsigned char hexDigitToChar(char ch)
return 0;
}

bool isValidHexChar(char ch)
static bool isValidHexChar(char ch)
{
// 0-9
if (ch > 47 && ch < 58)
Expand All @@ -192,7 +181,7 @@ bool isValidHexChar(char ch)
}

// converts the two hexadecimal characters to an unsigned char (a byte)
unsigned char hexPairToChar(char a, char b)
static unsigned char hexPairToChar(char a, char b)
{
return hexDigitToChar(a) * 16 + hexDigitToChar(b);
}
Expand Down Expand Up @@ -315,7 +304,7 @@ Guid newGuid()
Guid newGuid()
{
GUID newId;
CoCreateGuid(&newId);
(void)CoCreateGuid(&newId);

std::array<unsigned char, 16> bytes =
{
Expand Down