Skip to content

Commit

Permalink
improve performance of parseHexValue
Browse files Browse the repository at this point in the history
- use a static regex
- don't use a stringstream to parse
  • Loading branch information
tomjnixon authored and benjamin-weiss committed Aug 14, 2019
1 parent a54e15e commit d5f708f
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/detail/hex_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
#include <iomanip>
#include <regex>
#include <sstream>
#include <string>

namespace adm {
namespace detail {

unsigned int parseHexValue(const std::string& value,
unsigned int nmbOfChars) {
std::stringstream regexString;
regexString << "[0-9a-fA-F]{1," << nmbOfChars << "}";
const std::regex r(regexString.str());
if (std::regex_match(value, r)) {
unsigned int ret;
std::stringstream ss;
ss << std::hex << value;
ss >> ret;
return static_cast<unsigned int>(ret);
const static std::regex r("[0-9a-fA-F]+");

if (std::regex_match(value, r) && value.size() <= nmbOfChars) {
return static_cast<unsigned int>(std::stol(value, nullptr, 16));
} else {
std::stringstream errorString;
errorString << "failed to convert hex value to int: " << value;
Expand Down

0 comments on commit d5f708f

Please sign in to comment.