1#ifndef SIMDJSON_GENERIC_ONDEMAND_RAW_JSON_STRING_INL_H
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_RAW_JSON_STRING_INL_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/ondemand/raw_json_string.h"
7#include "simdjson/generic/ondemand/json_iterator-inl.h"
8#include "simdjson/generic/implementation_simdjson_result_base-inl.h"
13namespace SIMDJSON_IMPLEMENTATION {
19 return reinterpret_cast<const char *
>(buf);
23 return reinterpret_cast<const char *
>(buf)[i];
28 while(pos < target.size()) {
29 pos = target.find(
'"', pos);
30 if(pos == std::string_view::npos) {
return true; }
31 if(pos != 0 && target[pos-1] !=
'\\') {
return false; }
32 if(pos > 1 && target[pos-2] ==
'\\') {
33 size_t backslash_count{2};
34 for(
size_t i = 3; i <= pos; i++) {
35 if(target[pos-i] ==
'\\') { backslash_count++; }
38 if(backslash_count % 2 == 0) {
return false; }
48 const char * result = strchr(target+pos,
'"');
49 if(result ==
nullptr) {
return true; }
50 pos = result - target;
51 if(pos != 0 && target[pos-1] !=
'\\') {
return false; }
52 if(pos > 1 && target[pos-2] ==
'\\') {
53 size_t backslash_count{2};
54 for(
size_t i = 3; i <= pos; i++) {
55 if(target[pos-i] ==
'\\') { backslash_count++; }
58 if(backslash_count % 2 == 0) {
return false; }
68 return (length >= target.size()) && (raw()[target.size()] ==
'"') && !memcmp(raw(), target.data(), target.size());
75 return (raw()[target.size()] ==
'"') && !memcmp(raw(), target.data(), target.size());
77 const char * r{raw()};
79 for(;pos < target.size();pos++) {
80 if(r[pos] != target[pos]) {
return false; }
82 if(r[pos] !=
'"') {
return false; }
87 const char * r{raw()};
90 for(;pos < target.size();pos++) {
91 if(r[pos] != target[pos]) {
return false; }
95 if((target[pos] ==
'"') && !escaping) {
99 }
else if(target[pos] ==
'\\') {
100 escaping = !escaping;
105 if(r[pos] !=
'"') {
return false; }
113 const char * r{raw()};
115 for(;target[pos];pos++) {
116 if(r[pos] != target[pos]) {
return false; }
118 if(r[pos] !=
'"') {
return false; }
125 const char * r{raw()};
127 bool escaping{
false};
128 for(;target[pos];pos++) {
129 if(r[pos] != target[pos]) {
return false; }
133 if((target[pos] ==
'"') && !escaping) {
137 }
else if(target[pos] ==
'\\') {
138 escaping = !escaping;
143 if(r[pos] !=
'"') {
return false; }
148 return a.unsafe_is_equal(c);
151simdjson_unused simdjson_inline
bool operator==(std::string_view c,
const raw_json_string &a)
noexcept {
155simdjson_unused simdjson_inline
bool operator!=(
const raw_json_string &a, std::string_view c)
noexcept {
159simdjson_unused simdjson_inline
bool operator!=(std::string_view c,
const raw_json_string &a)
noexcept {
164simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> raw_json_string::unescape(json_iterator &iter,
bool allow_replacement)
const noexcept {
165 return iter.unescape(*
this, allow_replacement);
168simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> raw_json_string::unescape_wobbly(json_iterator &iter)
const noexcept {
169 return iter.unescape_wobbly(*
this);
172simdjson_unused simdjson_inline std::ostream &operator<<(std::ostream &out,
const raw_json_string &str)
noexcept {
173 bool in_escape =
false;
174 const char *s = str.raw();
177 case '\\': in_escape = !in_escape;
break;
178 case '"':
if (in_escape) { in_escape =
false; }
else {
return out; }
break;
179 default:
if (in_escape) { in_escape =
false; }
192simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string &&value) noexcept
193 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(value)) {}
194simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::simdjson_result(
error_code error) noexcept
195 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(error) {}
197simdjson_inline simdjson_result<const char *> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::raw() const noexcept {
198 if (error()) {
return error(); }
201simdjson_inline
char simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::operator[](
size_t i)
const noexcept {
202 if (error()) {
return error(); }
205simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::unescape(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &iter,
bool allow_replacement)
const noexcept {
206 if (error()) {
return error(); }
207 return first.unescape(iter, allow_replacement);
209simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::unescape_wobbly(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &iter)
const noexcept {
210 if (error()) {
return error(); }
211 return first.unescape_wobbly(iter);
A string escaped per JSON rules, terminated with quote (").
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-b...
static simdjson_inline bool is_free_from_unescaped_quote(std::string_view target) noexcept
Returns true if target is free from unescaped quote.
simdjson_inline raw_json_string() noexcept=default
Create a new invalid raw_json_string.
simdjson_inline const char * raw() const noexcept
Get the raw pointer to the beginning of the string in the JSON (just after the ").
simdjson_inline char operator[](size_t i) const noexcept
Get the character at index i.
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-b...
simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept
Comparisons between raw_json_string and std::string_view instances are potentially unsafe: the user i...
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.