ox 2.14.14 → 2.14.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/ox/hash_load.c CHANGED
@@ -3,24 +3,23 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #include <stdbool.h>
7
- #include <stdlib.h>
8
6
  #include <errno.h>
7
+ #include <stdarg.h>
8
+ #include <stdbool.h>
9
9
  #include <stdio.h>
10
+ #include <stdlib.h>
10
11
  #include <string.h>
11
- #include <stdarg.h>
12
12
 
13
- #include "ruby.h"
14
13
  #include "ox.h"
14
+ #include "ruby.h"
15
15
 
16
- #define MARK_INC 256
16
+ #define MARK_INC 256
17
17
 
18
18
  // The approach taken for the hash and has_no_attrs parsing is to push just
19
19
  // the key on to the stack and then decide what to do on the way up/out.
20
20
 
21
- static VALUE
22
- create_top(PInfo pi) {
23
- volatile VALUE top = rb_hash_new();
21
+ static VALUE create_top(PInfo pi) {
22
+ volatile VALUE top = rb_hash_new();
24
23
 
25
24
  helper_stack_push(&pi->helpers, 0, top, HashCode);
26
25
  pi->obj = top;
@@ -28,221 +27,204 @@ create_top(PInfo pi) {
28
27
  return top;
29
28
  }
30
29
 
31
- static void
32
- mark_value(PInfo pi, VALUE val) {
30
+ static void mark_value(PInfo pi, VALUE val) {
33
31
  if (NULL == pi->marked) {
34
- pi->marked = ALLOC_N(VALUE, MARK_INC);
35
- pi->mark_size = MARK_INC;
32
+ pi->marked = ALLOC_N(VALUE, MARK_INC);
33
+ pi->mark_size = MARK_INC;
36
34
  } else if (pi->mark_size <= pi->mark_cnt) {
37
- pi->mark_size += MARK_INC;
38
- REALLOC_N(pi->marked, VALUE, pi->mark_size);
35
+ pi->mark_size += MARK_INC;
36
+ REALLOC_N(pi->marked, VALUE, pi->mark_size);
39
37
  }
40
38
  pi->marked[pi->mark_cnt] = val;
41
39
  pi->mark_cnt++;
42
40
  }
43
41
 
44
- static bool
45
- marked(PInfo pi, VALUE val) {
42
+ static bool marked(PInfo pi, VALUE val) {
46
43
  if (NULL != pi->marked) {
47
- VALUE *vp = pi->marked + pi->mark_cnt - 1;
44
+ VALUE *vp = pi->marked + pi->mark_cnt - 1;
48
45
 
49
- for (; pi->marked <= vp; vp--) {
50
- if (val == *vp) {
51
- return true;
52
- }
53
- }
46
+ for (; pi->marked <= vp; vp--) {
47
+ if (val == *vp) {
48
+ return true;
49
+ }
50
+ }
54
51
  }
55
52
  return false;
56
53
  }
57
54
 
58
- static void
59
- unmark(PInfo pi, VALUE val) {
55
+ static void unmark(PInfo pi, VALUE val) {
60
56
  if (NULL != pi->marked) {
61
- VALUE *vp = pi->marked + pi->mark_cnt - 1;
62
- int i;
63
-
64
- for (i = 0; pi->marked <= vp; vp--, i++) {
65
- if (val == *vp) {
66
- for (; 0 < i; i--, vp++) {
67
- *vp = *(vp + 1);
68
- }
69
- pi->mark_cnt--;
70
- break;
71
- }
72
- }
57
+ VALUE *vp = pi->marked + pi->mark_cnt - 1;
58
+ int i;
59
+
60
+ for (i = 0; pi->marked <= vp; vp--, i++) {
61
+ if (val == *vp) {
62
+ for (; 0 < i; i--, vp++) {
63
+ *vp = *(vp + 1);
64
+ }
65
+ pi->mark_cnt--;
66
+ break;
67
+ }
68
+ }
73
69
  }
74
70
  }
75
71
 
76
- static void
77
- add_str(PInfo pi, VALUE s) {
78
- Helper parent = helper_stack_peek(&pi->helpers);
79
- volatile VALUE a;
72
+ static void add_str(PInfo pi, VALUE s) {
73
+ Helper parent = helper_stack_peek(&pi->helpers);
74
+ volatile VALUE a;
80
75
 
81
76
  if (0 != pi->options->rb_enc) {
82
77
  rb_enc_associate(s, pi->options->rb_enc);
83
78
  }
84
79
  switch (parent->type) {
85
80
  case NoCode:
86
- parent->obj = s;
87
- parent->type = StringCode;
88
- break;
89
- case ArrayCode:
90
- rb_ary_push(parent->obj, s);
91
- break;
81
+ parent->obj = s;
82
+ parent->type = StringCode;
83
+ break;
84
+ case ArrayCode: rb_ary_push(parent->obj, s); break;
92
85
  default:
93
- a = rb_ary_new();
94
- rb_ary_push(a, parent->obj);
95
- rb_ary_push(a, s);
96
- parent->obj = a;
97
- parent->type = ArrayCode;
98
- break;
86
+ a = rb_ary_new();
87
+ rb_ary_push(a, parent->obj);
88
+ rb_ary_push(a, s);
89
+ parent->obj = a;
90
+ parent->type = ArrayCode;
91
+ break;
99
92
  }
100
93
  }
101
94
 
102
- static void
103
- add_text(PInfo pi, char *text, int closed) {
95
+ static void add_text(PInfo pi, char *text, int closed) {
104
96
  add_str(pi, rb_str_new2(text));
105
97
  }
106
98
 
107
- static void
108
- add_cdata(PInfo pi, const char *text, size_t len) {
99
+ static void add_cdata(PInfo pi, const char *text, size_t len) {
109
100
  add_str(pi, rb_str_new(text, len));
110
101
  }
111
102
 
112
- static void
113
- add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
103
+ static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
114
104
  if (helper_stack_empty(&pi->helpers)) {
115
- create_top(pi);
105
+ create_top(pi);
116
106
  }
117
107
  if (NULL != attrs && NULL != attrs->name) {
118
- volatile VALUE h = rb_hash_new();
119
- volatile VALUE key;
120
- volatile VALUE val;
121
- volatile VALUE a;
108
+ volatile VALUE h = rb_hash_new();
109
+ volatile VALUE key;
110
+ volatile VALUE val;
111
+ volatile VALUE a;
122
112
 
123
113
  for (; 0 != attrs->name; attrs++) {
124
- if (Qnil != pi->options->attr_key_mod) {
125
- key = rb_funcall(pi->options->attr_key_mod, ox_call_id, 1, rb_str_new2(attrs->name));
126
- } else if (Yes == pi->options->sym_keys) {
127
- key = rb_id2sym(rb_intern(attrs->name));
128
- } else {
129
- key = rb_str_new2(attrs->name);
130
- }
131
- val = rb_str_new2(attrs->value);
132
- if (0 != pi->options->rb_enc) {
133
- rb_enc_associate(val, pi->options->rb_enc);
134
- }
135
- rb_hash_aset(h, key, val);
136
- }
137
- a = rb_ary_new();
138
- rb_ary_push(a, h);
139
- mark_value(pi, a);
140
- helper_stack_push(&pi->helpers, rb_intern(ename), a, ArrayCode);
114
+ if (Qnil != pi->options->attr_key_mod) {
115
+ key = rb_funcall(pi->options->attr_key_mod, ox_call_id, 1, rb_str_new2(attrs->name));
116
+ } else if (Yes == pi->options->sym_keys) {
117
+ key = rb_id2sym(rb_intern(attrs->name));
118
+ } else {
119
+ key = rb_str_new2(attrs->name);
120
+ }
121
+ val = rb_str_new2(attrs->value);
122
+ if (0 != pi->options->rb_enc) {
123
+ rb_enc_associate(val, pi->options->rb_enc);
124
+ }
125
+ rb_hash_aset(h, key, val);
126
+ }
127
+ a = rb_ary_new();
128
+ rb_ary_push(a, h);
129
+ mark_value(pi, a);
130
+ helper_stack_push(&pi->helpers, rb_intern(ename), a, ArrayCode);
141
131
  } else {
142
- helper_stack_push(&pi->helpers, rb_intern(ename), Qnil, NoCode);
132
+ helper_stack_push(&pi->helpers, rb_intern(ename), Qnil, NoCode);
143
133
  }
144
134
  }
145
135
 
146
- static void
147
- add_element_no_attrs(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
136
+ static void add_element_no_attrs(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
148
137
  if (helper_stack_empty(&pi->helpers)) {
149
- create_top(pi);
138
+ create_top(pi);
150
139
  }
151
140
  helper_stack_push(&pi->helpers, rb_intern(ename), Qnil, NoCode);
152
141
  }
153
142
 
154
- static int
155
- umark_hash_cb(VALUE key, VALUE value, VALUE x) {
143
+ static int umark_hash_cb(VALUE key, VALUE value, VALUE x) {
156
144
  unmark((PInfo)x, value);
157
145
 
158
146
  return ST_CONTINUE;
159
147
  }
160
148
 
161
- static void
162
- end_element_core(PInfo pi, const char *ename, bool check_marked) {
163
- Helper e = helper_stack_pop(&pi->helpers);
164
- Helper parent = helper_stack_peek(&pi->helpers);
165
- volatile VALUE pobj = parent->obj;
166
- volatile VALUE found = Qundef;
167
- volatile VALUE key;
168
- volatile VALUE a;
149
+ static void end_element_core(PInfo pi, const char *ename, bool check_marked) {
150
+ Helper e = helper_stack_pop(&pi->helpers);
151
+ Helper parent = helper_stack_peek(&pi->helpers);
152
+ volatile VALUE pobj = parent->obj;
153
+ volatile VALUE found = Qundef;
154
+ volatile VALUE key;
155
+ volatile VALUE a;
169
156
 
170
157
  if (NoCode == e->type) {
171
- e->obj = Qnil;
158
+ e->obj = Qnil;
172
159
  }
173
160
  if (Qnil != pi->options->element_key_mod) {
174
- key = rb_funcall(pi->options->element_key_mod, ox_call_id, 1, rb_id2str(e->var));
161
+ key = rb_funcall(pi->options->element_key_mod, ox_call_id, 1, rb_id2str(e->var));
175
162
  } else if (Yes == pi->options->sym_keys) {
176
- key = rb_id2sym(e->var);
163
+ key = rb_id2sym(e->var);
177
164
  } else {
178
- key = rb_id2str(e->var);
165
+ key = rb_id2str(e->var);
179
166
  }
180
167
  // Make sure the parent is a Hash. If not set then make a Hash. If an
181
168
  // Array or non-Hash then append to array or create and append.
182
169
  switch (parent->type) {
183
170
  case NoCode:
184
- pobj = rb_hash_new();
185
- parent->obj = pobj;
186
- parent->type = HashCode;
187
- break;
171
+ pobj = rb_hash_new();
172
+ parent->obj = pobj;
173
+ parent->type = HashCode;
174
+ break;
188
175
  case ArrayCode:
189
- pobj = rb_hash_new();
190
- rb_ary_push(parent->obj, pobj);
191
- break;
192
- case HashCode:
193
- found = rb_hash_lookup2(parent->obj, key, Qundef);
194
- break;
176
+ pobj = rb_hash_new();
177
+ rb_ary_push(parent->obj, pobj);
178
+ break;
179
+ case HashCode: found = rb_hash_lookup2(parent->obj, key, Qundef); break;
195
180
  default:
196
- a = rb_ary_new();
197
- rb_ary_push(a, parent->obj);
198
- pobj = rb_hash_new();
199
- rb_ary_push(a, pobj);
200
- parent->obj = a;
201
- parent->type = ArrayCode;
202
- break;
181
+ a = rb_ary_new();
182
+ rb_ary_push(a, parent->obj);
183
+ pobj = rb_hash_new();
184
+ rb_ary_push(a, pobj);
185
+ parent->obj = a;
186
+ parent->type = ArrayCode;
187
+ break;
203
188
  }
204
189
  if (Qundef == found) {
205
- rb_hash_aset(pobj, key, e->obj);
190
+ rb_hash_aset(pobj, key, e->obj);
206
191
  } else if (RUBY_T_ARRAY == rb_type(found)) {
207
- if (check_marked && marked(pi, found)) {
208
- unmark(pi, found);
209
- a = rb_ary_new();
210
- rb_ary_push(a, found);
211
- rb_ary_push(a, e->obj);
212
- rb_hash_aset(pobj, key, a);
213
- } else {
214
- rb_ary_push(found, e->obj);
215
- }
216
- } else { // something there other than an array
217
- if (check_marked && marked(pi, e->obj)) {
218
- unmark(pi, e->obj);
219
- }
220
- a = rb_ary_new();
221
- rb_ary_push(a, found);
222
- rb_ary_push(a, e->obj);
223
- rb_hash_aset(pobj, key, a);
192
+ if (check_marked && marked(pi, found)) {
193
+ unmark(pi, found);
194
+ a = rb_ary_new();
195
+ rb_ary_push(a, found);
196
+ rb_ary_push(a, e->obj);
197
+ rb_hash_aset(pobj, key, a);
198
+ } else {
199
+ rb_ary_push(found, e->obj);
200
+ }
201
+ } else { // something there other than an array
202
+ if (check_marked && marked(pi, e->obj)) {
203
+ unmark(pi, e->obj);
204
+ }
205
+ a = rb_ary_new();
206
+ rb_ary_push(a, found);
207
+ rb_ary_push(a, e->obj);
208
+ rb_hash_aset(pobj, key, a);
224
209
  }
225
210
  if (check_marked && NULL != pi->marked && RUBY_T_HASH == rb_type(e->obj)) {
226
- rb_hash_foreach(e->obj, umark_hash_cb, (VALUE)pi);
211
+ rb_hash_foreach(e->obj, umark_hash_cb, (VALUE)pi);
227
212
  }
228
213
  }
229
214
 
230
- static void
231
- end_element(PInfo pi, const char *ename) {
215
+ static void end_element(PInfo pi, const char *ename) {
232
216
  end_element_core(pi, ename, true);
233
217
  }
234
218
 
235
- static void
236
- end_element_no_attrs(PInfo pi, const char *ename) {
219
+ static void end_element_no_attrs(PInfo pi, const char *ename) {
237
220
  end_element_core(pi, ename, false);
238
221
  }
239
222
 
240
- static void
241
- finish(PInfo pi) {
223
+ static void finish(PInfo pi) {
242
224
  xfree(pi->marked);
243
225
  }
244
226
 
245
- struct _parseCallbacks _ox_hash_callbacks = {
227
+ struct _parseCallbacks _ox_hash_callbacks = {
246
228
  NULL,
247
229
  NULL,
248
230
  NULL,
@@ -253,9 +235,9 @@ struct _parseCallbacks _ox_hash_callbacks = {
253
235
  finish,
254
236
  };
255
237
 
256
- ParseCallbacks ox_hash_callbacks = &_ox_hash_callbacks;
238
+ ParseCallbacks ox_hash_callbacks = &_ox_hash_callbacks;
257
239
 
258
- struct _parseCallbacks _ox_hash_cdata_callbacks = {
240
+ struct _parseCallbacks _ox_hash_cdata_callbacks = {
259
241
  NULL,
260
242
  NULL,
261
243
  NULL,
@@ -266,9 +248,9 @@ struct _parseCallbacks _ox_hash_cdata_callbacks = {
266
248
  finish,
267
249
  };
268
250
 
269
- ParseCallbacks ox_hash_cdata_callbacks = &_ox_hash_cdata_callbacks;
251
+ ParseCallbacks ox_hash_cdata_callbacks = &_ox_hash_cdata_callbacks;
270
252
 
271
- struct _parseCallbacks _ox_hash_no_attrs_callbacks = {
253
+ struct _parseCallbacks _ox_hash_no_attrs_callbacks = {
272
254
  NULL,
273
255
  NULL,
274
256
  NULL,
@@ -279,9 +261,9 @@ struct _parseCallbacks _ox_hash_no_attrs_callbacks = {
279
261
  NULL,
280
262
  };
281
263
 
282
- ParseCallbacks ox_hash_no_attrs_callbacks = &_ox_hash_no_attrs_callbacks;
264
+ ParseCallbacks ox_hash_no_attrs_callbacks = &_ox_hash_no_attrs_callbacks;
283
265
 
284
- struct _parseCallbacks _ox_hash_no_attrs_cdata_callbacks = {
266
+ struct _parseCallbacks _ox_hash_no_attrs_cdata_callbacks = {
285
267
  NULL,
286
268
  NULL,
287
269
  NULL,
@@ -292,4 +274,4 @@ struct _parseCallbacks _ox_hash_no_attrs_cdata_callbacks = {
292
274
  NULL,
293
275
  };
294
276
 
295
- ParseCallbacks ox_hash_no_attrs_cdata_callbacks = &_ox_hash_no_attrs_cdata_callbacks;
277
+ ParseCallbacks ox_hash_no_attrs_cdata_callbacks = &_ox_hash_no_attrs_cdata_callbacks;
data/ext/ox/helper.h CHANGED
@@ -8,82 +8,75 @@
8
8
 
9
9
  #include "type.h"
10
10
 
11
- #define HELPER_STACK_INC 16
11
+ #define HELPER_STACK_INC 16
12
12
 
13
13
  typedef struct _helper {
14
- ID var; /* Object var ID */
15
- VALUE obj; /* object created or Qundef if not appropriate */
16
- Type type; /* type of object in obj */
14
+ ID var; /* Object var ID */
15
+ VALUE obj; /* object created or Qundef if not appropriate */
16
+ Type type; /* type of object in obj */
17
17
  } *Helper;
18
18
 
19
19
  typedef struct _helperStack {
20
- struct _helper base[HELPER_STACK_INC];
21
- Helper head; /* current stack */
22
- Helper end; /* stack end */
23
- Helper tail; /* pointer to one past last element name on stack */
20
+ struct _helper base[HELPER_STACK_INC];
21
+ Helper head; /* current stack */
22
+ Helper end; /* stack end */
23
+ Helper tail; /* pointer to one past last element name on stack */
24
24
  } *HelperStack;
25
25
 
26
- inline static void
27
- helper_stack_init(HelperStack stack) {
26
+ inline static void helper_stack_init(HelperStack stack) {
28
27
  stack->head = stack->base;
29
- stack->end = stack->base + sizeof(stack->base) / sizeof(struct _helper);
28
+ stack->end = stack->base + sizeof(stack->base) / sizeof(struct _helper);
30
29
  stack->tail = stack->head;
31
30
  }
32
31
 
33
- inline static int
34
- helper_stack_empty(HelperStack stack) {
32
+ inline static int helper_stack_empty(HelperStack stack) {
35
33
  return (stack->head == stack->tail);
36
34
  }
37
35
 
38
- inline static int
39
- helper_stack_depth(HelperStack stack) {
36
+ inline static int helper_stack_depth(HelperStack stack) {
40
37
  return (int)(stack->tail - stack->head);
41
38
  }
42
39
 
43
- inline static void
44
- helper_stack_cleanup(HelperStack stack) {
40
+ inline static void helper_stack_cleanup(HelperStack stack) {
45
41
  if (stack->base != stack->head) {
46
42
  xfree(stack->head);
47
- stack->head = stack->base;
43
+ stack->head = stack->base;
48
44
  }
49
45
  }
50
46
 
51
- inline static Helper
52
- helper_stack_push(HelperStack stack, ID var, VALUE obj, Type type) {
47
+ inline static Helper helper_stack_push(HelperStack stack, ID var, VALUE obj, Type type) {
53
48
  if (stack->end <= stack->tail) {
54
- size_t len = stack->end - stack->head;
55
- size_t toff = stack->tail - stack->head;
49
+ size_t len = stack->end - stack->head;
50
+ size_t toff = stack->tail - stack->head;
56
51
 
57
- if (stack->base == stack->head) {
58
- stack->head = ALLOC_N(struct _helper, len + HELPER_STACK_INC);
59
- memcpy(stack->head, stack->base, sizeof(struct _helper) * len);
60
- } else {
61
- REALLOC_N(stack->head, struct _helper, len + HELPER_STACK_INC);
62
- }
63
- stack->tail = stack->head + toff;
64
- stack->end = stack->head + len + HELPER_STACK_INC;
52
+ if (stack->base == stack->head) {
53
+ stack->head = ALLOC_N(struct _helper, len + HELPER_STACK_INC);
54
+ memcpy(stack->head, stack->base, sizeof(struct _helper) * len);
55
+ } else {
56
+ REALLOC_N(stack->head, struct _helper, len + HELPER_STACK_INC);
57
+ }
58
+ stack->tail = stack->head + toff;
59
+ stack->end = stack->head + len + HELPER_STACK_INC;
65
60
  }
66
- stack->tail->var = var;
67
- stack->tail->obj = obj;
61
+ stack->tail->var = var;
62
+ stack->tail->obj = obj;
68
63
  stack->tail->type = type;
69
64
  stack->tail++;
70
65
 
71
66
  return stack->tail - 1;
72
67
  }
73
68
 
74
- inline static Helper
75
- helper_stack_peek(HelperStack stack) {
69
+ inline static Helper helper_stack_peek(HelperStack stack) {
76
70
  if (stack->head < stack->tail) {
77
- return stack->tail - 1;
71
+ return stack->tail - 1;
78
72
  }
79
73
  return 0;
80
74
  }
81
75
 
82
- inline static Helper
83
- helper_stack_pop(HelperStack stack) {
76
+ inline static Helper helper_stack_pop(HelperStack stack) {
84
77
  if (stack->head < stack->tail) {
85
- stack->tail--;
86
- return stack->tail;
78
+ stack->tail--;
79
+ return stack->tail;
87
80
  }
88
81
  return 0;
89
82
  }
data/ext/ox/intern.c CHANGED
@@ -5,10 +5,9 @@
5
5
 
6
6
  #include <stdint.h>
7
7
 
8
- #include "ruby/version.h"
9
-
10
8
  #include "cache.h"
11
9
  #include "ox.h"
10
+ #include "ruby/version.h"
12
11
 
13
12
  // These are statics but in an attempt to stop the cross linking or maybe
14
13
  // something in Ruby they all have been given an ox prefix.