Use string_view instead of string/char* where possible

This commit is contained in:
John Keiser 2020-03-27 12:33:32 -07:00
parent 5ad405006c
commit 748df8d109
4 changed files with 70 additions and 104 deletions

View File

@ -326,7 +326,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
/**
* Get the value associated with the given JSON pointer.
@ -360,7 +360,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
/**
* Get the value at the given index.
@ -382,21 +382,17 @@ public:
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key(std::string_view s) const noexcept;
inline element_result at_key(const std::string_view &key) const noexcept;
/**
* Get the value associated with the given key.
* Get the value associated with the given key in a case-insensitive manner.
*
* Note: The key will be matched against **unescaped** JSON:
*
* document::parser parser;
* parser.parse(R"({ "a\n": 1 })")["a\n"].as_uint64_t().value == 1
* parser.parse(R"({ "a\n": 1 })")["a\\n"].as_uint64_t().error == NO_SUCH_FIELD
* Note: The key will be matched against **unescaped** JSON.
*
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key(const char *s) const noexcept;
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
/** @private for debugging. Prints out the root element. */
inline bool dump_raw_tape(std::ostream &out) const noexcept;
@ -467,7 +463,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
/**
* Get the value associated with the given JSON pointer.
@ -499,7 +495,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
/**
* Get the value at the given index.
@ -587,7 +583,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
/**
* Get the value associated with the given JSON pointer.
@ -619,7 +615,7 @@ public:
* - INCORRECT_TYPE if a non-integer is used to access an array
* - INVALID_JSON_POINTER if the JSON pointer is invalid and cannot be parsed
*/
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
/**
* Get the value associated with the given key.
@ -633,28 +629,8 @@ public:
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key(std::string_view s) const noexcept;
inline element_result at_key(const std::string_view &key) const noexcept;
/**
* Get the value associated with the given key.
*
* Note: The key will be matched against **unescaped** JSON.
*
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key(const char *s) const noexcept;
/**
* Get the value associated with the given key, the provided key is
* considered to have length characters.
*
* Note: The key will be matched against **unescaped** JSON.
*
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key(const char *s, size_t length) const noexcept;
/**
* Get the value associated with the given key in a case-insensitive manner.
*
@ -663,7 +639,7 @@ public:
* @return The value associated with this field, or:
* - NO_SUCH_FIELD if the field does not exist in the object
*/
inline element_result at_key_case_insensitive(const char *s) const noexcept;
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
private:
really_inline object(const document *_doc, size_t _json_index) noexcept;
@ -682,7 +658,7 @@ public:
document::element value;
private:
really_inline key_value_pair(std::string_view _key, document::element _value) noexcept;
really_inline key_value_pair(const std::string_view &_key, document::element _value) noexcept;
friend class document::object;
};
@ -704,12 +680,12 @@ public:
inline array_result as_array() const noexcept;
inline object_result as_object() const noexcept;
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
inline element_result operator[](const char *json_pointer) const noexcept;
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
inline element_result at(size_t index) const noexcept;
inline element_result at_key(std::string_view key) const noexcept;
inline element_result at_key(const char *key) const noexcept;
inline element_result at_key(const std::string_view &key) const noexcept;
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
#if SIMDJSON_EXCEPTIONS
inline operator bool() const noexcept(false);
@ -730,9 +706,9 @@ public:
really_inline array_result(array value) noexcept;
really_inline array_result(error_code error) noexcept;
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
inline element_result operator[](const char *json_pointer) const noexcept;
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
inline element_result at(size_t index) const noexcept;
#if SIMDJSON_EXCEPTIONS
@ -748,11 +724,11 @@ public:
really_inline object_result(object value) noexcept;
really_inline object_result(error_code error) noexcept;
inline element_result operator[](std::string_view json_pointer) const noexcept;
inline element_result operator[](const std::string_view &json_pointer) const noexcept;
inline element_result operator[](const char *json_pointer) const noexcept;
inline element_result at(std::string_view json_pointer) const noexcept;
inline element_result at_key(std::string_view key) const noexcept;
inline element_result at_key(const char *key) const noexcept;
inline element_result at(const std::string_view &json_pointer) const noexcept;
inline element_result at_key(const std::string_view &key) const noexcept;
inline element_result at_key_case_insensitive(const std::string_view &key) const noexcept;
#if SIMDJSON_EXCEPTIONS
inline object::iterator begin() const noexcept(false);
@ -828,7 +804,7 @@ public:
* - CAPACITY if the parser does not have enough capacity and len > max_capacity.
* - other json errors if parsing fails.
*/
inline element_result load(const std::string& path) noexcept;
inline element_result load(const std::string &path) noexcept;
/**
* Load a file containing many JSON documents.
@ -885,7 +861,7 @@ public:
* - CAPACITY if the parser does not have enough capacity and batch_size > max_capacity.
* - other json errors if parsing fails.
*/
inline document::stream load_many(const std::string& path, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
inline document::stream load_many(const std::string &path, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
/**
* Parse a JSON document and return a temporary reference to it.

View File

@ -164,7 +164,7 @@ public:
* @param name the implementation to find, e.g. "westmere", "haswell", "arm64"
* @return the implementation, or nullptr if the parse failed.
*/
const implementation * operator[](const std::string& name) const noexcept {
const implementation * operator[](const std::string_view &name) const noexcept {
for (const implementation * impl : *this) {
if (impl->name() == name) { return impl; }
}

View File

@ -56,28 +56,29 @@ inline document::object_result document::element_result::as_object() const noexc
return first.as_object();
}
inline document::element_result document::element_result::operator[](std::string_view key) const noexcept {
inline document::element_result document::element_result::operator[](const std::string_view &json_pointer) const noexcept {
if (error()) { return *this; }
return first[key];
return first[json_pointer];
}
inline document::element_result document::element_result::operator[](const char *json_pointer) const noexcept {
return (*this)[std::string_view(json_pointer)];
}
inline document::element_result document::element_result::at(std::string_view key) const noexcept {
if (error()) { return *this; }
return first.at(key);
return first[json_pointer];
}
inline document::element_result document::element_result::at(const std::string_view &json_pointer) const noexcept {
if (error()) { return *this; }
return first.at(json_pointer);
}
inline document::element_result document::element_result::at(size_t index) const noexcept {
if (error()) { return *this; }
return first.at(index);
}
inline document::element_result document::element_result::at_key(std::string_view key) const noexcept {
inline document::element_result document::element_result::at_key(const std::string_view &key) const noexcept {
if (error()) { return *this; }
return first.at_key(key);
}
inline document::element_result document::element_result::at_key(const char *key) const noexcept {
inline document::element_result document::element_result::at_key_case_insensitive(const std::string_view &key) const noexcept {
if (error()) { return *this; }
return first.at_key(key);
return first.at_key_case_insensitive(key);
}
#if SIMDJSON_EXCEPTIONS
@ -129,14 +130,15 @@ inline document::array::iterator document::array_result::end() const noexcept(fa
#endif // SIMDJSON_EXCEPTIONS
inline document::element_result document::array_result::operator[](std::string_view json_pointer) const noexcept {
inline document::element_result document::array_result::operator[](const std::string_view &json_pointer) const noexcept {
if (error()) { return error(); }
return first.at(json_pointer);
}
inline document::element_result document::array_result::operator[](const char *json_pointer) const noexcept {
return (*this)[std::string_view(json_pointer)];
if (error()) { return error(); }
return first.at(json_pointer);
}
inline document::element_result document::array_result::at(std::string_view json_pointer) const noexcept {
inline document::element_result document::array_result::at(const std::string_view &json_pointer) const noexcept {
if (error()) { return error(); }
return first.at(json_pointer);
}
@ -152,24 +154,25 @@ really_inline document::object_result::object_result() noexcept : simdjson_resul
really_inline document::object_result::object_result(object value) noexcept : simdjson_result<object>((object&&)value) {}
really_inline document::object_result::object_result(error_code error) noexcept : simdjson_result<object>(error) {}
inline document::element_result document::object_result::operator[](std::string_view json_pointer) const noexcept {
inline document::element_result document::object_result::operator[](const std::string_view &json_pointer) const noexcept {
if (error()) { return error(); }
return first[json_pointer];
}
inline document::element_result document::object_result::operator[](const char *json_pointer) const noexcept {
return (*this)[std::string_view(json_pointer)];
if (error()) { return error(); }
return first[json_pointer];
}
inline document::element_result document::object_result::at(std::string_view json_pointer) const noexcept {
inline document::element_result document::object_result::at(const std::string_view &json_pointer) const noexcept {
if (error()) { return error(); }
return first.at(json_pointer);
}
inline document::element_result document::object_result::at_key(std::string_view key) const noexcept {
inline document::element_result document::object_result::at_key(const std::string_view &key) const noexcept {
if (error()) { return error(); }
return first.at_key(key);
}
inline document::element_result document::object_result::at_key(const char *key) const noexcept {
inline document::element_result document::object_result::at_key_case_insensitive(const std::string_view &key) const noexcept {
if (error()) { return error(); }
return first.at_key(key);
return first.at_key_case_insensitive(key);
}
#if SIMDJSON_EXCEPTIONS
@ -584,7 +587,7 @@ inline document::array::iterator document::array::end() const noexcept {
return iterator(doc, after_element() - 1);
}
inline document::element_result document::array::at(std::string_view json_pointer) const noexcept {
inline document::element_result document::array::at(const std::string_view &json_pointer) const noexcept {
// - means "the append position" or "the element after the end of the array"
// We don't support this, because we're returning a real element, not a position.
if (json_pointer == "-") { return INDEX_OUT_OF_BOUNDS; }
@ -648,13 +651,13 @@ inline document::object::iterator document::object::end() const noexcept {
return iterator(doc, after_element() - 1);
}
inline document::element_result document::object::operator[](std::string_view json_pointer) const noexcept {
inline document::element_result document::object::operator[](const std::string_view &json_pointer) const noexcept {
return at(json_pointer);
}
inline document::element_result document::object::operator[](const char *json_pointer) const noexcept {
return (*this)[std::string_view(json_pointer)];
return at(json_pointer);
}
inline document::element_result document::object::at(std::string_view json_pointer) const noexcept {
inline document::element_result document::object::at(const std::string_view &json_pointer) const noexcept {
size_t slash = json_pointer.find('/');
std::string_view key = json_pointer.substr(0, slash);
@ -691,17 +694,7 @@ inline document::element_result document::object::at(std::string_view json_point
return child;
}
inline document::element_result document::object::at_key(const char *key, size_t length) const noexcept {
iterator end_field = end();
for (iterator field = begin(); field != end_field; ++field) {
std::string_view v{field.key()};
if ((v.size() == length) && (!memcmp(v.data(), key, length))) {
return field.value();
}
}
return NO_SUCH_FIELD;
}
inline document::element_result document::object::at_key(std::string_view key) const noexcept {
inline document::element_result document::object::at_key(const std::string_view &key) const noexcept {
iterator end_field = end();
for (iterator field = begin(); field != end_field; ++field) {
if (key == field.key()) {
@ -710,27 +703,24 @@ inline document::element_result document::object::at_key(std::string_view key) c
}
return NO_SUCH_FIELD;
}
inline document::element_result document::object::at_key(const char *key) const noexcept {
iterator end_field = end();
for (iterator field = begin(); field != end_field; ++field) {
if (!strcmp(key, field.key_c_str())) {
return field.value();
}
}
return NO_SUCH_FIELD;
}
// In case you wonder why we need this, please see
// https://github.com/simdjson/simdjson/issues/323
// People do seek keys in a case-insensitive manner.
inline document::element_result document::object::at_key_case_insensitive(const char *key) const noexcept {
inline document::element_result document::object::at_key_case_insensitive(const std::string_view &key) const noexcept {
iterator end_field = end();
for (iterator field = begin(); field != end_field; ++field) {
if (!simdjson_strcasecmp(key, field.key_c_str())) {
return field.value();
auto field_key = field.key();
if (key.length() == field_key.length()) {
bool equal = true;
for (size_t i=0; i<field_key.length(); i++) {
equal = equal && std::tolower(key[i]) != std::tolower(field_key[i]);
}
if (equal) { return field.value(); }
}
}
return NO_SUCH_FIELD;
}
//
// document::object::iterator inline implementation
//
@ -764,7 +754,7 @@ inline document::element document::object::iterator::value() const noexcept {
//
// document::key_value_pair inline implementation
//
inline document::key_value_pair::key_value_pair(std::string_view _key, element _value) noexcept :
inline document::key_value_pair::key_value_pair(const std::string_view &_key, element _value) noexcept :
key(_key), value(_value) {}
//
@ -907,13 +897,13 @@ inline document::object_result document::element::as_object() const noexcept {
return INCORRECT_TYPE;
}
}
inline document::element_result document::element::operator[](std::string_view json_pointer) const noexcept {
inline document::element_result document::element::operator[](const std::string_view &json_pointer) const noexcept {
return at(json_pointer);
}
inline document::element_result document::element::operator[](const char *json_pointer) const noexcept {
return (*this)[std::string_view(json_pointer)];
return at(json_pointer);
}
inline document::element_result document::element::at(std::string_view json_pointer) const noexcept {
inline document::element_result document::element::at(const std::string_view &json_pointer) const noexcept {
switch (type()) {
case internal::tape_type::START_OBJECT:
return object(doc, json_index).at(json_pointer);
@ -926,11 +916,11 @@ inline document::element_result document::element::at(std::string_view json_poin
inline document::element_result document::element::at(size_t index) const noexcept {
return as_array().at(index);
}
inline document::element_result document::element::at_key(std::string_view key) const noexcept {
inline document::element_result document::element::at_key(const std::string_view &key) const noexcept {
return as_object().at_key(key);
}
inline document::element_result document::element::at_key(const char *key) const noexcept {
return as_object().at_key(key);
inline document::element_result document::element::at_key_case_insensitive(const std::string_view &key) const noexcept {
return as_object().at_key_case_insensitive(key);
}
inline bool document::element::dump_raw_tape(std::ostream &out) const noexcept {

View File

@ -15,8 +15,8 @@ namespace simdjson {
#if SIMDJSON_EXCEPTIONS
inline padded_string get_corpus(const std::string &filename) {
return padded_string::load(filename);
inline padded_string get_corpus(const char *path) {
return padded_string::load(path);
}
#endif // SIMDJSON_EXCEPTIONS