[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
strtod() without locale
From: |
Paavo Helde |
Subject: |
strtod() without locale |
Date: |
Tue, 25 Nov 2008 01:23:52 -0600 |
User-agent: |
Xnews/5.04.25 |
My goal is to convert numeric strings to doubles, using always period
'.' as the decimal point, regardless of what setlocale() calls other
threads might have been calling at the same time. The strtod() function
would work nicely except it is affected by locale settings. I came up
with the following (error checking and endptr stuff left out from here):
#include <string.h>
#include <sstream>
#include <locale>
double strtod_dot(const char* nptr) {
size_t n = strspn(nptr, "0123456789.eE+-");
std::istringstream is(std::string(nptr, n));
is.imbue(std::locale("C"));
double f;
is >> f;
return f;
}
This works, but it is about 5 times slower than strtod() (yes, measured
it) and makes a lot of useless work, whereas the operation should
actually be simpler and faster than strtod(). Any suggestions what could
I do better?
TIA
Paavo
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- strtod() without locale,
Paavo Helde <=