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

Add vstrtonum util as replacement for atof/strtod, etc. #19309

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

markcmiller86
Copy link
Member

@markcmiller86 markcmiller86 commented Feb 14, 2024

Description

This is a draft PR for developers to see where I am headed with this. It defines vstrtonum<sometype>() with some special sauce to handle things like default values, error checking, range checking, etc. Read the comment in StringHelpers.h for an overview...

// ****************************************************************************
// Function: vstrtonum
//
// Purpose: Replacement for strtoX() and atoX() methods.
//
// Instead of any of these kinds of uses...
//
// int k = atoi(numstr);
// float f1 = atof(numstr);
//
// unsigned u = (unsigned) strtoul(numstr, 0);
// if (errno != 0) u = 0xFFFFFFFF; // set to default val
//
// float f2 = (float) strtod(numstr, 0);
// if (errno != 0) {
// f2 = -1.0; // set to default value
// debug5 << numstr << " bad value" << endl; // log error
// }
//
// ...do this...
//
// int k = vstrtonum<int>(numstr);
// float f1 = vstrtonum<float>(numstr);
//
// unsigned u = vstrtonum<unsigned>(numstr, 0xFFFFFFFF);
// float f = vstrtonum<float>(numstr, -1.0, debug5);
//
// Templatized methods to convert strings to language-native typed
// numeric values, perform some minimal error checking and optionally
// emit error messages with potentially useful context when errors
// are encountered.
//
// This should always be used in place of strtoX() or atoX() when
// reading ascii numerical data.
//
// We do a minimal amount of error checking for a signed conversion
// by checking if first non-whitespace character is a minus sign and
// artificially setting errno to EDOM (not something strtoX/atoX
// would ever do). We could add more error checking for different
// cases too by, for example, checking value read for int type and
// seeing if it is too big to fit in an int. We currently do not
// do this but it would be easy to add. We could also easily add
// logic for other, less frequently used types such as shorts or
// maybe int64_t, etc.
//
// The default method treats all ascii as long double for the strtoX
// conversion and then casts it to correct type. We specialize some
// cases for slightly better behavior.
//
// I ran performance tests on macOS doing 1 million conversions with
// these methods (including error checking) and 1 million directly
// with strtoX and atoX methods and observed no significant diffs
// in performance. In addition, keep in mind that these methods are
// typically being used in conjunction with file I/O, which almost
// certainly dominates performance. The only time this might not be
// true is for memory resident "files", mmaps, and/or SSDs.
//
// At some point, it would make sense to enhance this to use locale
// so we can handle different character encodings as well as regional
// specific interpretations (e.g. European 1.234.456,89). The idea
// would be to set the locale VisIt is using (a pref. maybe) and then
// these methods would just use that locale in calls to strtoX. That
// could be achieved globally in VisIt with a call to setlocale().
// However, when in the United States reading an ascii data file
// formatted for human readability in Germany the desire would be
// to specify "de_DE" for the locale during the read of just that
// file suggesting something more complicated than just a global
// setting.

@markcmiller86 markcmiller86 marked this pull request as draft February 14, 2024 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant