Skip to content

Maximum string length DoubleToStringConverter::EcmaScriptConverter() ToShortest? 26 chars? #146

@N-Dekker

Description

@N-Dekker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions