str2hex to work in std::string instead of malloc

Reimplement a test function using nicer C++ types. No functional change here.
This commit is contained in:
ridiculousfish 2022-04-09 15:19:18 -07:00
parent 031b26584b
commit cb027bfdc0

View File

@ -495,14 +495,13 @@ static void test_format() {
} }
/// Helper to convert a narrow string to a sequence of hex digits. /// Helper to convert a narrow string to a sequence of hex digits.
static char *str2hex(const char *input) { static std::string str2hex(const std::string &input) {
char *output = (char *)malloc(5 * std::strlen(input) + 1); std::string output;
char *p = output; for (char c : input) {
for (; *input; input++) { char buff[16];
sprintf(p, "0x%02X ", (int)*input & 0xFF); size_t amt = snprintf(buff, sizeof buff, "0x%02X ", (int)(c & 0xFF));
p += 5; output.append(buff, amt);
} }
*p = '\0';
return output; return output;
} }
@ -520,13 +519,9 @@ static void test_convert() {
const wcstring w = str2wcstring(orig); const wcstring w = str2wcstring(orig);
std::string n = wcs2string(w); std::string n = wcs2string(w);
if (orig != n) { if (orig != n) {
char *o2 = str2hex(orig.c_str());
char *n2 = str2hex(n.c_str());
err(L"Line %d - %d: Conversion cycle of string:\n%4d chars: %s\n" err(L"Line %d - %d: Conversion cycle of string:\n%4d chars: %s\n"
L"produced different string:\n%4d chars: %s", L"produced different string:\n%4d chars: %s",
__LINE__, i, orig.size(), o2, n.size(), n2); __LINE__, i, orig.size(), str2hex(orig).c_str(), n.size(), str2hex(n).c_str());
free(o2);
free(n2);
} }
} }
} }