Skip to content

Commit 4fbc0a2

Browse files
committed
commandlineflags: Replace strtod by std::stringstream
Using std::stringstream allows conversion of double to string independent of the current locale setting. Signed-off-by: Stefan Weil <[email protected]>
1 parent d047fa1 commit 4fbc0a2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/training/commandlineflags.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// See the License for the specific language governing permissions and
99
// limitations under the License.
1010

11+
#include <cmath> // for std::isnan, NAN
12+
#include <locale> // for std::locale::classic
13+
#include <sstream> // for std::stringstream
1114
#include "baseapi.h" // TessBaseAPI::Version
1215
#include "commandlineflags.h"
1316
#include "errcode.h"
@@ -106,9 +109,17 @@ static bool SafeAtoi(const char* str, int* val) {
106109
}
107110

108111
static bool SafeAtod(const char* str, double* val) {
109-
char* endptr = nullptr;
110-
*val = strtod(str, &endptr);
111-
return endptr != nullptr && *endptr == '\0';
112+
double d = NAN;
113+
std::stringstream stream(str);
114+
// Use "C" locale for reading double value.
115+
stream.imbue(std::locale::classic());
116+
stream >> d;
117+
*val = 0;
118+
bool success = !std::isnan(d);
119+
if (success) {
120+
*val = d;
121+
}
122+
return success;
112123
}
113124

114125
static void PrintCommandLineFlags() {

0 commit comments

Comments
 (0)