Skip to content

Commit bef3264

Browse files
authored
Merge pull request #3948 from opensim-org/refactor_cleanup-3943
Refactor static variables in OpenSim::IO
2 parents 33f3f59 + e0c2a3c commit bef3264

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ v4.6
4141
states belong, a date/time stamp of when the file was written, and a user-specified note. `.ostate` files also support a
4242
configurable output precision. At the highest ouput precsion (~20 significant figures), serialization/deserialization is
4343
a lossless process. (#3902)
44+
- Improved `OpenSim::IO::stod` string-to-decimal parsing function by making it not-locale-dependant (#3943, #3924; thanks @alexbeattie42)
4445

4546
v4.5.1
4647
======

OpenSim/Common/IO.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,28 @@ using namespace OpenSim;
6060
using namespace std;
6161

6262
// STATICS
63-
bool IO::_Scientific = false;
64-
bool IO::_GFormatForDoubleOutput = false;
65-
int IO::_Pad = 8;
66-
int IO::_Precision = 8;
67-
char IO::_DoubleFormat[] = "%16.8lf";
68-
bool IO::_PrintOfflineDocuments = true;
69-
const std::locale _locale = std::locale::classic();
63+
namespace
64+
{
65+
constexpr int IO_DBLFMTLEN = 256;
66+
67+
/** Specifies whether number output is in scientific or float format. */
68+
bool _Scientific = false;
69+
70+
/** Specifies whether number output is in %g format or not. */
71+
bool _GFormatForDoubleOutput = false;
72+
73+
/** Specifies number of digits of padding in number output. */
74+
int _Pad = 8;
75+
76+
/** Specifies the precision of number output. */
77+
int _Precision = 8;
78+
79+
/** The output format string. */
80+
char _DoubleFormat[IO_DBLFMTLEN] = "%16.8lf";
81+
82+
/** Whether offline documents should also be printed when Object::print is called. */
83+
bool _PrintOfflineDocuments = true;
84+
}
7085

7186

7287
//=============================================================================
@@ -482,10 +497,16 @@ double IO::
482497
stod(const std::string& __str, std::size_t* __idx)
483498
{
484499
std::istringstream iss(__str);
485-
iss.imbue(_locale);
500+
501+
// Always parse numbers with "C" locale, which uses a period character
502+
// for the decimal place. Otherwise, Finns, Dutch, and other locales
503+
// that use comma characters as a decimal place will encounter parsing
504+
// issues (#3943, #3924).
505+
iss.imbue(std::locale::classic());
506+
486507
double result;
487508
iss >> result;
488-
if(iss.fail()){
509+
if (iss.fail()) {
489510
result = std::numeric_limits<double>::quiet_NaN();
490511
log_warn("Encountered non-numeric string value: {} ; parsed value:{}",__str, result);
491512
}

OpenSim/Common/IO.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
// DEFINES
3737
constexpr int IO_STRLEN = 2048;
38-
constexpr int IO_DBLFMTLEN = 256;
3938

4039

4140
namespace OpenSim {
@@ -53,19 +52,6 @@ class OSIMCOMMON_API IO {
5352
// DATA
5453
//=============================================================================
5554
private:
56-
// NUMBER OUTPUT
57-
/** Specifies whether number output is in scientific or float format. */
58-
static bool _Scientific;
59-
/** Specifies whether number output is in %g format or not. */
60-
static bool _GFormatForDoubleOutput;
61-
/** Specifies number of digits of padding in number output. */
62-
static int _Pad;
63-
/** Specifies the precision of number output. */
64-
static int _Precision;
65-
/** The output format string. */
66-
static char _DoubleFormat[IO_DBLFMTLEN];
67-
/** Whether offline documents should also be printed when Object::print is called. */
68-
static bool _PrintOfflineDocuments;
6955

7056

7157
//=============================================================================

0 commit comments

Comments
 (0)