chore: String toHex should return a string not do inplace conversion

This commit is contained in:
sithlord48 2025-01-25 07:54:33 -05:00 committed by Nick Bolton
parent ac54a97a3f
commit 9b1489384c
5 changed files with 7 additions and 11 deletions

View file

@ -176,7 +176,7 @@ std::string removeFileExt(std::string filename)
return filename.substr(0, dot); return filename.substr(0, dot);
} }
void toHex(std::string &subject, int width, const char fill) std::string toHex(const std::string &subject, int width, const char fill)
{ {
std::stringstream ss; std::stringstream ss;
ss << std::hex; ss << std::hex;
@ -184,7 +184,7 @@ void toHex(std::string &subject, int width, const char fill)
ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i]; ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i];
} }
subject = ss.str(); return ss.str();
} }
void uppercase(std::string &subject) void uppercase(std::string &subject)

View file

@ -71,8 +71,9 @@ std::string removeFileExt(std::string filename);
//! Convert into hexdecimal //! Convert into hexdecimal
/*! /*!
Convert each character in \c subject into hexdecimal form with \c width Convert each character in \c subject into hexdecimal form with \c width
Return a new hexString
*/ */
void toHex(std::string &subject, int width, const char fill = '0'); std::string toHex(const std::string &subject, int width, const char fill = '0');
//! Convert to all uppercase //! Convert to all uppercase
/*! /*!

View file

@ -221,7 +221,7 @@ int SslApi::getErrorCode(int status) const
void SslApi::formatFingerprint(std::string &fingerprint) const void SslApi::formatFingerprint(std::string &fingerprint) const
{ {
// to hexidecimal // to hexidecimal
deskflow::string::toHex(fingerprint, 2); fingerprint = deskflow::string::toHex(fingerprint, 2);
// all uppercase // all uppercase
deskflow::string::uppercase(fingerprint); deskflow::string::uppercase(fingerprint);
// add colon to separate each 2 charactors // add colon to separate each 2 charactors

View file

@ -625,7 +625,7 @@ void SecureSocket::formatFingerprint(std::string &fingerprint, bool hex, bool se
{ {
if (hex) { if (hex) {
// to hexidecimal // to hexidecimal
deskflow::string::toHex(fingerprint, 2); fingerprint = deskflow::string::toHex(fingerprint, 2);
} }
// all uppercase // all uppercase

View file

@ -56,12 +56,7 @@ TEST(StringTests, sprintf_formatWithArgument_formatedString)
TEST(StringTests, toHex_plaintext_hexString) TEST(StringTests, toHex_plaintext_hexString)
{ {
std::string subject = "foobar"; EXPECT_EQ("666f6f626172", string::toHex("foobar", 2));
int width = 2;
string::toHex(subject, width);
EXPECT_EQ("666f6f626172", subject);
} }
TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput) TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput)