Skip to content

Commit

Permalink
use double-conversion from google instead of locale depend functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushistov committed Apr 27, 2016
1 parent 2ba84bc commit 115fc76
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "double-conversion"]
path = double-conversion
url = https://github.com/google/double-conversion
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ project(json11 VERSION 1.0.0 LANGUAGES CXX)

enable_testing()

add_subdirectory(double-conversion)
include_directories(double-conversion)
add_library(json11 json11.cpp)
target_include_directories(json11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(json11
PUBLIC -std=c++11
PRIVATE -fno-rtti -fno-exceptions -Wall -Wextra -Werror)
target_link_libraries(json11 double-conversion)
configure_file("json11.pc.in" "json11.pc" @ONLY)

add_executable(json11_test test.cpp)
target_link_libraries(json11_test json11)
target_link_libraries(json11_test json11 double-conversion)

install(TARGETS json11 DESTINATION lib)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/json11.hpp" DESTINATION include)
Expand Down
1 change: 1 addition & 0 deletions double-conversion
Submodule double-conversion added at 56a045
16 changes: 13 additions & 3 deletions json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <cstdio>
#include <limits>

#include "double-conversion/double-conversion.h"

namespace json11 {

static const int max_depth = 200;
Expand All @@ -47,9 +49,13 @@ static void dump(std::nullptr_t, string &out) {

static void dump(double value, string &out) {
if (std::isfinite(value)) {
const double_conversion::DoubleToStringConverter &dc = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
char buf[32];
snprintf(buf, sizeof buf, "%.17g", value);
out += buf;
double_conversion::StringBuilder str_builder(buf, sizeof(buf));
dc.ToShortest(value, &str_builder);
// dc.ToFixed(value, 17, &str_builder);
// snprintf(buf, sizeof buf, "%.17g", value);
out += str_builder.Finalize();
} else {
out += "null";
}
Expand Down Expand Up @@ -608,8 +614,12 @@ struct JsonParser {
while (in_range(str[i], '0', '9'))
i++;
}
using double_conversion::StringToDoubleConverter;

return std::strtod(str.c_str() + start_pos, nullptr);
StringToDoubleConverter double_conv{StringToDoubleConverter::NO_FLAGS, 0., 0., "Infinity", "NaN",};
int processed_characters_count;
return double_conv.StringToDouble(&str[start_pos], i - start_pos, &processed_characters_count);
// return std::strtod(str.c_str() + start_pos, nullptr);
}

/* expect(str, res)
Expand Down
4 changes: 2 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char **argv) {
}

const string simple_test =
R"({"k1":"v1", "k2":42, "k3":["a",123,true,false,null]})";
R"({"k1":"v1", "k2":42.1, "k3":["a",123,true,false,null]})";

string err;
auto json = Json::parse(simple_test, err);
Expand Down Expand Up @@ -151,7 +151,7 @@ int main(int argc, char **argv) {
// Json literals
Json obj = Json::object({
{ "k1", "v1" },
{ "k2", 42.0 },
{ "k2", 42.1 },
{ "k3", Json::array({ "a", 123.0, true, false, nullptr }) },
});

Expand Down

0 comments on commit 115fc76

Please sign in to comment.