Skip to content

Commit 748df8d

Browse files
committed
Use string_view instead of string/char* where possible
1 parent 5ad4050 commit 748df8d

File tree

4 files changed

+69
-103
lines changed

4 files changed

+69
-103
lines changed

include/simdjson/document.h

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class document::element : protected internal::tape_ref {
326326
* - INCORRECT_TYPE if a non-integer is used to access an array
327327
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
328328
*/
329-
inline element_result operator[](std::string_view json_pointer) const noexcept;
329+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
330330

331331
/**
332332
* Get the value associated with the given JSON pointer.
@@ -360,7 +360,7 @@ class document::element : protected internal::tape_ref {
360360
* - INCORRECT_TYPE if a non-integer is used to access an array
361361
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
362362
*/
363-
inline element_result at(std::string_view json_pointer) const noexcept;
363+
inline element_result at(const std::string_view &json_pointer) const noexcept;
364364

365365
/**
366366
* Get the value at the given index.
@@ -382,21 +382,17 @@ class document::element : protected internal::tape_ref {
382382
* @return The value associated with this field, or:
383383
* - NO_SUCH_FIELD if the field does not exist in the object
384384
*/
385-
inline element_result at_key(std::string_view s) const noexcept;
385+
inline element_result at_key(const std::string_view &key) const noexcept;
386386

387387
/**
388-
* Get the value associated with the given key.
389-
*
390-
* Note: The key will be matched against **unescaped** JSON:
388+
* Get the value associated with the given key in a case-insensitive manner.
391389
*
392-
* document::parser parser;
393-
* parser.parse(R"({ "a\n": 1 })")["a\n"].as_uint64_t().value == 1
394-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].as_uint64_t().error == NO_SUCH_FIELD
390+
* Note: The key will be matched against **unescaped** JSON.
395391
*
396392
* @return The value associated with this field, or:
397393
* - NO_SUCH_FIELD if the field does not exist in the object
398394
*/
399-
inline element_result at_key(const char *s) const noexcept;
395+
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
400396

401397
/** @private for debugging. Prints out the root element. */
402398
inline bool dump_raw_tape(std::ostream &out) const noexcept;
@@ -467,7 +463,7 @@ class document::array : protected internal::tape_ref {
467463
* - INCORRECT_TYPE if a non-integer is used to access an array
468464
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
469465
*/
470-
inline element_result operator[](std::string_view json_pointer) const noexcept;
466+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
471467

472468
/**
473469
* Get the value associated with the given JSON pointer.
@@ -499,7 +495,7 @@ class document::array : protected internal::tape_ref {
499495
* - INCORRECT_TYPE if a non-integer is used to access an array
500496
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
501497
*/
502-
inline element_result at(std::string_view json_pointer) const noexcept;
498+
inline element_result at(const std::string_view &json_pointer) const noexcept;
503499

504500
/**
505501
* Get the value at the given index.
@@ -587,7 +583,7 @@ class document::object : protected internal::tape_ref {
587583
* - INCORRECT_TYPE if a non-integer is used to access an array
588584
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
589585
*/
590-
inline element_result operator[](std::string_view json_pointer) const noexcept;
586+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
591587

592588
/**
593589
* Get the value associated with the given JSON pointer.
@@ -619,7 +615,7 @@ class document::object : protected internal::tape_ref {
619615
* - INCORRECT_TYPE if a non-integer is used to access an array
620616
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
621617
*/
622-
inline element_result at(std::string_view json_pointer) const noexcept;
618+
inline element_result at(const std::string_view &json_pointer) const noexcept;
623619

624620
/**
625621
* Get the value associated with the given key.
@@ -633,28 +629,8 @@ class document::object : protected internal::tape_ref {
633629
* @return The value associated with this field, or:
634630
* - NO_SUCH_FIELD if the field does not exist in the object
635631
*/
636-
inline element_result at_key(std::string_view s) const noexcept;
632+
inline element_result at_key(const std::string_view &key) const noexcept;
637633

638-
/**
639-
* Get the value associated with the given key.
640-
*
641-
* Note: The key will be matched against **unescaped** JSON.
642-
*
643-
* @return The value associated with this field, or:
644-
* - NO_SUCH_FIELD if the field does not exist in the object
645-
*/
646-
inline element_result at_key(const char *s) const noexcept;
647-
648-
/**
649-
* Get the value associated with the given key, the provided key is
650-
* considered to have length characters.
651-
*
652-
* Note: The key will be matched against **unescaped** JSON.
653-
*
654-
* @return The value associated with this field, or:
655-
* - NO_SUCH_FIELD if the field does not exist in the object
656-
*/
657-
inline element_result at_key(const char *s, size_t length) const noexcept;
658634
/**
659635
* Get the value associated with the given key in a case-insensitive manner.
660636
*
@@ -663,7 +639,7 @@ class document::object : protected internal::tape_ref {
663639
* @return The value associated with this field, or:
664640
* - NO_SUCH_FIELD if the field does not exist in the object
665641
*/
666-
inline element_result at_key_case_insensitive(const char *s) const noexcept;
642+
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
667643

668644
private:
669645
really_inline object(const document *_doc, size_t _json_index) noexcept;
@@ -682,7 +658,7 @@ class document::key_value_pair {
682658
document::element value;
683659

684660
private:
685-
really_inline key_value_pair(std::string_view _key, document::element _value) noexcept;
661+
really_inline key_value_pair(const std::string_view &_key, document::element _value) noexcept;
686662
friend class document::object;
687663
};
688664

@@ -704,12 +680,12 @@ class document::element_result : public simdjson_result<document::element> {
704680
inline array_result as_array() const noexcept;
705681
inline object_result as_object() const noexcept;
706682

707-
inline element_result operator[](std::string_view json_pointer) const noexcept;
683+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
708684
inline element_result operator[](const char *json_pointer) const noexcept;
709-
inline element_result at(std::string_view json_pointer) const noexcept;
685+
inline element_result at(const std::string_view &json_pointer) const noexcept;
710686
inline element_result at(size_t index) const noexcept;
711-
inline element_result at_key(std::string_view key) const noexcept;
712-
inline element_result at_key(const char *key) const noexcept;
687+
inline element_result at_key(const std::string_view &key) const noexcept;
688+
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
713689

714690
#if SIMDJSON_EXCEPTIONS
715691
inline operator bool() const noexcept(false);
@@ -730,9 +706,9 @@ class document::array_result : public simdjson_result<document::array> {
730706
really_inline array_result(array value) noexcept;
731707
really_inline array_result(error_code error) noexcept;
732708

733-
inline element_result operator[](std::string_view json_pointer) const noexcept;
709+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
734710
inline element_result operator[](const char *json_pointer) const noexcept;
735-
inline element_result at(std::string_view json_pointer) const noexcept;
711+
inline element_result at(const std::string_view &json_pointer) const noexcept;
736712
inline element_result at(size_t index) const noexcept;
737713

738714
#if SIMDJSON_EXCEPTIONS
@@ -748,11 +724,11 @@ class document::object_result : public simdjson_result<document::object> {
748724
really_inline object_result(object value) noexcept;
749725
really_inline object_result(error_code error) noexcept;
750726

751-
inline element_result operator[](std::string_view json_pointer) const noexcept;
727+
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
752728
inline element_result operator[](const char *json_pointer) const noexcept;
753-
inline element_result at(std::string_view json_pointer) const noexcept;
754-
inline element_result at_key(std::string_view key) const noexcept;
755-
inline element_result at_key(const char *key) const noexcept;
729+
inline element_result at(const std::string_view &json_pointer) const noexcept;
730+
inline element_result at_key(const std::string_view &key) const noexcept;
731+
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
756732

757733
#if SIMDJSON_EXCEPTIONS
758734
inline object::iterator begin() const noexcept(false);
@@ -828,7 +804,7 @@ class document::parser {
828804
* - CAPACITY if the parser does not have enough capacity and len > max_capacity.
829805
* - other json errors if parsing fails.
830806
*/
831-
inline element_result load(const std::string& path) noexcept;
807+
inline element_result load(const std::string &path) noexcept;
832808

833809
/**
834810
* Load a file containing many JSON documents.
@@ -885,7 +861,7 @@ class document::parser {
885861
* - CAPACITY if the parser does not have enough capacity and batch_size > max_capacity.
886862
* - other json errors if parsing fails.
887863
*/
888-
inline document::stream load_many(const std::string& path, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
864+
inline document::stream load_many(const std::string &path, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
889865

890866
/**
891867
* Parse a JSON document and return a temporary reference to it.

include/simdjson/implementation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class available_implementation_list {
164164
* @param name the implementation to find, e.g. "westmere", "haswell", "arm64"
165165
* @return the implementation, or nullptr if the parse failed.
166166
*/
167-
const implementation * operator[](const std::string& name) const noexcept {
167+
const implementation * operator[](const std::string_view &name) const noexcept {
168168
for (const implementation * impl : *this) {
169169
if (impl->name() == name) { return impl; }
170170
}

0 commit comments

Comments
 (0)