|
3 | 3 | #include <iterator> |
4 | 4 |
|
5 | 5 | ParsedJson::iterator::iterator(ParsedJson &pj_) : pj(pj_), depth(0), location(0), tape_length(0), depthindex(nullptr) { |
6 | | - if(pj.isValid()) { |
7 | | - depthindex = new scopeindex_t[pj.depthcapacity]; |
8 | | - if(depthindex == nullptr) { return; |
9 | | -} |
10 | | - depthindex[0].start_of_scope = location; |
11 | | - current_val = pj.tape[location++]; |
12 | | - current_type = (current_val >> 56); |
13 | | - depthindex[0].scope_type = current_type; |
14 | | - if (current_type == 'r') { |
15 | | - tape_length = current_val & JSONVALUEMASK; |
16 | | - if(location < tape_length) { |
| 6 | + if(!pj.isValid()) { |
| 7 | + throw InvalidJSON(); |
| 8 | + } |
| 9 | + depthindex = new scopeindex_t[pj.depthcapacity]; |
| 10 | + // memory allocation would throw |
| 11 | + //if(depthindex == nullptr) { |
| 12 | + // return; |
| 13 | + //} |
| 14 | + depthindex[0].start_of_scope = location; |
| 15 | + current_val = pj.tape[location++]; |
| 16 | + current_type = (current_val >> 56); |
| 17 | + depthindex[0].scope_type = current_type; |
| 18 | + if (current_type == 'r') { |
| 19 | + tape_length = current_val & JSONVALUEMASK; |
| 20 | + if(location < tape_length) { |
17 | 21 | current_val = pj.tape[location]; |
18 | 22 | current_type = (current_val >> 56); |
19 | 23 | depth++; |
20 | 24 | depthindex[depth].start_of_scope = location; |
21 | 25 | depthindex[depth].scope_type = current_type; |
22 | 26 | } |
23 | | - } |
| 27 | + } else { |
| 28 | + // should never happen |
| 29 | + throw InvalidJSON(); |
24 | 30 | } |
25 | | - } |
| 31 | +} |
26 | 32 |
|
27 | 33 | ParsedJson::iterator::~iterator() { |
28 | 34 | delete[] depthindex; |
29 | 35 | } |
30 | 36 |
|
31 | 37 | ParsedJson::iterator::iterator(const iterator &o): |
32 | 38 | pj(o.pj), depth(o.depth), location(o.location), |
33 | | - tape_length(o.tape_length), current_type(o.current_type), |
| 39 | + tape_length(0), current_type(o.current_type), |
34 | 40 | current_val(o.current_val), depthindex(nullptr) { |
35 | 41 | depthindex = new scopeindex_t[pj.depthcapacity]; |
36 | | - if(depthindex != nullptr) { |
37 | | - memcpy(depthindex, o.depthindex, pj.depthcapacity * sizeof(depthindex[0])); |
38 | | - } else { |
39 | | - tape_length = 0; |
40 | | - } |
| 42 | + // allocation might throw |
| 43 | + memcpy(depthindex, o.depthindex, pj.depthcapacity * sizeof(depthindex[0])); |
| 44 | + tape_length = o.tape_length; |
41 | 45 | } |
42 | 46 |
|
43 | 47 | ParsedJson::iterator::iterator(iterator &&o): |
|
0 commit comments