Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor static variables in OpenSim::IO #3948

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading