From a1ffcd3654739dbd4da208972515e47639f1f117 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 14 Apr 2019 13:01:07 +0200 Subject: [PATCH] Use std::stringstream for add_str_double Using std::stringstream allows conversion of double to string independent of the current locale setting. Signed-off-by: Stefan Weil --- src/ccutil/strngs.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ccutil/strngs.cpp b/src/ccutil/strngs.cpp index 1da96f6e..f2326533 100644 --- a/src/ccutil/strngs.cpp +++ b/src/ccutil/strngs.cpp @@ -2,7 +2,6 @@ * File: strngs.cpp (Formerly strings.c) * Description: STRING class functions. * Author: Ray Smith - * Created: Fri Feb 15 09:13:30 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,6 +19,8 @@ #include "strngs.h" #include // for assert #include // for malloc, free +#include // for std::locale::classic +#include // for std::stringstream #include "errcode.h" // for ASSERT_HOST #include "genericvector.h" // for GenericVector #include "helpers.h" // for ReverseN @@ -389,11 +390,13 @@ void STRING::add_str_int(const char* str, int number) { void STRING::add_str_double(const char* str, double number) { if (str != nullptr) *this += str; - // Allow space for the maximum possible length of %8g. - char num_buffer[kMaxDoubleSize]; - snprintf(num_buffer, kMaxDoubleSize - 1, "%.8g", number); - num_buffer[kMaxDoubleSize - 1] = '\0'; - *this += num_buffer; + std::stringstream stream; + // Use "C" locale (needed for double value). + stream.imbue(std::locale::classic()); + // Use 8 digits for double value. + stream.precision(8); + stream << number; + *this += stream.str().c_str(); } STRING & STRING::operator=(const char* cstr) {