8000 Make ParsedJson::Iterator backcompat test · JavaScriptExpert/simdjson@f0f111b · GitHub
[go: up one dir, main page]

Skip to content

Commit f0f111b

Browse files
committed
Make ParsedJson::Iterator backcompat test
1 parent c95e45d commit f0f111b

File tree

4 files changed

+271
-259
lines changed

4 files changed

+271
-259
lines changed

include/simdjson/common_defs.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ constexpr size_t DEFAULT_MAX_DEPTH = 1024;
7373
#define unlikely(x) x
7474
#endif
7575

76+
#define SIMDJSON_PUSH_DISABLE_WARNINGS __pragma(warning( push ))
77+
#define SIMDJSON_DISABLE_VS_WARNING(WARNING_NUMBER) __pragma(warning( disable : WARNING_NUMBER ))
78+
#define SIMDJSON_DISABLE_DEPRECATED_WARNING SIMDJSON_DISABLE_VS_WARNING(4996)
79+
#define SIMDJSON_POP_DISABLE_WARNINGS __pragma(warning( pop ))
7680

77-
#else
81+
#else // MSC_VER
7882

7983

8084
#define really_inline inline __attribute__((always_inline, unused))
@@ -90,6 +94,12 @@ constexpr size_t DEFAULT_MAX_DEPTH = 1024;
9094
#define unlikely(x) __builtin_expect(!!(x), 0)
9195
#endif
9296

97+
#define SIMDJSON_PUSH_DISABLE_WARNINGS _Pragma("GCC diagnostic push")
98+
#define SIMDJSON_PRAGMA(P) _Pragma(#P)
99+
#define SIMDJSON_DISABLE_GCC_WARNING(WARNING) SIMDJSON_PRAGMA(GCC diagnostic ignored #WARNING)
100+
#define SIMDJSON_DISABLE_DEPRECATED_WARNING SIMDJSON_DISABLE_GCC_WARNING(-Wdeprecated-declarations)
101+
#define SIMDJSON_POP_DISABLE_WARNINGS _Pragma("GCC diagnostic pop")
102+
93103
#endif // MSC_VER
94104

95105
#endif // SIMDJSON_COMMON_DEFS_H

include/simdjson/document.h

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,6 @@ class document {
180180
*/
181181
inline element_result at_key(std::string_view s) const noexcept;
182182

183-
/**
184-
* Get the value associated with the given key.
185-
*
186-
* Note: The key will be matched against **unescaped** JSON:
187-
*
188-
* document::parser parser;
189-
* parser.parse(R"({ "a\n": 1 })")["a\n"].as_uint64_t().value == 1
190-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].as_uint64_t().error == NO_SUCH_FIELD
191-
*
192-
* @return The value associated with this field, or:
193-
* - NO_SUCH_FIELD if the field does not exist in the object
194-
*/
195-
inline element_result at_key(const char *s) const noexcept;
196-
197183
std::unique_ptr<uint64_t[]> tape;
198184
std::unique_ptr<uint8_t[]> string_buf;// should be at least byte_capacity
199185

@@ -256,6 +242,11 @@ class document::doc_result : public simdjson_result<document&> {
256242
*/
257243
inline array_result as_array() const noexcept;
258244

245+
/**
246+
* Get the root element of this document.
247+
*/
248+
inline element_result root() const noexcept;
249+
259250
/**
260251
* Get the value associated with the given JSON pointer.
261252
*
@@ -644,18 +635,14 @@ class document::element : protected internal::tape_ref {
644635
inline element_result at_key(std::string_view s) const noexcept;
645636

646637
/**
647-
* Get the value associated with the given key.
648-
*
649-
* Note: The key will be matched against **unescaped** JSON:
638+
* Get the value associated with the given key in a case-insensitive manner.
650639
*
651-
* document::parser parser;
652-
* parser.parse(R"({ "a\n": 1 })")["a\n"].as_uint64_t().value == 1
653-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].as_uint64_t().error == NO_SUCH_FIELD
640+
* Note: The key will be matched against **unescaped** JSON.
654641
*
655642
* @return The value associated with this field, or:
656643
* - NO_SUCH_FIELD if the field does not exist in the object
657644
*/
658-
inline element_result at_key(const char *s) const noexcept;
645+
inline element_result at_key_case_insensitive(std::string_view s) const noexcept;
659646

660647
private:
661648
really_inline element(const document *_doc, size_t _json_index) noexcept;
@@ -889,7 +876,7 @@ class document::object : protected internal::tape_ref {
889876
* @return The value associated with this field, or:
890877
* - NO_SUCH_FIELD if the field does not exist in the object
891878
*/
892-
inline element_result at_key(std::string_view s) const noexcept;
879+
inline element_result at_key(std::string_view key) const noexcept;
893880

894881
/**
895882
* Get the value associated with the given key.
@@ -899,18 +886,18 @@ class document::object : protected internal::tape_ref {
899886
* @return The value associated with this field, or:
900887
* - NO_SUCH_FIELD if the field does not exist in the object
901888
*/
902-
inline element_result at_key(const char *s) const noexcept;
889+
inline element_result at_key(const char *key) const noexcept;
903890

904891
/**
905-
* Get the value associated with the given key, the provided key is
906-
* considered to have length characters.
892+
* Get the value associated with the given key in a case-insensitive manner.
907893
*
908894
* Note: The key will be matched against **unescaped** JSON.
909895
*
910896
* @return The value associated with this field, or:
911897
* - NO_SUCH_FIELD if the field does not exist in the object
912898
*/
913-
inline element_result at_key(const char *s, size_t length) const noexcept;
899+
inline element_result at_key_case_insensitive(std::string_view key) const noexcept;
900+
914901
/**
915902
* Get the value associated with the given key in a case-insensitive manner.
916903
*
@@ -919,7 +906,7 @@ class document::object : protected internal::tape_ref {
919906
* @return The value associated with this field, or:
920907
* - NO_SUCH_FIELD if the field does not exist in the object
921908
*/
922-
inline element_result at_key_case_insensitive(const char *s) const noexcept;
909+
inline element_result at_key_case_insensitive(const char *key) const noexcept;
923910

924911
private:
925912
really_inline object(const document *_doc, size_t _json_index) noexcept;
@@ -967,6 +954,8 @@ class document::element_result : public simdjson_result<document::element> {
967954
inline element_result at(size_t index) const noexcept;
968955
inline element_result at_key(std::string_view key) const noexcept;
969956
inline element_result at_key(const char *key) const noexcept;
957+
inline element_result at_key_case_insensitive(std::string_view key) const noexcept;
958+
inline element_result at_key_case_insensitive(const char *key) const noexcept;
970959

971960
#if SIMDJSON_EXCEPTIONS
972961
inline operator bool() const noexcept(false);
@@ -1009,7 +998,7 @@ class document::object_result : public simdjson_result<document::object> {
1009998
inline element_result operator[](const char *json_pointer) const noexcept;
1010999
inline element_result at(std::string_view json_pointer) const noexcept;
10111000
inline element_result at_key(std::string_view key) const noexcept;
1012-
inline element_result at_key(const char *key) const noexcept;
1001+
inline element_result at_key_case_insensitive(std::string_view key) const noexcept;
10131002

10141003
#if SIMDJSON_EXCEPTIONS
10151004
inline object::iterator begin() const noexcept(false);

include/simdjson/inline/document.h

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ inline document::element_result document::element_result::at_key(std::string_vie
7575
if (error()) { return *this; }
7676
return first.at_key(key);
7777
}
78-
inline document::element_result document::element_result::at_key(const char *key) const noexcept {
78+
inline document::element_result document::element_result::at_key_case_insensitive(std::string_view key) const noexcept {
7979
if (error()) { return *this; }
80-
return first.at_key(key);
80+
return first.at_key_case_insensitive(key);
8181
}
8282

8383
#if SIMDJSON_EXCEPTIONS
@@ -167,9 +167,9 @@ inline document::element_result document::object_result::at_key(std::string_view
167167
if (error()) { return error(); }
168168
return first.at_key(key);
169169
}
170-
inline document::element_result document::object_result::at_key(const char *key) const noexcept {
170+
inline document::element_result document::object_result::at_key_case_insensitive(std::string_view key) const noexcept {
171171
if (error()) { return error(); }
172-
return first.at_key(key);
172+
return first.at_key_case_insensitive(key);
173173
}
174174

175175
#if SIMDJSON_EXCEPTIONS
@@ -227,9 +227,6 @@ inline document::element_result document::at(size_t index) const noexcept {
227227
inline document::element_result document::at_key(std::string_view key) const noexcept {
228228
return as_object().at_key(key);
229229
}
230-
inline document::element_result document::at_key(const char *key) const noexcept {
231-
return as_object().at_key(key);
232-
}
233230
inline document::element_result document::operator[](std::string_view json_pointer) const noexcept {
234231
return at(json_pointer);
235232
}
@@ -359,12 +356,14 @@ inline bool document::dump_raw_tape(std::ostream &os) const noexcept {
359356
inline document::doc_result::doc_result(document &doc, error_code error) noexcept : simdjson_result<document&>(doc, error) { }
360357

361358
inline document::array_result document::doc_result::as_array() const noexcept {
362-
if (error()) { return error(); }
363-
return first.root().as_array();
359+
return root().as_array();
364360
}
365361
inline document::object_result document::doc_result::as_object() const noexcept {
362+
return root().as_object();
363+
}
364+
inline document::element_result document::doc_result::root() const noexcept {
366365
if (error()) { return error(); }
367-
return first.root().as_object();
366+
return first.root();
368367
}
369368

370369
inline document::element_result document::doc_result::operator[](std::string_view key) const noexcept {
@@ -783,16 +782,6 @@ inline document::element_result document::object::at(std::string_view json_point
783782

784783
return child;
785784
}
786-
inline document::element_result document::object::at_key(const char *key, size_t length) const noexcept {
787-
iterator end_field = end();
788-
for (iterator field = begin(); field != end_field; ++field) {
789-
std::string_view v{field.key()};
790-
if ((v.size() == length) && (!memcmp(v.data(), key, length))) {
791-
return field.value();
792-
}
793-
}
794-
return NO_SUCH_FIELD;
795-
}
796785
inline document::element_result document::object::at_key(std::string_view key) const noexcept {
797786
iterator end_field = end();
798787
for (iterator field = begin(); field != end_field; ++field) {
@@ -802,27 +791,21 @@ inline document::element_result document::object::at_key(std::string_view key) c
802791
}
803792
return NO_SUCH_FIELD;
804793
}
805-
inline document::element_result document::object::at_key(const char *key) const noexcept {
806-
iterator end_field = end();
807-
for (iterator field = begin(); field != end_field; ++field) {
808-
if (!strcmp(key, field.key_c_str())) {
809-
return field.value();
810-
}
811-
}
812-
return NO_SUCH_FIELD;
813-
}
814794
// In case you wonder why we need this, please see
815795
// https://github.com/simdjson/simdjson/issues/323
816796
// People do seek keys in a case-insensitive manner.
817-
inline document::element_result document::object::at_key_case_insensitive(const char *key) const noexcept {
797+
inline document::element_result document::object::at_key_case_insensitive(std::string_view key) const noexcept {
818798
iterator end_field = end();
819799
for (iterator field = begin(); field != end_field; ++field) {
820-
if (!simdjson_strcasecmp(key, field.key_c_str())) {
800+
if (std::equal(key.begin(), key.end(), field.key().begin(), [](char a, char b) {
801+
return std::tolower(a) == std::tolower(b);
802+
})) {
821803
return field.value();
822804
}
823805
}
824806
return NO_SUCH_FIELD;
825807
}
808+
826809
//
827810
// document::object::iterator inline implementation
828811
//
@@ -1021,8 +1004,8 @@ inline document::element_result document::element::at(size_t index) const noexce
10211004
inline document::element_result document::element::at_key(std::string_view key) const noexcept {
10221005
return as_object().at_key(key);
10231006
}
1024-
inline document::element_result document::element::at_key(const char *key) const noexcept {
1025-
return as_object().at_key(key);
1007+
inline document::element_result document::element::at_key_case_insensitive(std::string_view key) const noexcept {
1008+
return as_object().at_key_case_insensitive(key);
10261009
}
10271010

10281011
//

0 commit comments

Comments
 (0)
0