Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Mar 1, 2020
1 parent a5aca5c commit d135f35
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions benchmarks/microbenchmarks/double_to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,32 @@ class number_serialiser

class number_serialiser_mk2
{
bool should_convert_to_comma;
static constexpr int Excel_Digit_Precision = 15; //sf
bool should_convert_comma;

void convert_comma(char *buf, int len)
{
char *buf_end = buf + len;
char *decimal = std::find(buf, buf_end, ',');
if (decimal != buf_end)
{
*decimal = '.';
}
}

public:
explicit number_serialiser_mk2()
: should_convert_to_comma(std::use_facet<std::numpunct<char>>(std::locale{}).decimal_point() == ',')
: should_convert_comma(std::use_facet<std::numpunct<char>>(std::locale{}).decimal_point() == ',')
{
}

std::string serialise(double d)
{
char buf[16];
char buf[Excel_Digit_Precision + 1]; // need space for trailing '\0'
int len = snprintf(buf, sizeof(buf), "%16f", d);
if (should_convert_to_comma)
if (should_convert_comma)
{
auto decimal = std::find(buf, buf + len, ',');
if (decimal != buf + len)
{
*decimal = '.';
}
convert_comma(buf, len);
}
return std::string(buf, len);
}
Expand Down

0 comments on commit d135f35

Please sign in to comment.