simdjson  3.11.0
Ridiculously Fast JSON
simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string Class Reference

A string escaped per JSON rules, terminated with quote ("). More...

Public Member Functions

simdjson_inline raw_json_string () noexcept=default
 Create a new invalid raw_json_string. More...
 
simdjson_inline raw_json_string (const uint8_t *_buf) noexcept
 Create a new invalid raw_json_string pointed at the given location in the JSON. More...
 
simdjson_inline const char * raw () const noexcept
 Get the raw pointer to the beginning of the string in the JSON (just after the "). More...
 
simdjson_inline bool unsafe_is_equal (size_t length, std::string_view target) const noexcept
 This compares the current instance to the std::string_view target: returns true if they are byte-by-byte equal (no escaping is done) on target.size() characters, and if the raw_json_string instance has a quote character at byte index target.size(). More...
 
simdjson_inline bool unsafe_is_equal (std::string_view target) const noexcept
 This compares the current instance to the std::string_view target: returns true if they are byte-by-byte equal (no escaping is done). More...
 
simdjson_inline bool unsafe_is_equal (const char *target) const noexcept
 This compares the current instance to the C string target: returns true if they are byte-by-byte equal (no escaping is done). More...
 
simdjson_inline bool is_equal (std::string_view target) const noexcept
 This compares the current instance to the std::string_view target: returns true if they are byte-by-byte equal (no escaping is done).
 
simdjson_inline bool is_equal (const char *target) const noexcept
 This compares the current instance to the C string target: returns true if they are byte-by-byte equal (no escaping is done).
 

Static Public Member Functions

static simdjson_inline bool is_free_from_unescaped_quote (std::string_view target) noexcept
 Returns true if target is free from unescaped quote. More...
 
static simdjson_inline bool is_free_from_unescaped_quote (const char *target) noexcept
 

Detailed Description

A string escaped per JSON rules, terminated with quote (").

They are used to represent unescaped keys inside JSON documents.

(In other words, a pointer to the beginning of a string, just after the start quote, inside a JSON file.)

This class is deliberately simplistic and has little functionality. You can compare a raw_json_string instance with an unescaped C string, but that is nearly all you can do.

The raw_json_string is unescaped. If you wish to write an unescaped version of it to your own buffer, you may do so using the parser.unescape(string, buff) method, using an ondemand::parser instance. Doing so requires you to have a sufficiently large buffer.

The raw_json_string instances originate typically from field instance which in turn represent key-value pairs from object instances. From a field instance, you get the raw_json_string instance by calling key(). You can, if you want a more usable string_view instance, call the unescaped_key() method on the field instance. You may also create a raw_json_string from any other string value, with the value.get_raw_json_string() method. Again, you can get a more usable string_view instance by calling get_string().

Definition at line 36 of file raw_json_string.h.

Constructor & Destructor Documentation

◆ raw_json_string() [1/2]

simdjson_inline simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::raw_json_string ( )
defaultnoexcept

Create a new invalid raw_json_string.

Exists so you can declare a variable and later assign to it before use.

◆ raw_json_string() [2/2]

simdjson_inline simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::raw_json_string ( const uint8_t *  _buf)
noexcept

Create a new invalid raw_json_string pointed at the given location in the JSON.

The given location must be just after the beginning quote (") in the JSON file.

It must be terminated by a ", and be a valid JSON string.

Definition at line 16 of file raw_json_string-inl.h.

Member Function Documentation

◆ is_free_from_unescaped_quote()

simdjson_inline bool simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::is_free_from_unescaped_quote ( std::string_view  target)
staticnoexcept

Returns true if target is free from unescaped quote.

If target is known at compile-time, we might expect the computation to happen at compile time with many compilers (not all!).

Definition at line 21 of file raw_json_string-inl.h.

◆ raw()

simdjson_inline const char * simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::raw ( ) const
noexcept

Get the raw pointer to the beginning of the string in the JSON (just after the ").

It is possible for this function to return a null pointer if the instance has outlived its existence.

Definition at line 18 of file raw_json_string-inl.h.

◆ unsafe_is_equal() [1/3]

simdjson_inline bool simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::unsafe_is_equal ( const char *  target) const
noexcept

This compares the current instance to the C string target: returns true if they are byte-by-byte equal (no escaping is done).

The provided C string should not contain an unescaped quote character: the caller is responsible for this check. See is_free_from_unescaped_quote.

If target is a compile-time constant, and your compiler likes you, you should be able to do the following without performance penalty...

static_assert(raw_json_string::is_free_from_unescaped_quote(target), ""); s.unsafe_is_equal(target);

Definition at line 102 of file raw_json_string-inl.h.

◆ unsafe_is_equal() [2/3]

simdjson_inline bool simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::unsafe_is_equal ( size_t  length,
std::string_view  target 
) const
noexcept

This compares the current instance to the std::string_view target: returns true if they are byte-by-byte equal (no escaping is done) on target.size() characters, and if the raw_json_string instance has a quote character at byte index target.size().

We never read more than length + 1 bytes in the raw_json_string instance. If length is smaller than target.size(), this will return false.

The std::string_view instance may contain any characters. However, the caller is responsible for setting length so that length bytes may be read in the raw_json_string.

Performance: the comparison may be done using memcmp which may be efficient for long strings.

Definition at line 58 of file raw_json_string-inl.h.

◆ unsafe_is_equal() [3/3]

simdjson_inline bool simdjson::SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string::unsafe_is_equal ( std::string_view  target) const
noexcept

This compares the current instance to the std::string_view target: returns true if they are byte-by-byte equal (no escaping is done).

The std::string_view instance should not contain unescaped quote characters: the caller is responsible for this check. See is_free_from_unescaped_quote.

Performance: the comparison is done byte-by-byte which might be inefficient for long strings.

If target is a compile-time constant, and your compiler likes you, you should be able to do the following without performance penalty...

static_assert(raw_json_string::is_free_from_unescaped_quote(target), ""); s.unsafe_is_equal(target);

Definition at line 63 of file raw_json_string-inl.h.


The documentation for this class was generated from the following files: