Skip to content

Commit

Permalink
Merge pull request #3948 from opensim-org/refactor_cleanup-3943
Browse files Browse the repository at this point in the history
Refactor static variables in OpenSim::IO
  • Loading branch information
nickbianco authored Oct 23, 2024
2 parents 33f3f59 + e0c2a3c commit bef3264
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ v4.6
states belong, a date/time stamp of when the file was written, and a user-specified note. `.ostate` files also support a
configurable output precision. At the highest ouput precsion (~20 significant figures), serialization/deserialization is
a lossless process. (#3902)
- Improved `OpenSim::IO::stod` string-to-decimal parsing function by making it not-locale-dependant (#3943, #3924; thanks @alexbeattie42)

v4.5.1
======
Expand Down
39 changes: 30 additions & 9 deletions OpenSim/Common/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,28 @@ using namespace OpenSim;
using namespace std;

// STATICS
bool IO::_Scientific = false;
bool IO::_GFormatForDoubleOutput = false;
int IO::_Pad = 8;
int IO::_Precision = 8;
char IO::_DoubleFormat[] = "%16.8lf";
bool IO::_PrintOfflineDocuments = true;
const std::locale _locale = std::locale::classic();
namespace
{
constexpr int IO_DBLFMTLEN = 256;

/** Specifies whether number output is in scientific or float format. */
bool _Scientific = false;

/** Specifies whether number output is in %g format or not. */
bool _GFormatForDoubleOutput = false;

/** Specifies number of digits of padding in number output. */
int _Pad = 8;

/** Specifies the precision of number output. */
int _Precision = 8;

/** The output format string. */
char _DoubleFormat[IO_DBLFMTLEN] = "%16.8lf";

/** Whether offline documents should also be printed when Object::print is called. */
bool _PrintOfflineDocuments = true;
}


//=============================================================================
Expand Down Expand Up @@ -482,10 +497,16 @@ double IO::
stod(const std::string& __str, std::size_t* __idx)
{
std::istringstream iss(__str);
iss.imbue(_locale);

// Always parse numbers with "C" locale, which uses a period character
// for the decimal place. Otherwise, Finns, Dutch, and other locales
// that use comma characters as a decimal place will encounter parsing
// issues (#3943, #3924).
iss.imbue(std::locale::classic());

double result;
iss >> result;
if(iss.fail()){
if (iss.fail()) {
result = std::numeric_limits<double>::quiet_NaN();
log_warn("Encountered non-numeric string value: {} ; parsed value:{}",__str, result);
}
Expand Down
14 changes: 0 additions & 14 deletions OpenSim/Common/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

// DEFINES
constexpr int IO_STRLEN = 2048;
constexpr int IO_DBLFMTLEN = 256;


namespace OpenSim {
Expand All @@ -53,19 +52,6 @@ class OSIMCOMMON_API IO {
// DATA
//=============================================================================
private:
// NUMBER OUTPUT
/** Specifies whether number output is in scientific or float format. */
static bool _Scientific;
/** Specifies whether number output is in %g format or not. */
static bool _GFormatForDoubleOutput;
/** Specifies number of digits of padding in number output. */
static int _Pad;
/** Specifies the precision of number output. */
static int _Precision;
/** The output format string. */
static char _DoubleFormat[IO_DBLFMTLEN];
/** Whether offline documents should also be printed when Object::print is called. */
static bool _PrintOfflineDocuments;


//=============================================================================
Expand Down

0 comments on commit bef3264

Please sign in to comment.