From 28f0d7be05d77e98ca21c6f31ec48075eb9d178d Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Wed, 13 Aug 2014 14:22:05 +0200 Subject: [PATCH] Sort out TRI_CompareValuesJson to use UTF8 on demand. --- UnitTests/Basics/json-utilities-test.cpp | 3 +- .../Ahuacatl/ahuacatl-access-optimiser.cpp | 42 ++++----- arangod/Aql/AqlValue.cpp | 8 +- arangod/Cluster/ClusterInfo.cpp | 2 +- arangod/Cluster/ClusterMethods.cpp | 2 +- arangod/RestHandler/RestDocumentHandler.cpp | 2 +- arangod/RestHandler/RestVocbaseBaseHandler.h | 2 +- arangod/V8Server/v8-collection.cpp | 2 +- arangod/V8Server/v8-vocbase.cpp | 2 +- arangod/VocBase/index.cpp | 2 +- .../json-utilities.cpp} | 89 +++++++++++-------- lib/{BasicsC => Basics}/json-utilities.h | 24 +++-- lib/CMakeLists.txt | 2 +- lib/Makefile.files | 2 +- 14 files changed, 101 insertions(+), 83 deletions(-) rename lib/{BasicsC/json-utilities.c => Basics/json-utilities.cpp} (90%) rename lib/{BasicsC => Basics}/json-utilities.h (94%) diff --git a/UnitTests/Basics/json-utilities-test.cpp b/UnitTests/Basics/json-utilities-test.cpp index 506123d786..af5bef811d 100644 --- a/UnitTests/Basics/json-utilities-test.cpp +++ b/UnitTests/Basics/json-utilities-test.cpp @@ -27,8 +27,8 @@ #include -#include "BasicsC/json-utilities.h" #include "BasicsC/string-buffer.h" +#include "Basics/json-utilities.h" // ----------------------------------------------------------------------------- // --SECTION-- private macros @@ -81,6 +81,7 @@ BOOST_AUTO_TEST_CASE (tst_compare_values_equal) { TRI_json_t* l; TRI_json_t* r; + // With Utf8-mode: JSON_CHECK(0, TRI_CompareValuesJson, "null", "null"); JSON_CHECK(0, TRI_CompareValuesJson, "false", "false"); JSON_CHECK(0, TRI_CompareValuesJson, "true", "true"); diff --git a/arangod/Ahuacatl/ahuacatl-access-optimiser.cpp b/arangod/Ahuacatl/ahuacatl-access-optimiser.cpp index b9cdec3785..77f6f27b35 100644 --- a/arangod/Ahuacatl/ahuacatl-access-optimiser.cpp +++ b/arangod/Ahuacatl/ahuacatl-access-optimiser.cpp @@ -30,10 +30,10 @@ #include "ahuacatl-access-optimiser.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/logging.h" #include "BasicsC/string-buffer.h" #include "BasicsC/tri-strings.h" +#include "Basics/json-utilities.h" #include "Ahuacatl/ahuacatl-context.h" #include "Ahuacatl/ahuacatl-conversions.h" @@ -532,7 +532,7 @@ static TRI_aql_field_access_t* MergeAndExact (TRI_aql_context_t* const context, if (rhs->_type == TRI_AQL_ACCESS_RANGE_SINGLE) { // check if value is in range - int result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._singleRange._value); + int result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._singleRange._value, true); bool contained = ((rhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_EXCLUDED && result > 0) || (rhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_INCLUDED && result >= 0) || @@ -560,7 +560,7 @@ static TRI_aql_field_access_t* MergeAndExact (TRI_aql_context_t* const context, bool contained; // compare lower end - result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._lower._value); + result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._lower._value, true); contained = ((rhs->_value._between._lower._type == TRI_AQL_RANGE_LOWER_EXCLUDED && result > 0) || (rhs->_value._between._lower._type == TRI_AQL_RANGE_LOWER_INCLUDED && result >= 0)); @@ -575,7 +575,7 @@ static TRI_aql_field_access_t* MergeAndExact (TRI_aql_context_t* const context, } // compare upper end - result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._upper._value); + result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._upper._value, true); contained = ((rhs->_value._between._upper._type == TRI_AQL_RANGE_UPPER_EXCLUDED && result < 0) || (rhs->_value._between._upper._type == TRI_AQL_RANGE_UPPER_INCLUDED && result <= 0)); @@ -763,7 +763,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con rhs = tmp; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._singleRange._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._singleRange._value, true); lhsType = lhs->_value._singleRange._type; rhsType = rhs->_value._singleRange._type; lhsValue = lhs->_value._singleRange._value; @@ -921,7 +921,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con int compareResult; if (lhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_EXCLUDED) { - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value, true); if (compareResult >= 0) { // lhs value is bigger than rhs upper bound TRI_FreeAccessAql(rhs); @@ -931,7 +931,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con return lhs; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value, true); if (compareResult > 0) { // lhs value is bigger than rhs lower bound rhs->_value._between._lower._type = lhs->_value._singleRange._type; @@ -953,7 +953,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con } if (lhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_INCLUDED) { - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value, true); if (compareResult > 0 || (compareResult == 0 && rhs->_value._between._upper._type == TRI_AQL_RANGE_UPPER_EXCLUDED)) { // lhs value is bigger than rhs upper bound TRI_FreeAccessAql(rhs); @@ -977,7 +977,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con return lhs; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value, true); if (compareResult > 0) { // lhs value is bigger than rhs lower bound rhs->_value._between._lower._type = lhs->_value._singleRange._type; @@ -993,7 +993,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con } if (lhs->_value._singleRange._type == TRI_AQL_RANGE_UPPER_EXCLUDED) { - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value, true); if (compareResult <= 0) { // lhs value is smaller than rhs lower bound TRI_FreeAccessAql(rhs); @@ -1003,7 +1003,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con return lhs; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value, true); if (compareResult < 0) { // lhs value is smaller than rhs upper bound rhs->_value._between._upper._type = lhs->_value._singleRange._type; @@ -1025,7 +1025,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con } if (lhs->_value._singleRange._type == TRI_AQL_RANGE_UPPER_INCLUDED) { - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._lower._value, true); if (compareResult < 0 || (compareResult == 0 && rhs->_value._between._lower._type == TRI_AQL_RANGE_LOWER_EXCLUDED)) { // lhs value is smaller than rhs lower bound TRI_FreeAccessAql(rhs); @@ -1048,7 +1048,7 @@ static TRI_aql_field_access_t* MergeAndRangeSingle (TRI_aql_context_t* const con return lhs; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._between._upper._value, true); if (compareResult < 0) { // lhs value is smaller than rhs upper bound rhs->_value._between._upper._type = lhs->_value._singleRange._type; @@ -1098,7 +1098,7 @@ static TRI_aql_field_access_t* MergeAndRangeDouble (TRI_aql_context_t* const con int compareResult; // check lower bound - compareResult = TRI_CompareValuesJson(lhs->_value._between._lower._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._between._lower._value, rhs->_value._between._lower._value, true); if (compareResult > 0) { // we'll patch lhs with the value of rhs lhs->_value._between._lower._type = rhs->_value._between._lower._type; @@ -1114,7 +1114,7 @@ static TRI_aql_field_access_t* MergeAndRangeDouble (TRI_aql_context_t* const con } // check upper bound - compareResult = TRI_CompareValuesJson(lhs->_value._between._upper._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._between._upper._value, rhs->_value._between._upper._value, true); if (compareResult < 0) { // we'll patch lhs with the value of rhs lhs->_value._between._upper._type = rhs->_value._between._upper._type; @@ -1361,7 +1361,7 @@ static TRI_aql_field_access_t* MergeOrExact (TRI_aql_context_t* const context, if (rhs->_type == TRI_AQL_ACCESS_RANGE_SINGLE) { // check if value is in range - int result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._singleRange._value); + int result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._singleRange._value, true); bool contained = ((rhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_EXCLUDED && result > 0) || (rhs->_value._singleRange._type == TRI_AQL_RANGE_LOWER_INCLUDED && result >= 0) || @@ -1389,7 +1389,7 @@ static TRI_aql_field_access_t* MergeOrExact (TRI_aql_context_t* const context, bool contained; // compare lower end - result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._lower._value); + result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._lower._value, true); contained = ((rhs->_value._between._lower._type == TRI_AQL_RANGE_LOWER_EXCLUDED && result > 0) || (rhs->_value._between._lower._type == TRI_AQL_RANGE_LOWER_INCLUDED && result >= 0)); @@ -1404,7 +1404,7 @@ static TRI_aql_field_access_t* MergeOrExact (TRI_aql_context_t* const context, } // compare upper end - result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._upper._value); + result = TRI_CompareValuesJson(lhs->_value._value, rhs->_value._between._upper._value, true); contained = ((rhs->_value._between._upper._type == TRI_AQL_RANGE_UPPER_EXCLUDED && result < 0) || (rhs->_value._between._upper._type == TRI_AQL_RANGE_UPPER_INCLUDED && result <= 0)); @@ -1512,7 +1512,7 @@ static TRI_aql_field_access_t* MergeOrRangeSingle (TRI_aql_context_t* const cont rhs = tmp; } - compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._singleRange._value); + compareResult = TRI_CompareValuesJson(lhs->_value._singleRange._value, rhs->_value._singleRange._value, true); lhsType = lhs->_value._singleRange._type; rhsType = rhs->_value._singleRange._type; @@ -1604,7 +1604,7 @@ static TRI_aql_field_access_t* MergeOrRangeDouble (TRI_aql_context_t* const cont int compareResult; // check lower bound - compareResult = TRI_CompareValuesJson(lhs->_value._between._lower._value, rhs->_value._between._lower._value); + compareResult = TRI_CompareValuesJson(lhs->_value._between._lower._value, rhs->_value._between._lower._value, true); if (compareResult < 0) { // we'll patch lhs with the value of rhs lhs->_value._between._lower._type = rhs->_value._between._lower._type; @@ -1620,7 +1620,7 @@ static TRI_aql_field_access_t* MergeOrRangeDouble (TRI_aql_context_t* const cont } // check upper bound - compareResult = TRI_CompareValuesJson(lhs->_value._between._upper._value, rhs->_value._between._upper._value); + compareResult = TRI_CompareValuesJson(lhs->_value._between._upper._value, rhs->_value._between._upper._value, true); if (compareResult > 0) { // we'll patch lhs with the value of rhs lhs->_value._between._upper._type = rhs->_value._between._upper._type; diff --git a/arangod/Aql/AqlValue.cpp b/arangod/Aql/AqlValue.cpp index 9be9ce4d36..d16e2bfcda 100644 --- a/arangod/Aql/AqlValue.cpp +++ b/arangod/Aql/AqlValue.cpp @@ -27,7 +27,7 @@ #include "Aql/AqlValue.h" #include "Aql/AqlItemBlock.h" -#include "BasicsC/json-utilities.h" +#include "Basics/json-utilities.h" #include "V8/v8-conv.h" #include "V8Server/v8-wrapshapedjson.h" #include "VocBase/voc-shaper.h" @@ -387,12 +387,12 @@ int AqlValue::Compare (AqlValue const& left, if (left._type == AqlValue::JSON && right._type == AqlValue::SHAPED) { triagens::basics::Json rjson = right.toJson(rightcoll); - return TRI_CompareValuesJson(left._json->json(), rjson.json()); + return TRI_CompareValuesJson(left._json->json(), rjson.json(), true); } if (left._type == AqlValue::SHAPED && right._type == AqlValue::JSON) { triagens::basics::Json ljson = left.toJson(leftcoll); - return TRI_CompareValuesJson(ljson.json(), right._json->json()); + return TRI_CompareValuesJson(ljson.json(), right._json->json(), true); } // No other comparisons are defined @@ -407,7 +407,7 @@ int AqlValue::Compare (AqlValue const& left, } case AqlValue::JSON: { - return TRI_CompareValuesJson(left._json->json(), right._json->json()); + return TRI_CompareValuesJson(left._json->json(), right._json->json(), true); } case AqlValue::SHAPED: { diff --git a/arangod/Cluster/ClusterInfo.cpp b/arangod/Cluster/ClusterInfo.cpp index 5698e5cf83..d5aaa8d6b3 100644 --- a/arangod/Cluster/ClusterInfo.cpp +++ b/arangod/Cluster/ClusterInfo.cpp @@ -32,10 +32,10 @@ #include "BasicsC/conversions.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/logging.h" #include "BasicsC/tri-strings.h" #include "BasicsC/vector.h" +#include "Basics/json-utilities.h" #include "Basics/JsonHelper.h" #include "Basics/ReadLocker.h" #include "Basics/WriteLocker.h" diff --git a/arangod/Cluster/ClusterMethods.cpp b/arangod/Cluster/ClusterMethods.cpp index b4f2fc7e86..afbc2e1b10 100644 --- a/arangod/Cluster/ClusterMethods.cpp +++ b/arangod/Cluster/ClusterMethods.cpp @@ -32,9 +32,9 @@ #include "BasicsC/conversions.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/tri-strings.h" #include "BasicsC/vector.h" +#include "Basics/json-utilities.h" #include "Basics/StringUtils.h" #include "VocBase/index.h" #include "VocBase/server.h" diff --git a/arangod/RestHandler/RestDocumentHandler.cpp b/arangod/RestHandler/RestDocumentHandler.cpp index b626212206..fa1467280e 100644 --- a/arangod/RestHandler/RestDocumentHandler.cpp +++ b/arangod/RestHandler/RestDocumentHandler.cpp @@ -32,8 +32,8 @@ #include "Basics/StringUtils.h" #include "BasicsC/conversions.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/string-buffer.h" +#include "Basics/json-utilities.h" #include "Rest/HttpRequest.h" #include "VocBase/document-collection.h" #include "VocBase/vocbase.h" diff --git a/arangod/RestHandler/RestVocbaseBaseHandler.h b/arangod/RestHandler/RestVocbaseBaseHandler.h index 2654905fbe..17bd042587 100644 --- a/arangod/RestHandler/RestVocbaseBaseHandler.h +++ b/arangod/RestHandler/RestVocbaseBaseHandler.h @@ -35,8 +35,8 @@ #include "Admin/RestBaseHandler.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/logging.h" +#include "Basics/json-utilities.h" #include "Rest/HttpResponse.h" #include "Utils/transactions.h" diff --git a/arangod/V8Server/v8-collection.cpp b/arangod/V8Server/v8-collection.cpp index 2470960c5d..07507b8d52 100644 --- a/arangod/V8Server/v8-collection.cpp +++ b/arangod/V8Server/v8-collection.cpp @@ -36,7 +36,7 @@ #include "Basics/Utf8Helper.h" #include "BasicsC/conversions.h" -#include "BasicsC/json-utilities.h" +#include "Basics/json-utilities.h" #include "V8/v8-conv.h" #include "Utils/transactions.h" #include "Utils/V8TransactionContext.h" diff --git a/arangod/V8Server/v8-vocbase.cpp b/arangod/V8Server/v8-vocbase.cpp index baf4e7e169..170e697e18 100644 --- a/arangod/V8Server/v8-vocbase.cpp +++ b/arangod/V8Server/v8-vocbase.cpp @@ -39,7 +39,7 @@ #include "Basics/Utf8Helper.h" #include "BasicsC/conversions.h" -#include "BasicsC/json-utilities.h" +#include "Basics/json-utilities.h" #include "Utils/transactions.h" #include "Utils/AhuacatlGuard.h" #include "Utils/AhuacatlTransaction.h" diff --git a/arangod/VocBase/index.cpp b/arangod/VocBase/index.cpp index ff0d6c87a8..1b3b4b06c4 100644 --- a/arangod/VocBase/index.cpp +++ b/arangod/VocBase/index.cpp @@ -32,13 +32,13 @@ #include "BasicsC/conversions.h" #include "BasicsC/files.h" #include "BasicsC/json.h" -#include "BasicsC/json-utilities.h" #include "BasicsC/linked-list.h" #include "BasicsC/logging.h" #include "BasicsC/string-buffer.h" #include "BasicsC/tri-strings.h" #include "BasicsC/utf8-helper.h" #include "BasicsC/fasthash.h" +#include "Basics/json-utilities.h" #include "Basics/JsonHelper.h" #include "CapConstraint/cap-constraint.h" #include "GeoIndex/geo-index.h" diff --git a/lib/BasicsC/json-utilities.c b/lib/Basics/json-utilities.cpp similarity index 90% rename from lib/BasicsC/json-utilities.c rename to lib/Basics/json-utilities.cpp index 0995037973..42341f72e0 100644 --- a/lib/BasicsC/json-utilities.c +++ b/lib/Basics/json-utilities.cpp @@ -27,7 +27,9 @@ /// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// -#include "BasicsC/json-utilities.h" +#include "Basics/json-utilities.h" + +#include "BasicsC/utf8-helper.h" #include "BasicsC/string-buffer.h" #include "BasicsC/hashes.h" @@ -50,8 +52,8 @@ static TRI_json_t* MergeRecursive (TRI_memory_zone_t* zone, n = rhs->_value._objects._length; for (i = 0; i < n; i += 2) { // enumerate all the replacement values - TRI_json_t* key = TRI_AtVector(&rhs->_value._objects, i); - TRI_json_t* value = TRI_AtVector(&rhs->_value._objects, i + 1); + auto key = reinterpret_cast(TRI_AtVector(&rhs->_value._objects, i)); + auto value = reinterpret_cast(TRI_AtVector(&rhs->_value._objects, i + 1)); if (value->_type == TRI_JSON_NULL && nullMeansRemove) { // replacement value is a null and we don't want to store nulls => delete attribute from the result @@ -153,7 +155,7 @@ static int TypeWeight (const TRI_json_t* const value) { //////////////////////////////////////////////////////////////////////////////// static int CompareJson (TRI_json_t* lhs, TRI_json_t* rhs, size_t size) { - return TRI_CompareValuesJson((TRI_json_t*) lhs, (TRI_json_t*) rhs); + return TRI_CompareValuesJson((TRI_json_t*) lhs, (TRI_json_t*) rhs, true); } //////////////////////////////////////////////////////////////////////////////// @@ -179,7 +181,7 @@ static TRI_json_t* GetMergedKeyList (const TRI_json_t* const lhs, n = lhs->_value._objects._length; for (i = 0 ; i < n; i += 2) { - TRI_json_t* key = TRI_AtVector(&lhs->_value._objects, i); + auto key = reinterpret_cast(TRI_AtVector(&lhs->_value._objects, i)); TRI_ASSERT(TRI_IsStringJson(key)); TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, keys, key); @@ -189,7 +191,7 @@ static TRI_json_t* GetMergedKeyList (const TRI_json_t* const lhs, n = rhs->_value._objects._length; for (i = 0 ; i < n; i += 2) { - TRI_json_t* key = TRI_AtVector(&rhs->_value._objects, i); + auto key = reinterpret_cast(TRI_AtVector(&rhs->_value._objects, i)); TRI_ASSERT(TRI_IsStringJson(key)); TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, keys, key); @@ -215,7 +217,8 @@ static TRI_json_t* GetMergedKeyList (const TRI_json_t* const lhs, //////////////////////////////////////////////////////////////////////////////// int TRI_CompareValuesJson (const TRI_json_t* const lhs, - const TRI_json_t* const rhs) { + const TRI_json_t* const rhs, + bool useUTF8) { // note: both lhs and rhs may be NULL! int lWeight = TypeWeight(lhs); int rWeight = TypeWeight(rhs); @@ -264,7 +267,23 @@ int TRI_CompareValuesJson (const TRI_json_t* const lhs, case TRI_JSON_STRING: case TRI_JSON_STRING_REFERENCE: // same for STRING and STRING_REFERENCE - return strcmp(lhs->_value._string.data, rhs->_value._string.data); + int res; + if (useUTF8) { + res = TRI_compare_utf8(lhs->_value._string.data, + rhs->_value._string.data); + } + else { + res = strcmp(lhs->_value._string.data, rhs->_value._string.data); + } + if (res < 0) { + return -1; + } + else if (res > 0) { + return 1; + } + else { + return 0; + } case TRI_JSON_LIST: { size_t nl = lhs->_value._objects._length; @@ -283,9 +302,9 @@ int TRI_CompareValuesJson (const TRI_json_t* const lhs, TRI_json_t* rhsValue; int result; - lhsValue = (i >= nl) ? NULL : TRI_AtVector(&lhs->_value._objects, i); - rhsValue = (i >= nr) ? NULL : TRI_AtVector(&rhs->_value._objects, i); - result = TRI_CompareValuesJson(lhsValue, rhsValue); + lhsValue = (i >= nl) ? NULL : reinterpret_cast(TRI_AtVector(&lhs->_value._objects, i)); + rhsValue = (i >= nr) ? NULL : reinterpret_cast(TRI_AtVector(&rhs->_value._objects, i)); + result = TRI_CompareValuesJson(lhsValue, rhsValue, useUTF8); if (result != 0) { return result; } @@ -312,13 +331,13 @@ int TRI_CompareValuesJson (const TRI_json_t* const lhs, TRI_json_t* rhsValue; int result; - keyElement = TRI_AtVector(&keys->_value._objects, i); + keyElement = reinterpret_cast(TRI_AtVector(&keys->_value._objects, i)); TRI_ASSERT(TRI_IsStringJson(keyElement)); lhsValue = TRI_LookupArrayJson((TRI_json_t*) lhs, keyElement->_value._string.data); // may be NULL rhsValue = TRI_LookupArrayJson((TRI_json_t*) rhs, keyElement->_value._string.data); // may be NULL - result = TRI_CompareValuesJson(lhsValue, rhsValue); + result = TRI_CompareValuesJson(lhsValue, rhsValue, useUTF8); if (result != 0) { TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, keys); @@ -343,7 +362,7 @@ int TRI_CompareValuesJson (const TRI_json_t* const lhs, bool TRI_CheckSameValueJson (const TRI_json_t* const lhs, const TRI_json_t* const rhs) { - return (TRI_CompareValuesJson(lhs, rhs) == 0); + return (TRI_CompareValuesJson(lhs, rhs, false) == 0); } //////////////////////////////////////////////////////////////////////////////// @@ -400,11 +419,11 @@ TRI_json_t* TRI_BetweenListJson (const TRI_json_t* const list, n = list->_value._objects._length; for (i = 0; i < n; ++i) { - TRI_json_t* p = TRI_AtVector(&list->_value._objects, i); + auto p = reinterpret_cast(TRI_AtVector(&list->_value._objects, i)); if (lower) { // lower bound is set - int compareResult = TRI_CompareValuesJson(lower, p); + int compareResult = TRI_CompareValuesJson(lower, p, true); if (compareResult > 0 || (compareResult == 0 && ! includeLower)) { // element is bigger than lower bound continue; @@ -413,7 +432,7 @@ TRI_json_t* TRI_BetweenListJson (const TRI_json_t* const list, if (upper) { // upper bound is set - int compareResult = TRI_CompareValuesJson(p, upper); + int compareResult = TRI_CompareValuesJson(p, upper, true); if (compareResult > 0 || (compareResult == 0 && ! includeUpper)) { // element is smaller than upper bound continue; @@ -450,10 +469,10 @@ TRI_json_t* TRI_UniquifyListJson (const TRI_json_t* const list) { n = list->_value._objects._length; for (i = 0; i < n; ++i) { - TRI_json_t* p = TRI_AtVector(&list->_value._objects, i); + auto p = reinterpret_cast(TRI_AtVector(&list->_value._objects, i)); // don't push value if it is the same as the last value - if (last == NULL || TRI_CompareValuesJson(p, last) > 0) { + if (last == NULL || TRI_CompareValuesJson(p, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p); // remember last element @@ -514,13 +533,13 @@ TRI_json_t* TRI_UnionizeListsJson (const TRI_json_t* const list1, int compareResult; // both lists not yet exhausted - p1 = TRI_AtVector(&list1->_value._objects, i1); - p2 = TRI_AtVector(&list2->_value._objects, i2); - compareResult = TRI_CompareValuesJson(p1, p2); + p1 = reinterpret_cast(TRI_AtVector(&list1->_value._objects, i1)); + p2 = reinterpret_cast(TRI_AtVector(&list2->_value._objects, i2)); + compareResult = TRI_CompareValuesJson(p1, p2, true); if (compareResult < 0) { // left element is smaller - if (! unique || last == NULL || TRI_CompareValuesJson(p1, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p1, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p1); last = p1; } @@ -528,7 +547,7 @@ TRI_json_t* TRI_UnionizeListsJson (const TRI_json_t* const list1, } else if (compareResult > 0) { // right element is smaller - if (! unique || last == NULL || TRI_CompareValuesJson(p2, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p2, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p2); last = p2; } @@ -536,7 +555,7 @@ TRI_json_t* TRI_UnionizeListsJson (const TRI_json_t* const list1, } else { // both elements are equal - if (! unique || last == NULL || TRI_CompareValuesJson(p1, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p1, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p1); last = p1; @@ -550,9 +569,9 @@ TRI_json_t* TRI_UnionizeListsJson (const TRI_json_t* const list1, } else if (i1 < n1 && i2 >= n2) { // only right list is exhausted - p1 = TRI_AtVector(&list1->_value._objects, i1); + p1 = reinterpret_cast(TRI_AtVector(&list1->_value._objects, i1)); - if (! unique || last == NULL || TRI_CompareValuesJson(p1, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p1, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p1); last = p1; } @@ -560,9 +579,9 @@ TRI_json_t* TRI_UnionizeListsJson (const TRI_json_t* const list1, } else if (i1 >= n1 && i2 < n2) { // only left list is exhausted - p2 = TRI_AtVector(&list2->_value._objects, i2); + p2 = reinterpret_cast(TRI_AtVector(&list2->_value._objects, i2)); - if (! unique || last == NULL || TRI_CompareValuesJson(p2, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p2, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p2); last = p2; } @@ -616,10 +635,10 @@ TRI_json_t* TRI_IntersectListsJson (const TRI_json_t* const list1, // iterate over lists while (i1 < n1 && i2 < n2) { // pointers to elements in both lists - TRI_json_t* p1 = TRI_AtVector(&list1->_value._objects, i1); - TRI_json_t* p2 = TRI_AtVector(&list2->_value._objects, i2); + auto p1 = reinterpret_cast(TRI_AtVector(&list1->_value._objects, i1)); + auto p2 = reinterpret_cast(TRI_AtVector(&list2->_value._objects, i2)); - int compareResult = TRI_CompareValuesJson(p1, p2); + int compareResult = TRI_CompareValuesJson(p1, p2, true); if (compareResult < 0) { // left element is smaller @@ -631,7 +650,7 @@ TRI_json_t* TRI_IntersectListsJson (const TRI_json_t* const list1, } else { // both elements are equal - if (! unique || last == NULL || TRI_CompareValuesJson(p1, last) > 0) { + if (! unique || last == NULL || TRI_CompareValuesJson(p1, last, true) > 0) { TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, result, p1); last = p1; @@ -698,13 +717,13 @@ bool TRI_HasDuplicateKeyJson (TRI_json_t const* object) { TRI_json_t* key; TRI_json_t* value; - key = TRI_AtVector(&object->_value._objects, i); + key = reinterpret_cast(TRI_AtVector(&object->_value._objects, i)); if (! TRI_IsStringJson(key)) { continue; } - value = TRI_AtVector(&object->_value._objects, i + 1); + value = reinterpret_cast(TRI_AtVector(&object->_value._objects, i + 1)); // recursively check sub-array elements if (value->_type == TRI_JSON_ARRAY && TRI_HasDuplicateKeyJson(value)) { diff --git a/lib/BasicsC/json-utilities.h b/lib/Basics/json-utilities.h similarity index 94% rename from lib/BasicsC/json-utilities.h rename to lib/Basics/json-utilities.h index 84f191fdd0..56e1c741f1 100644 --- a/lib/BasicsC/json-utilities.h +++ b/lib/Basics/json-utilities.h @@ -27,16 +27,13 @@ /// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// -#ifndef ARANGODB_BASICS_C_JSON__UTILITIES_H -#define ARANGODB_BASICS_C_JSON__UTILITIES_H 1 +#ifndef ARANGODB_BASICS_JSON__UTILITIES_H +#define ARANGODB_BASICS_JSON__UTILITIES_H 1 + +#include "Basics/Common.h" -#include "BasicsC/common.h" #include "BasicsC/json.h" -#ifdef __cplusplus -extern "C" { -#endif - // ----------------------------------------------------------------------------- // --SECTION-- public functions // ----------------------------------------------------------------------------- @@ -47,10 +44,15 @@ extern "C" { /// the values are first compared by their types, and only by their values if /// the types are the same /// returns -1 if lhs is smaller than rhs, 0 if lhs == rhs, and 1 if rhs is -/// greater than lhs +/// greater than lhs. +/// +/// If useUTF8 is set to true, strings will be converted using proper UTF8, +/// otherwise, strcmp is used, so it should only be used to test for equality +/// in this case. //////////////////////////////////////////////////////////////////////////////// -int TRI_CompareValuesJson (const TRI_json_t* const, const TRI_json_t* const); +int TRI_CompareValuesJson (const TRI_json_t* const, const TRI_json_t* const, + bool useUTF8 = true); //////////////////////////////////////////////////////////////////////////////// /// @brief check if two json values are the same @@ -151,10 +153,6 @@ uint64_t TRI_HashJsonByAttributes (TRI_json_t const* json, bool docComplete, int* error); -#ifdef __cplusplus -} -#endif - #endif // ----------------------------------------------------------------------------- diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 782f78c74d..54bcff6bb0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -50,6 +50,7 @@ add_library( Basics/Exceptions.cpp Basics/FileUtils.cpp Basics/InitialiseBasics.cpp + Basics/json-utilities.cpp Basics/JsonHelper.cpp Basics/Mutex.cpp Basics/MutexLocker.cpp @@ -79,7 +80,6 @@ add_library( BasicsC/hashes.c Basics/init.cpp BasicsC/json.c - BasicsC/json-utilities.c BasicsC/linked-list.c BasicsC/logging.c BasicsC/memory.c diff --git a/lib/Makefile.files b/lib/Makefile.files index 7f0f043f12..44803709b4 100644 --- a/lib/Makefile.files +++ b/lib/Makefile.files @@ -17,6 +17,7 @@ lib_libarango_a_SOURCES = \ lib/Basics/Exceptions.cpp \ lib/Basics/FileUtils.cpp \ lib/Basics/InitialiseBasics.cpp \ + lib/Basics/json-utilities.cpp \ lib/Basics/JsonHelper.cpp \ lib/Basics/Mutex.cpp \ lib/Basics/MutexLocker.cpp \ @@ -46,7 +47,6 @@ lib_libarango_a_SOURCES = \ lib/BasicsC/hashes.c \ lib/Basics/init.cpp \ lib/BasicsC/json.c \ - lib/BasicsC/json-utilities.c \ lib/BasicsC/linked-list.c \ lib/BasicsC/locks-macos.c \ lib/BasicsC/locks-posix.c \