danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
717 stars 163 forks source link

`std::from_chars` parsing fails tests on Windows #405

Closed cschreib-ibex closed 1 year ago

cschreib-ibex commented 1 year ago

Describe the bug

Recently, jsoncons has started using std::from_char when available. We are on Windows, with _MSC_VER == 1934, which does enable it based on the checks in compiler_support.hpp. However, std::from_chars seems unable to parse all floating point representations, as demonstrated by tests failing (e.g., "csv_parser number detection" fails).

I don't know if this is a quirk of the MSVC implementation that affects all versions, or if it is specific to our version of MSVC. But at present the std::from_char parsing does not have feature-parity with the classic strtof/strtod.

Enumerate the steps to reproduce the bug

Include a small, self-contained example if possible

#include <charconv>
#include <string>
#include <iostream>

int main() {
    const std::string s = "5001173100E95978";
    double d = 0.0;
    auto res = std::from_chars(s.data(), s.data() + s.size(), d);
    if (res.ec != std::errc())
    {
        std::cout << "from_chars: convert chars to double failed" << std::endl;
    }
    else
    {
        std::cout << "from_chars: " << d << std::endl;
    }

    char* ptr = nullptr;
    d = std::strtod(s.data(), &ptr);
    if (ptr == s.data())
    {
        std::cout << "strtod: convert chars to double failed" << std::endl;
    }
    else
    {
        std::cout << "strtod: " << d << std::endl;
    }

    return 0;
}

This code demonstrates the inconsistency. Outputs:

GCC 12.2 (compiler explorer)

from_chars: convert chars to double failed
strtod: inf

MSVC 19.33 (compiler explorer)

from_chars: convert chars to double failed
strtod: inf

What compiler, architecture, and operating system?

What jsoncons library version?

cschreib-ibex commented 1 year ago

"Fix" provided in https://github.com/danielaparker/jsoncons/pull/406. I simply disabled the automatic detection for from_chars, such that it is disabled by default. Not knowing better which exact compiler/STL version works or not, I figured it was a simpler first step.

danielaparker commented 1 year ago

Thanks. We can revert for now.

danielaparker commented 1 year ago

Fix is in 0.170.0