simdjson 4.6.11
Ridiculously Fast JSON
Loading...
Searching...
No Matches
raw_json_string-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_RAW_JSON_STRING_INL_H
2
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"
9#endif // SIMDJSON_CONDITIONAL_INCLUDE
10
11namespace simdjson {
12
13namespace SIMDJSON_IMPLEMENTATION {
14namespace ondemand {
15
16simdjson_inline raw_json_string::raw_json_string(const uint8_t * _buf) noexcept : buf{_buf} {}
17
18simdjson_inline const char * raw_json_string::raw() const noexcept {
19 return reinterpret_cast<const char *>(buf);
20}
21
22simdjson_inline char raw_json_string::operator[](size_t i) const noexcept {
23 return reinterpret_cast<const char *>(buf)[i];
24}
25
26simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_view target) noexcept {
27 size_t pos{0};
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++; }
36 else { break; }
37 }
38 if(backslash_count % 2 == 0) { return false; }
39 }
40 pos++;
41 }
42 return true;
43}
44
45simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* target) noexcept {
46 size_t pos{0};
47 while(target[pos]) {
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++; }
56 else { break; }
57 }
58 if(backslash_count % 2 == 0) { return false; }
59 }
60 pos++;
61 }
62 return true;
63}
64
65
66simdjson_inline bool raw_json_string::unsafe_is_equal(size_t length, std::string_view target) const noexcept {
67 // If we are going to call memcmp, then we must know something about the length of the raw_json_string.
68 return (length >= target.size()) && (raw()[target.size()] == '"') && !memcmp(raw(), target.data(), target.size());
69}
70
71simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) const noexcept {
72 // Assumptions: does not contain unescaped quote characters("), and
73 // the raw content is quote terminated within a valid JSON string.
74 if(target.size() <= SIMDJSON_PADDING) {
75 return (raw()[target.size()] == '"') && !memcmp(raw(), target.data(), target.size());
76 }
77 // Past SIMDJSON_PADDING we can no longer rely on the padding to keep the
78 // comparison in bounds, so we must stop at the quote terminating the key.
79 return is_equal(target);
80}
81
82simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept {
83 const char * r{raw()};
84 size_t pos{0};
85 bool escaping{false};
86 for(;pos < target.size();pos++) {
87 if(r[pos] != target[pos]) { return false; }
88 // if target is a compile-time constant and it is free from
89 // quotes, then the next part could get optimized away through
90 // inlining.
91 if((target[pos] == '"') && !escaping) {
92 // We have reached the end of the raw_json_string but
93 // the target is not done.
94 return false;
95 } else if(target[pos] == '\\') {
96 escaping = !escaping;
97 } else {
98 escaping = false;
99 }
100 }
101 if(r[pos] != '"') { return false; }
102 return true;
103}
104
105
106simdjson_inline bool raw_json_string::unsafe_is_equal(const char * target) const noexcept {
107 // Assumptions: 'target' does not contain unescaped quote characters, is null terminated and
108 // the raw content is quote terminated within a valid JSON string.
109 const char * r{raw()};
110 size_t pos{0};
111 for(;target[pos];pos++) {
112 if(r[pos] != target[pos]) { return false; }
113 }
114 if(r[pos] != '"') { return false; }
115 return true;
116}
117
118simdjson_inline bool raw_json_string::is_equal(const char* target) const noexcept {
119 // Assumptions: does not contain unescaped quote characters, and
120 // the raw content is quote terminated within a valid JSON string.
121 const char * r{raw()};
122 size_t pos{0};
123 bool escaping{false};
124 for(;target[pos];pos++) {
125 if(r[pos] != target[pos]) { return false; }
126 // if target is a compile-time constant and it is free from
127 // quotes, then the next part could get optimized away through
128 // inlining.
129 if((target[pos] == '"') && !escaping) {
130 // We have reached the end of the raw_json_string but
131 // the target is not done.
132 return false;
133 } else if(target[pos] == '\\') {
134 escaping = !escaping;
135 } else {
136 escaping = false;
137 }
138 }
139 if(r[pos] != '"') { return false; }
140 return true;
141}
142
143simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept {
144 return a.unsafe_is_equal(c);
145}
146
147simdjson_unused simdjson_inline bool operator==(std::string_view c, const raw_json_string &a) noexcept {
148 return a == c;
149}
150
151simdjson_unused simdjson_inline bool operator!=(const raw_json_string &a, std::string_view c) noexcept {
152 return !(a == c);
153}
154
155simdjson_unused simdjson_inline bool operator!=(std::string_view c, const raw_json_string &a) noexcept {
156 return !(a == c);
157}
158
159
160simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> raw_json_string::unescape(json_iterator &iter, bool allow_replacement) const noexcept {
161 return iter.unescape(*this, allow_replacement);
162}
163
164simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> raw_json_string::unescape_wobbly(json_iterator &iter) const noexcept {
165 return iter.unescape_wobbly(*this);
166}
167
168simdjson_unused simdjson_inline std::ostream &operator<<(std::ostream &out, const raw_json_string &str) noexcept {
169 bool in_escape = false;
170 const char *s = str.raw();
171 while (true) {
172 switch (*s) {
173 case '\\': in_escape = !in_escape; break;
174 case '"': if (in_escape) { in_escape = false; } else { return out; } break;
175 default: if (in_escape) { in_escape = false; }
176 }
177 out << *s;
178 s++;
179 }
180}
181
182} // namespace ondemand
183} // namespace SIMDJSON_IMPLEMENTATION
184} // namespace simdjson
185
186namespace simdjson {
187
188simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string &&value) noexcept
189 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(value)) {}
190simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::simdjson_result(error_code error) noexcept
191 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>(error) {}
192
193simdjson_inline simdjson_result<const char *> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::raw() const noexcept {
194 if (error()) { return error(); }
195 return first.raw();
196}
197simdjson_inline char simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string>::operator[](size_t i) const noexcept {
198 if (error()) { return error(); }
199 return first[i];
200}
201simdjson_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 {
202 if (error()) { return error(); }
203 return first.unescape(iter, allow_replacement);
204}
205simdjson_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 {
206 if (error()) { return error(); }
207 return first.unescape_wobbly(iter);
208}
209} // namespace simdjson
210
211#endif // SIMDJSON_GENERIC_ONDEMAND_RAW_JSON_STRING_INL_H
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.
Definition base.h:8
error_code
All possible errors returned by simdjson.
Definition error.h:19
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
Definition base.h:33