-
Notifications
You must be signed in to change notification settings - Fork 301
Description
Having tried DoubleToStringConverter::EcmaScriptConverter().ToShortest(value, &builder) for quite a few floating point values, it appears to me that the length of the built string is always below 26 characters. (While some values do actually get represented by a string of length 25, excluding terminating null-character.) Is that a correct observation? Examples:
"-1.7976931348623157e+308" (-DBL_MAX, string length 24)
"-0.0000033333333333333333" (-3e-005/9.0, string length 25)
Is this maximum string length already documented somewhere? And could this number be added to the double-conversion library API as a named constant? For example as a public member of DoubleToStringConverter:
static const int kMaxCharsShortestStringEcmaScript = 26;
I think it would be very helpful to have such a constant available for users, as I guess EcmaScriptConverter is quite popular. And a user of ToShortest typically likes to know how "short" that string could possibly be!
Typical use case:
char userBuffer[ DoubleToStringConverter::kMaxCharsShortestStringEcmaScript ];
StringBuilder builder(userBuffer, sizeof(userBuffer));
DoubleToStringConverter::EcmaScriptConverter().ToShortest(value, &builder);
Note that in the ITK library, we currently just use a buffer size of 32, because such a named constant is not yet available: https://github.com/InsightSoftwareConsortium/ITK/blob/c00531fe23cea71f61685e50e4b6503ab8f1ae82/Modules/Core/Common/src/itkNumberToString.cxx#L51 (From pull request InsightSoftwareConsortium/ITK#2256) Which seems suboptimal to me.