Skip to content

Commit c14b2fb

Browse files
committed
Remove const char* variants for at_key()
- Remove const char * variants for at_key(), string_view covers them - Add at_key_case_insensitive variants on *_result - Add at(), at_key(), at_key_case_insensitive() tests
1 parent f0f111b commit c14b2fb

File tree

3 files changed

+80
-159
lines changed

3 files changed

+80
-159
lines changed

include/simdjson/document.h

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ 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+
183197
std::unique_ptr<uint64_t[]> tape;
184198
std::unique_ptr<uint8_t[]> string_buf;// should be at least byte_capacity
185199

@@ -635,14 +649,18 @@ class document::element : protected internal::tape_ref {
635649
inline element_result at_key(std::string_view s) const noexcept;
636650

637651
/**
638-
* Get the value associated with the given key in a case-insensitive manner.
652+
* Get the value associated with the given key.
639653
*
640-
* Note: The key will be matched against **unescaped** JSON.
654+
* Note: The key will be matched against **unescaped** JSON:
655+
*
656+
* document::parser parser;
657+
* parser.parse(R"({ "a\n": 1 })")["a\n"].as_uint64_t().value == 1
658+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].as_uint64_t().error == NO_SUCH_FIELD
641659
*
642660
* @return The value associated with this field, or:
643661
* - NO_SUCH_FIELD if the field does not exist in the object
644662
*/
645-
inline element_result at_key_case_insensitive(std::string_view s) const noexcept;
663+
inline element_result at_key(const char *s) const noexcept;
646664

647665
private:
648666
really_inline element(const document *_doc, size_t _json_index) noexcept;
@@ -876,7 +894,7 @@ class document::object : protected internal::tape_ref {
876894
* @return The value associated with this field, or:
877895
* - NO_SUCH_FIELD if the field does not exist in the object
878896
*/
879-
inline element_result at_key(std::string_view key) const noexcept;
897+
inline element_result at_key(std::string_view s) const noexcept;
880898

881899
/**
882900
* Get the value associated with the given key.
@@ -886,18 +904,18 @@ class document::object : protected internal::tape_ref {
886904
* @return The value associated with this field, or:
887905
* - NO_SUCH_FIELD if the field does not exist in the object
888906
*/
889-
inline element_result at_key(const char *key) const noexcept;
907+
inline element_result at_key(const char *s) const noexcept;
890908

891909
/**
892-
* Get the value associated with the given key in a case-insensitive manner.
910+
* Get the value associated with the given key, the provided key is
911+
* considered to have length characters.
893912
*
894913
* Note: The key will be matched against **unescaped** JSON.
895914
*
896915
* @return The value associated with this field, or:
897916
* - NO_SUCH_FIELD if the field does not exist in the object
898917
*/
899-
inline element_result at_key_case_insensitive(std::string_view key) const noexcept;
900-
918+
inline element_result at_key(const char *s, size_t length) const noexcept;
901919
/**
902920
* Get the value associated with the given key in a case-insensitive manner.
903921
*
@@ -906,7 +924,7 @@ class document::object : protected internal::tape_ref {
906924
* @return The value associated with this field, or:
907925
* - NO_SUCH_FIELD if the field does not exist in the object
908926
*/
909-
inline element_result at_key_case_insensitive(const char *key) const noexcept;
927+
inline element_result at_key_case_insensitive(const char *s) const noexcept;
910928

911929
private:
912930
really_inline object(const document *_doc, size_t _json_index) noexcept;
@@ -954,8 +972,6 @@ class document::element_result : public simdjson_result<document::element> {
954972
inline element_result at(size_t index) const noexcept;
955973
inline element_result at_key(std::string_view key) const noexcept;
956974
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;
959975

960976
#if SIMDJSON_EXCEPTIONS
961977
inline operator bool() const noexcept(false);
@@ -998,7 +1014,7 @@ class document::object_result : public simdjson_result<document::object> {
9981014
inline element_result operator[](const char *json_pointer) const noexcept;
9991015
inline element_result at(std::string_view json_pointer) const noexcept;
10001016
inline element_result at_key(std::string_view key) const noexcept;
1001-
inline element_result at_key_case_insensitive(std::string_view key) const noexcept;
1017+
inline element_result at_key(const char *key) const noexcept;
10021018

10031019
#if SIMDJSON_EXCEPTIONS
10041020
inline object::iterator begin() const noexcept(false);

include/simdjson/inline/document.h

Lines changed: 30 additions & 11 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_case_insensitive(std::string_view key) const noexcept {
78+
inline document::element_result document::element_result::at_key(const char *key) const noexcept {
7979
if (error()) { return *this; }
80-
return first.at_key_case_insensitive(key);
80+
return first.at_key(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_case_insensitive(std::string_view key) const noexcept {
170+
inline document::element_result document::object_result::at_key(const char *key) const noexcept {
171171
if (error()) { return error(); }
172-
return first.at_key_case_insensitive(key);
172+
return first.at_key(key);
173173
}
174174

175175
#if SIMDJSON_EXCEPTIONS
@@ -227,6 +227,9 @@ 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+
}
230233
inline document::element_result document::operator[](std::string_view json_pointer) const noexcept {
231234
return at(json_pointer);
232235
}
@@ -782,6 +785,16 @@ inline document::element_result document::object::at(std::string_view json_point
782785

783786
return child;
784787
}
788+
inline document::element_result document::object::at_key(const char *key, size_t length) const noexcept {
789+
iterator end_field = end();
790+
for (iterator field = begin(); field != end_field; ++field) {
791+
std::string_view v{field.key()};
792+
if ((v.size() == length) && (!memcmp(v.data(), key, length))) {
793+
return field.value();
794+
}
795+
}
796+
return NO_SUCH_FIELD;
797+
}
785798
inline document::element_result document::object::at_key(std::string_view key) const noexcept {
786799
iterator end_field = end();
787800
for (iterator field = begin(); field != end_field; ++field) {
@@ -791,21 +804,27 @@ inline document::element_result document::object::at_key(std::string_view key) c
791804
}
792805
return NO_SUCH_FIELD;
793806
}
807+
inline document::element_result document::object::at_key(const char *key) const noexcept {
808+
iterator end_field = end();
809+
for (iterator field = begin(); field != end_field; ++field) {
810+
if (!strcmp(key, field.key_c_str())) {
811+
return field.value();
812+
}
813+
}
814+
return NO_SUCH_FIELD;
815+
}
794816
// In case you wonder why we need this, please see
795817
// https://github.com/simdjson/simdjson/issues/323
796818
// People do seek keys in a case-insensitive manner.
797-
inline document::element_result document::object::at_key_case_insensitive(std::string_view key) const noexcept {
819+
inline document::element_result document::object::at_key_case_insensitive(const char *key) const noexcept {
798820
iterator end_field = end();
799821
for (iterator field = begin(); field != end_field; ++field) {
800-
if (std::equal(key.begin(), key.end(), field.key().begin(), [](char a, char b) {
801-
return std::tolower(a) == std::tolower(b);
802-
})) {
822+
if (!simdjson_strcasecmp(key, field.key_c_str())) {
803823
return field.value();
804824
}
805825
}
806826
return NO_SUCH_FIELD;
807827
}
808-
809828
//
810829
// document::object::iterator inline implementation
811830
//
@@ -1004,8 +1023,8 @@ inline document::element_result document::element::at(size_t index) const noexce
10041023
inline document::element_result document::element::at_key(std::string_view key) const noexcept {
10051024
return as_object().at_key(key);
10061025
}
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);
1026+
inline document::element_result document::element::at_key(const char *key) const noexcept {
1027+
return as_object().at_key(key);
10091028
}
10101029

10111030
//

0 commit comments

Comments
 (0)