From 27c94294e558f5da86c1badf79fe471ad114c69a Mon Sep 17 00:00:00 2001 From: chuikingshek Date: Wed, 30 Oct 2024 16:16:55 +0800 Subject: [PATCH 1/4] Improvement: use standard switch/case statement --- json.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/json.cpp b/json.cpp index 420b2c9..56f86b7 100644 --- a/json.cpp +++ b/json.cpp @@ -881,7 +881,15 @@ Json::parse(Json& json, const char*& p, const char* e, int context, int depth) json.setLong(0); return success; - case '1' ... '9': // integer + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': // integer if (context & (COLON | COMMA | KEY)) goto OnColonCommaKey; for (x = (c - '0') * d; p < e; ++p) { From 654c5a6f40585b4df67e68475e86b08e065471fc Mon Sep 17 00:00:00 2001 From: chuikingshek Date: Wed, 30 Oct 2024 17:24:30 +0800 Subject: [PATCH 2/4] Improvement: use c11 timespec_get instead of posix clock_gettime --- json_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_test.cpp b/json_test.cpp index e222ada..f56c65b 100644 --- a/json_test.cpp +++ b/json_test.cpp @@ -101,7 +101,7 @@ struct timespec now(void) { struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); + timespec_get(&ts, TIME_UTC); return ts; } From cdba25727e3fd6a88e5f353f0192fea6b2c5d939 Mon Sep 17 00:00:00 2001 From: chuikingshek Date: Wed, 30 Oct 2024 17:26:51 +0800 Subject: [PATCH 3/4] Build system: add CMake support --- .gitignore | 2 ++ CMakeLists.txt | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index 24f31d8..319a48e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /*.a /*.ok /*_test +/build/* +/cmake-build-* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ee14399 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.5) +project(json_cpp) + +# options + +set(JART_JSON_CPP_LIBRARY_NAME ${CMAKE_PROJECT_NAME} CACHE STRING "CMake target name and library name of json.cpp, default to 'json_cpp' (project name)") +option(JART_JSON_CPP_ENABLE_TEST "enable json.cpp test" ON) +option(JART_JSON_CPP_ENABLE_TEST_JSON_TEST_SUITE "enable json.cpp json test suite" ON) + +# library: double conversion + +set(DOUBLE_CONVERSION_LIBRARY_NAME "${JART_JSON_CPP_LIBRARY_NAME}_double_conversion") +add_library(${DOUBLE_CONVERSION_LIBRARY_NAME} STATIC) +target_compile_features(${DOUBLE_CONVERSION_LIBRARY_NAME} PRIVATE cxx_std_17) +target_include_directories(${DOUBLE_CONVERSION_LIBRARY_NAME} PUBLIC double-conversion) +target_sources(${DOUBLE_CONVERSION_LIBRARY_NAME} PRIVATE + double-conversion/bignum-dtoa.cc + double-conversion/bignum-dtoa.h + double-conversion/bignum.cc + double-conversion/bignum.h + double-conversion/cached-powers.cc + double-conversion/cached-powers.h + double-conversion/diy-fp.h + double-conversion/double-conversion.h + double-conversion/double-to-string.cc + double-conversion/double-to-string.h + double-conversion/fast-dtoa.cc + double-conversion/fast-dtoa.h + double-conversion/fixed-dtoa.cc + double-conversion/fixed-dtoa.h + double-conversion/ieee.h + double-conversion/string-to-double.cc + double-conversion/string-to-double.h + double-conversion/strtod.cc + double-conversion/strtod.h + double-conversion/utils.h +) + +# library: json.cpp + +set(LIBRARY_NAME ${JART_JSON_CPP_LIBRARY_NAME}) +add_library(${LIBRARY_NAME} STATIC) +target_compile_features(${LIBRARY_NAME} PRIVATE cxx_std_17) +target_include_directories(${LIBRARY_NAME} PUBLIC .) +target_sources(${LIBRARY_NAME} PRIVATE + jtckdint.h + json.h + json.cpp +) +target_link_libraries(${LIBRARY_NAME} INTERFACE ${DOUBLE_CONVERSION_LIBRARY_NAME}) + +# test: benchmark + +if (JART_JSON_CPP_ENABLE_TEST) + set(TEST_NAME "${JART_JSON_CPP_LIBRARY_NAME}_test") + add_executable(${TEST_NAME}) + target_compile_features(${TEST_NAME} PRIVATE cxx_std_17) + target_sources(${TEST_NAME} PRIVATE json_test.cpp) + target_link_libraries(${TEST_NAME} PRIVATE ${LIBRARY_NAME}) +endif () + +# test: json test suite + +if (JART_JSON_CPP_ENABLE_TEST_JSON_TEST_SUITE) + set(TEST_JSON_TEST_SUITE_NAME "${JART_JSON_CPP_LIBRARY_NAME}_test_json_test_suite") + add_executable(${TEST_JSON_TEST_SUITE_NAME}) + target_compile_features(${TEST_JSON_TEST_SUITE_NAME} PRIVATE cxx_std_17) + target_sources(${TEST_JSON_TEST_SUITE_NAME} PRIVATE jsontestsuite_test.cpp) + target_link_libraries(${TEST_JSON_TEST_SUITE_NAME} PRIVATE ${LIBRARY_NAME}) +endif () From c2bc12bbed181f23904eb115dc194f557192df09 Mon Sep 17 00:00:00 2001 From: chuikingshek Date: Wed, 30 Oct 2024 17:31:31 +0800 Subject: [PATCH 4/4] Improvement: remove redundant #include --- jsontestsuite_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/jsontestsuite_test.cpp b/jsontestsuite_test.cpp index b5fe16e..719eeed 100644 --- a/jsontestsuite_test.cpp +++ b/jsontestsuite_test.cpp @@ -19,7 +19,6 @@ #include #include -#include #define HI_RESET "\033[0m" // green #define HI_GOOD "\033[32m" // green