@@ -41,65 +41,72 @@ namespace ondemand {
41
41
//
42
42
43
43
simdjson_really_inline array::array () noexcept = default;
44
- simdjson_really_inline array::array (document *_doc, json_iterator::container _container ) noexcept
45
- : doc{_doc}, container{_container}, at_start{ true }, error{SUCCESS}
44
+ simdjson_really_inline array::array (document *_doc, bool has_value ) noexcept
45
+ : doc{_doc}, has_next{has_value }, error{SUCCESS}
46
46
{
47
47
}
48
- simdjson_really_inline array::array (document *_doc, error_code _error ) noexcept
49
- : doc{_doc }, container{_doc-> iter . current_container () }, at_start{ false }, error{_error }
48
+ simdjson_really_inline array::array (array &&other ) noexcept
49
+ : doc{other. doc }, has_next{other. has_next }, error{other. error }
50
50
{
51
- SIMDJSON_ASSUME (_error);
51
+ // Terminate the other iterator
52
+ other.has_next = false ;
53
+ }
54
+ simdjson_really_inline array &array::operator =(array &&other) noexcept {
55
+ doc = other.doc ;
56
+ has_next = other.has_next ;
57
+ error = other.error ;
58
+ // Terminate the other iterator
59
+ other.has_next = false ;
60
+ return *this ;
52
61
}
53
62
54
- simdjson_really_inline bool array::finished () const noexcept {
55
- return !doc->iter .in_container (container);
63
+ simdjson_really_inline array::~array () noexcept {
64
+ if (!error && has_next) {
65
+ logger::log_event (doc->iter , " unfinished" , " array" );
66
+ doc->iter .skip_container ();
67
+ }
56
68
}
57
69
58
- simdjson_really_inline array array::start (document *doc) noexcept {
59
- error_code error;
60
- json_iterator::container c;
61
- if ((error = doc->iter .start_array ().get (c))) { return error_chain (doc, error); }
62
- return array (doc, c);
70
+ simdjson_really_inline simdjson_result<array> array::start (document *doc) noexcept {
71
+ bool has_value;
72
+ SIMDJSON_TRY ( doc->iter .start_array ().get (has_value) );
73
+ return array (doc, has_value);
63
74
}
64
75
simdjson_really_inline array array::started (document *doc) noexcept {
65
76
return array (doc, doc->iter .started_array ());
66
77
}
67
- simdjson_really_inline array array::error_chain (document *doc, error_code error) noexcept {
68
- return array (doc, error);
69
- }
70
- simdjson_really_inline array array::begin () noexcept {
78
+ simdjson_really_inline array::iterator array::begin () noexcept {
71
79
return *this ;
72
80
}
73
- simdjson_really_inline array array::end () noexcept {
74
- return {} ;
81
+ simdjson_really_inline array::iterator array::end () noexcept {
82
+ return * this ;
75
83
}
76
84
77
85
simdjson_really_inline error_code array::report_error () noexcept {
78
- container = doc->iter .current_container ().child (); // Make it so we'll stop
79
- auto result = error;
80
- error = SUCCESS;
81
- return result;
86
+ SIMDJSON_ASSUME (error);
87
+ has_next = false ;
88
+ return error;
82
89
}
83
90
84
- simdjson_really_inline simdjson_result<value> array::operator *() noexcept {
85
- if (error) { return { doc, report_error () }; }
86
- return value::start (doc);
91
+ simdjson_really_inline array::iterator::iterator (array &_a) noexcept : a{&_a} {}
92
+
93
+ simdjson_really_inline array::iterator::iterator () noexcept = default;
94
+ simdjson_really_inline array::iterator::iterator (const array::iterator &_a) noexcept = default;
95
+ simdjson_really_inline array::iterator &array::iterator::operator =(const array::iterator &_a) noexcept = default ;
96
+
97
+ simdjson_really_inline simdjson_result<value> array::iterator::operator *() noexcept {
98
+ if (a->error ) { return a->report_error (); }
99
+ return value::start (a->doc );
87
100
}
88
- simdjson_really_inline bool array::operator ==(const array &other ) noexcept {
89
- return !(* this != other) ;
101
+ simdjson_really_inline bool array::iterator:: operator ==(const array::iterator & ) noexcept {
102
+ return !a-> has_next ;
90
103
}
91
- simdjson_really_inline bool array::operator !=(const array &) noexcept {
92
- // If we're at the start, check for empty array.
93
- if (at_start) {
94
- at_start = false ;
95
- return !doc->iter .is_empty_array ();
96
- }
97
- return !finished ();
104
+ simdjson_really_inline bool array::iterator::operator !=(const array::iterator &) noexcept {
105
+ return a->has_next ;
98
106
}
99
- simdjson_really_inline array &array::operator ++() noexcept {
100
- SIMDJSON_ASSUME (!at_start);
101
-
102
- error = doc->iter .next_element (container).error ();
107
+ simdjson_really_inline array::iterator &array::iterator::operator ++() noexcept {
108
+ if (a->error ) { return *this ; }
109
+ a->error = a->doc ->iter .has_next_element ().get (a->has_next ); // If there's an error, has_next stays true.
103
110
return *this ;
104
111
}
105
112
@@ -111,25 +118,82 @@ namespace simdjson {
111
118
112
119
simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::simdjson_result(
113
120
SIMDJSON_IMPLEMENTATION::ondemand::array &&value
114
- ) noexcept :
115
- internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>(
116
- std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array>(value)
117
- )
121
+ ) noexcept
122
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>(
123
+ std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array>(value)
124
+ )
118
125
{
119
126
}
120
127
simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::simdjson_result(
121
- SIMDJSON_IMPLEMENTATION::ondemand::document *doc,
122
128
error_code error
123
- ) noexcept :
124
- internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>({ doc, error }, error)
129
+ ) noexcept
130
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>(error)
131
+ {
132
+ }
133
+
134
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::begin() noexcept {
135
+ if (error ()) { return error (); }
136
+ return first.begin ();
137
+ }
138
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::end() noexcept {
139
+ if (error ()) { return error (); }
140
+ return first.end ();
141
+ }
142
+
143
+ //
144
+ // array::iterator
145
+ //
146
+
147
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::simdjson_result() noexcept
148
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>({}, SUCCESS)
149
+ {
150
+ }
151
+
152
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::simdjson_result(
153
+ SIMDJSON_IMPLEMENTATION::ondemand::array::iterator &&value
154
+ ) noexcept
155
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>(value))
156
+ {
157
+ }
158
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::simdjson_result(error_code error) noexcept
159
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>({}, error)
125
160
{
126
161
}
127
162
128
- simdjson_really_inline SIMDJSON_IMPLEMENTATION::ondemand::array simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::begin() noexcept {
129
- return first;
163
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::simdjson_result(
164
+ const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &a
165
+ ) noexcept
166
+ : internal::simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>(a)
167
+ {
130
168
}
131
- simdjson_really_inline SIMDJSON_IMPLEMENTATION::ondemand::array simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::end() noexcept {
132
- return {};
169
+
170
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::operator =(
171
+ const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &a
172
+ ) noexcept {
173
+ first = a.first ;
174
+ second = a.second ;
175
+ return *this ;
176
+ }
177
+
178
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::operator *() noexcept {
179
+ if (error ()) { return error (); }
180
+ return *first;
181
+ }
182
+ // Assumes it's being compared with the end. true if depth < doc->iter.depth.
183
+ simdjson_really_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::operator ==(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &other) noexcept {
184
+ if (error ()) { return true ; }
185
+ return first == other.first ;
186
+ }
187
+ // Assumes it's being compared with the end. true if depth >= doc->iter.depth.
188
+ simdjson_really_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::operator !=(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &other) noexcept {
189
+ if (error ()) { return false ; }
190
+ return first != other.first ;
191
+ }
192
+ // Checks for ']' and ','
193
+ simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array::iterator>::operator ++() noexcept {
194
+ if (error ()) { return *this ; }
195
+ ++first;
196
+ return *this ;
133
197
}
134
198
135
199
} // namespace simdjson
0 commit comments